Spring定時任務@Scheduled

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

發佈了37 篇原創文章 · 獲贊 4 · 訪問量 9468
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章