My Blog List

Thursday, April 18, 2013

@Async in Spring Future

FutureAnnotaion class
/**
 * (c)Copyright 2010-2010, BaruSoft Co., Ltd. All rights reserved 
* * @description
* * @create 2013. 4. 18. * @author jjhangu */ package com.jjhangu.core; import java.util.concurrent.Future; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; /** * @author jjhangu * */ public class FutureAnnotaion { @Async public Future getMessage(int j) { final int result = 0; for (int i = 0; i < 10000000; i++) { if (i == (10000000 - 1)) { System.out.println("finish : " + j); } } return new AsyncResult(result); } }
FutureAnnotationMain class
/**
 * (c)Copyright 2010-2010, BaruSoft Co., Ltd. All rights reserved 
* * @description
* * @create 2013. 4. 18. * @author jjhangu */ package com.jjhangu.core; import java.util.concurrent.ExecutionException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author jjhangu * */ public class FutureAnnotationMain { public static void main(String args[]) throws InterruptedException, ExecutionException { final ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml"); final FutureAnnotaion obj = (FutureAnnotaion) context.getBean("futureAnnotaion"); for (int i = 0; i < 10; i++) { obj.getMessage(i); } } }
SpringBeans.xml


      
 
      
 

 
 
 
pom.xml

 4.0.0
 com.mkyong.core
 Spring3Example
 jar
 1.0-SNAPSHOT
 Spring3Example
 http://maven.apache.org

 
  3.0.5.RELEASE
 

 
  
   junit
   junit
   4.8.2
   test
  

  
  

  
   org.springframework
   spring-context
   ${spring.version}
  

  
   cglib
   cglib
   2.2.2
  

 


log
finish : 8
finish : 4
finish : 9
finish : 7
finish : 6
finish : 5
finish : 3
finish : 0
finish : 2
finish : 1

No comments:

Post a Comment