Spring 定时任务和异步处理

import org.springframework.scheduling.annotation.Async;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

 

 

@Component

public class MailUtility {

 

//@Scheduled(cron="0/5 * *  * * ? ")

@Async

public void sendMail(){

 

 System.out.println("I Will be formatting html mail and sending it");

 

try {

    Thread.sleep(10000);

 

} catch (InterruptedException e) {

 

     e.printStackTrace();

}

 

System.out.println("Asynchronous method call of send email — Complete");

 

}

 

}

 

 

 

配置文件中:

 

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

    <task:scheduler id="scheduler" pool-size="10" />  

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

http://www.springframework.org/schema/task  

http://www.springframework.org/schema/task/spring-task-3.0.xsd

 xmlns:task="http://www.springframework.org/schema/task

 异步处理使用在较长时间的处理中,先返回界面。

猜你喜欢

转载自guanxi.iteye.com/blog/2284671