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 ArrayList與LinkedList使用方法詳解
Java中容器對(duì)象主要用來(lái)存儲(chǔ)其他對(duì)象,根據(jù)實(shí)現(xiàn)原理不同,主要有3類常用的容器對(duì)象:ArrayList使用數(shù)組結(jié)構(gòu)存儲(chǔ)容器中的元素、LinkedList使用鏈表結(jié)構(gòu)存儲(chǔ)容器中的元素2022-11-11
springboot serverEndpoint導(dǎo)致@resource注解不生效
在SpringBoot中,@Resource注解用于注入依賴,本文主要介紹了springboot serverEndpoint導(dǎo)致@resource注解不生效,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
詳解Spring Boot加載properties和yml配置文件
本篇文章主要介紹了詳解Spring Boot加載properties和yml配置文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
MyBatis-Plus標(biāo)簽@TableField之fill自動(dòng)填充方式
這篇文章主要介紹了MyBatis-Plus標(biāo)簽@TableField之fill自動(dòng)填充方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
如何使用spring ResponseEntity處理http響應(yīng)
這篇文章主要介紹了如何使用spring ResponseEntity處理http響應(yīng)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java高級(jí)特性之反射機(jī)制實(shí)例詳解
這篇文章主要介紹了Java高級(jí)特性之反射機(jī)制,結(jié)合實(shí)例形式詳細(xì)分析了Java反射機(jī)制原理、功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-08-08
關(guān)于Cannot?resolve?com.microsoft.sqlserver:sqljdbc4:4.0報(bào)錯(cuò)問題解
這篇文章主要給大家介紹了關(guān)于Cannot?resolve?com.microsoft.sqlserver:sqljdbc4:4.0報(bào)錯(cuò)問題的解決辦法,這個(gè)是在pom文件中添加依賴出現(xiàn)報(bào)錯(cuò)問題,需要的朋友可以參考下2024-02-02

