SpringMVC 異步與定時使用示例

1.Spring 的xml配置:

<aop:aspectj-autoproxy/>
<task:annotation-driven executor="annotationExecutor" scheduler="scheduler"/>
<!-- 支持 @Async 註解 -->
<task:executor id="annotationExecutor" pool-size="20"/>
<!--配置線程池-->
<task:scheduler id="scheduler" pool-size="10" />

2.定義異步同步方法:

package com.leibao.http.async;
/**
 * User: 
 * Date: 13:48 2017/12/13
 * Email: 
 */
@Service
public class IdCardAuthAsync {
    @Autowired
    private ReportModelAuthInfoService reportModelAuthInfoService;
    @Autowired
    private ReportService reportService;
    private final static Logger logger = LoggerFactory.getLogger(IdCardAuthAsync.class);
    @Async
    public void getIdcardAuthDeal(Map map,User user,Report report) {
        logger.info("身份證認證開始");
    }
   @Scheduled(cron = "0/10 * * * * ? ")
    public void checkEmailByUndetected() {
        System.out.println("定時檢測!!");
    }
}

3.異步的使用:

[1]封裝實例:異步

@Autowired
private IdCardAuthAsync idCardAuthAsync;

[2].調用:async

idCardAuthAsync.getIdcardAuthDeal(map,updateUser,report);
相關文章
相關標籤/搜索