Spring整合quartz做定時(shí)任務(wù)的示例代碼
今天我們來(lái)分享一波在spring項(xiàng)目使用quartz做定時(shí)任務(wù)。
首先我這里的項(xiàng)目已經(jīng)是一個(gè)可以跑起來(lái)的完整項(xiàng)目,web.xml里面的配置我就不貼出來(lái)了。
1.新建一個(gè)類ConfigConsts
我們用來(lái)放cron表達(dá)式:
??更多cron表達(dá)式??
package com.aowang.quartz; public abstract class ConfigConsts { /** 30分鐘執(zhí)行一次 */ public static final String quartzInterval = "0 0/30 * * * ?"; /** 每天凌晨1:30分執(zhí)行*/ public static final String quartzCustomerInterval = "0 30 1 * * ?"; /** 每天凌晨1:00分執(zhí)行*/ public static final String quartzMaterialInterval = "0 0 1 * * ?"; /** 每天凌晨2:00分執(zhí)行*/ public static final String quartzSupplierInterval = "0 0 2 * * ?"; }
2.新建一個(gè)QuartzHandler類來(lái)實(shí)現(xiàn)我們的代碼邏輯
package com.aowang.quartz; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.http.HttpRequest; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.aowang.framework.daoComp.Jconn; import com.aowang.framework.daoComp.jComp; import com.aowang.utils.APIUtils; import com.aowang.utils.Constants; import com.aowang.utils.DateUtil; import com.aowang.utils.Utils; import com.aowang.utils.http.HttpClientResult; import com.aowang.utils.http.HttpClientUtils; import net.sf.json.JSONArray; /** * 描述:定時(shí)任務(wù)調(diào)度 * * @author dsn * @date 2020年8月28日 下午2:57:41 */ public class QuartzHandler { private static final Logger logger = LoggerFactory.getLogger(QuartzHandler.class); private static boolean isFirst = true;// 第一次執(zhí)行定時(shí)任務(wù) @Autowired private Jconn jcon; // 數(shù)據(jù)庫(kù)組件 private Map<String, Object> map = new HashMap<String, Object>(); private static String startDate = "20130101"; /** * Description:定時(shí)執(zhí)行拉取客戶主數(shù)據(jù)處理 <BR> * * @author dsn * @date 2020年8月28日 下午11:57:28 * @version 1.0 * @throws Exception */ public void run4Customer() throws Exception { // 定時(shí)執(zhí)行1 System.out.println("定時(shí)任務(wù)開啟----------------------------"); //這里面就可以寫代碼邏輯 } }
3.新建一個(gè)application-quartz.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 要調(diào)用的工作類 --> <bean id="mainClass" class="com.aowang.quartz.QuartzHandler"> </bean> <!-- 任務(wù)配置列表 --> <bean id="task_customer" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 指定任務(wù)類 --> <property name="targetObject" ref="mainClass" /> <!-- 指定任務(wù)執(zhí)行的方法 --> <property name="targetMethod" value="run4Customer" /> <!-- 將運(yùn)行時(shí)間策略常量放入bean池 --> <bean id="interval_customer" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <property name="staticField" value="com.aowang.quartz.ConfigConsts.quartzInterval"/> </bean> <!-- 觸發(fā)器配置 時(shí)間指定 --> <bean id="trigger_customer" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="task_customer" /> <property name="cronExpression" ref="interval_customer" /> <!-- 總管理類 如果將lazy-init='false'那么容器啟動(dòng)就會(huì)執(zhí)行調(diào)度程序 --> <bean lazy-init="true" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!-- 觸發(fā)器列表 --> <ref bean="trigger_customer" /> </list> </property> </beans>
4.在applicationContent.xml中引入第3步新建的xml
<import resource="application-quartz.xml"/>
其實(shí)就是這么的簡(jiǎn)單,完事。
到此這篇關(guān)于Spring整合quartz定時(shí)任務(wù) 的文章就介紹到這了,更多相關(guān)Spring整合quartz內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java獲取服務(wù)器IP及端口的方法實(shí)例分析
這篇文章主要介紹了Java獲取服務(wù)器IP及端口的方法,結(jié)合實(shí)例形式分析了java針對(duì)客戶端及服務(wù)器端各種常見的信息操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-12-12Java使用EasyExcel模版導(dǎo)出詳細(xì)操作教程
業(yè)務(wù)中經(jīng)常需要按照一個(gè)特定的模板導(dǎo)出特定內(nèi)容,有些單元格還要求特殊的格式,所以下面這篇文章主要給大家介紹了關(guān)于Java使用EasyExcel模版導(dǎo)出的相關(guān)資料,需要的朋友可以參考下2023-10-10springboot-assembly自定義打包全過(guò)程
這篇文章主要介紹了springboot-assembly自定義打包全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06永中文檔在線轉(zhuǎn)換服務(wù)Swagger調(diào)用說(shuō)明
這篇文章主要為大家介紹了永中文檔在線轉(zhuǎn)換服務(wù)Swagger調(diào)用說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Java關(guān)鍵字final、static使用總結(jié)
final方法不能被子類的方法覆蓋,但可以被繼承。用static修飾的代碼塊表示靜態(tài)代碼塊,當(dāng)Java虛擬機(jī)(JVM)加載類時(shí),就會(huì)執(zhí)行該代碼塊,下面通過(guò)本文給大家分享Java關(guān)鍵字final、static使用總結(jié),感興趣的朋友一起看看吧2017-07-07Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解
本文主要介紹了Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04java使用Dijkstra算法實(shí)現(xiàn)單源最短路徑
這篇文章主要為大家詳細(xì)介紹了java使用Dijkstra算法實(shí)現(xiàn)單源最短路徑,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01