在项目中增加task定时任务
生活随笔
收集整理的這篇文章主要介紹了
在项目中增加task定时任务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.新建task類
package net.qdedu.task;import lombok.extern.slf4j.Slf4j; import net.qdedu.activity.service.ActivityBaseService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;@Service @Slf4j public class WeeHoursTask {@Autowiredprivate ActivityBaseService activityBaseService;/***檢測活動是否到開始時間*/public void collectKnowledgeAbilityData() {log.warn("start");activityBaseService.batchUpdateStartStatus();}/*** 檢測活動是否到結束時間*/public void collectWorkGradeData() {log.warn("stop");activityBaseService.batchUpdateStopStatus();}}?
2.增加配置文件spring-task.xml
定時時間了解推薦:https://blog.csdn.net/u012843873/article/details/72957965
我們使用的是spring內置的定時任務
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd"default-lazy-init="true"><context:annotation-config /><!--spring掃描注解的配置--><context:component-scan base-package="net.qdedu.task" />//新建項目包的掃描路徑<!-- 每隔3分鐘執行一次 --><task:scheduled-tasks><!-- 檢測活動是否到結束時間 每50秒執行一次 ref:類的名稱 method:對應類的方法--><task:scheduled ref="weeHoursTask" method="collectWorkGradeData" initial-delay="50000" fixed-delay="50000"/> <!-- 檢測活動是否到開始時間 每50秒執行一次 ref:類的名稱 method:對應類的方法--><task:scheduled ref="weeHoursTask" method="collectKnowledgeAbilityData" initial-delay="50000" fixed-delay="50000"/></task:scheduled-tasks></beans>?3.在spring-context.xml增加掃描spring-task.xml
?
轉載于:https://www.cnblogs.com/houpengwei/p/10905520.html
總結
以上是生活随笔為你收集整理的在项目中增加task定时任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode Implement T
- 下一篇: 灯光问题