1、配置文件頭部添加:
xmlns:task="http://www.springframework.org/schema/task"
2、xsi:schemaLocation添加:
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
3、spring掃描配置
<context:component-scan base-package="com.watereasy.union.open.*"/>
4、任務掃描註解配置
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
5、具體示例
@Scheduled(cron = "*/10 * * * * ?")
public void task(){
try{
DYPushMQ dyPushMQ = GMSIMAGE_MQ.get();
JSONArray jsonArray = dyPushMQ.getJsonArray();
if(CollectionUtils.isNotEmpty(jsonArray)){
dyGoodsService.pushProducts(jsonArray.toJSONString());
jsonArray.clear();
}
}catch (Exception e) {
log.error("Exception detail:{}", e);
}
}
tips:
正則表達式見:https://blog.csdn.net/lapush/article/details/90262899