SSM框架配置定時任務

在springmvc.xml配置文件中,添加如下配置:

在bean下

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

然後在 xsi下添加:

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

開啓定時任務掃描:

<task:annotation-driven/>
<context:component-scan base-package="com.yyg.timer"/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<task:scheduler id="myScheduler" pool-size="10"/>

Java代碼:

@Component
public class InsertVideoInfoJob {

    @Autowired
    private WXVideoReadyDao readyVideoDao;

    @Scheduled(cron = "0 5 0 ? * TUE")
    public void insertVideoInfo () {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //獲取未推送的最早的一條記錄
        WXVideoReadyModel param = new WXVideoReadyModel();
        List<WXVideoReadyModel> list = readyVideoDao.select(param);
        System.out.println("已獲取到微推送視頻");
        if(list.size() > 0){
            for (WXVideoReadyModel model : list) {
                System.out.println("視頻信息已插入視頻表");
                //更新視頻預備表---視頻已推送
                WXVideoReadyModel alreadyPut = model;
                alreadyPut.setIsInput(1);
                readyVideoDao.update(alreadyPut);
                System.out.println("更新視頻預備表");
            }

        }
    }
}

到此爲止就好了。


其實開始的時候我在springmvc.xml文件下 並沒有加 

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<task:scheduler id="myScheduler" pool-size="10"/>

 所以啓動會報錯但是定時任務依舊會執行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章