Spring 3:如何从TaskExecutor调用@Async注释方法

时间:2013-07-19 12:44:22

标签: java multithreading spring spring-mvc asynchronous

我是Spring的异步任务执行新手,所以如果这听起来像个愚蠢的问题,请原谅我。

我读到从Spring 3.x开始在方法级别引入@Async注释,以便异步发生该方法的调用。 我还读到我们可以在spring配置文件中配置ThreadPoolTask​​Executor。

我无法理解的是,如何从tak执行器调用@Async注释方法让我们假设 - AsyncTaskExecutor

早些时候我们曾经做过类似的事情:

@Autowired protected AsyncTaskExecutor executor;

然后

executor.submit(<Some Runnable or Callable task>)

我无法理解@Async注释方法与TaskExecutor之间的关系。

我尝试在互联网上搜索很多东西,但却无法得到任何东西。

有人可以为此提供一个例子。

4 个答案:

答案 0 :(得分:30)

以下是@Async使用的示例:

@Async
void doSomething() {
    // this will be executed asynchronously
}

现在从另一个类调用该方法,它将异步运行。如果您想要返回值,请使用Future

@Async
Future<String> returnSomething(int i) {
    // this will be executed asynchronously
}

@AsyncTaskExecutor之间的关系是@Async在幕后使用TaskExecutor。来自文档:

  

默认情况下,在方法上指定@Async时,将使用的执行程序是提供给'annotation-driven'元素的执行程序,如上所述。但是,当需要指示在执行给定方法时应该使用除默认值之外的执行程序时,可以使用@Async批注的value属性。

因此,要设置默认执行程序,请将其添加到spring config

<task:annotation-driven executor="myExecutor" />

或者使用特定执行程序一次性尝试

@Async("otherExecutor")

请参阅http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-async

答案 1 :(得分:3)

完整示例

  1. Config Spring

    @Configuration        
    @EnableAsync        
    @ComponentScan("com.async")
    public class AppConfig {
    
        @Bean
        public AsyncManager asyncManger() {
            return new AsyncManager();
        }
    
        @Bean
        public AsyncExecutor asyncExecutor() {
            return new AsyncExecutor();
        }
    }
    
  2. Executor Class Created,Executor我已创建,以便spring负责线程管理。

    public class AsyncExecutor extends AsyncConfigurerSupport {
    
        @Override
        public Executor getAsyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(2);
            executor.setMaxPoolSize(2);
            executor.setQueueCapacity(500);
            executor.setThreadNamePrefix("Violation-");
            executor.initialize();
            return executor;
        }
    }
    
  3. 创建经理。

    public class AsyncManager {
    
        @Autowired
        private AsyncService asyncService;
    
        public void doAsyncTask(){
            try {
                Map<Long, ViolationDetails> violation = asyncService.getViolation();
                if(!org.springframework.util.CollectionUtils.isEmpty(violation)){
                    violation.entrySet().forEach( violationEntry -> {System.out.println(violationEntry.getKey() +"" +violationEntry.getValue());});
                }
                System.out.println("do some async task");
            } catch (Exception e) {
            }
    
        }
    }
    
  4. 配置您的服务类。

    @Service
    public class AsyncService {
    
        @Autowired
        private AsyncExecutor asyncExecutor;
    
        @Async
        public Map<Long,ViolationDetails> getViolation() {
            // TODO Auto-generated method stub
            List<Long> list = Arrays.asList(100l,200l,300l,400l,500l,600l,700l);
            Executor executor = asyncExecutor.getAsyncExecutor();
            Map<Long,ViolationDetails>  returnMap = new HashMap<>();
            for(Long estCode : list){
                ViolationDetails violationDetails = new ViolationDetails(estCode);
                returnMap.put(estCode, violationDetails);
                executor.execute((Runnable)new ViolationWorker(violationDetails));
            }
            return returnMap;       
        }
    }
    class ViolationWorker implements Runnable{
    
        private ViolationDetails violationDetails;
    
        public ViolationWorker(ViolationDetails violationDetails){
            this.violationDetails = violationDetails;
        }
    
        @Override
        public void run() {
            violationDetails.setViolation(System.currentTimeMillis());
            System.out.println(violationDetails.getEstablishmentID() + "    " + violationDetails.getViolation());
        }
    }
    
  5. 模型。

    public class ViolationDetails {
        private long establishmentID;
        private long violation;
    
    
        public ViolationDetails(long establishmentID){
            this.establishmentID = establishmentID;
        }
    
        public long getEstablishmentID() {
            return establishmentID;
        }
        public void setEstablishmentID(long establishmentID) {
            this.establishmentID = establishmentID;
        }
        public long getViolation() {
            return violation;
        }
        public void setViolation(long violation) {
            this.violation = violation;
        }
    
    }
    
  6. 测试运行

    public class AppTest {
        public static void main(String[] args) throws SQLException {
            AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
            ctx.register(AppConfig.class);
            ctx.refresh();
    
            AsyncManager task= ctx.getBean(AsyncManager.class);
            task.doAsyncTask();
        }
    }
    

答案 2 :(得分:1)

在配置文件中,应该提到带有线程池名称的注释驱动任务,带有@Async(池名称)的方法将作为该池的一部分执行。这将为具有@Async批注的代理类创建一个代理类,并为每个线程执行它。

答案 3 :(得分:0)

您可以在方法和以下内容中将@Async添加到您的应用程序上下文中。

    <task:annotation-driven executor="asynExecutor"/>   
    <task:executor id="asynExecutor" pool-size="5" />