Spring整合quartz做定時任務(wù)的示例代碼
今天我們來分享一波在spring項目使用quartz做定時任務(wù)。
首先我這里的項目已經(jīng)是一個可以跑起來的完整項目,web.xml里面的配置我就不貼出來了。
1.新建一個類ConfigConsts
我們用來放cron表達式:
??更多cron表達式??
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.新建一個QuartzHandler類來實現(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;
/**
* 描述:定時任務(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í)行定時任務(wù)
@Autowired
private Jconn jcon; // 數(shù)據(jù)庫組件
private Map<String, Object> map = new HashMap<String, Object>();
private static String startDate = "20130101";
/**
* Description:定時執(zhí)行拉取客戶主數(shù)據(jù)處理 <BR>
*
* @author dsn
* @date 2020年8月28日 下午11:57:28
* @version 1.0
* @throws Exception
*/
public void run4Customer() throws Exception {
// 定時執(zhí)行1
System.out.println("定時任務(wù)開啟----------------------------");
//這里面就可以寫代碼邏輯
}
}3.新建一個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" />
<!-- 將運行時間策略常量放入bean池 -->
<bean id="interval_customer" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="com.aowang.quartz.ConfigConsts.quartzInterval"/>
</bean>
<!-- 觸發(fā)器配置 時間指定 -->
<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'那么容器啟動就會執(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"/>
其實就是這么的簡單,完事。
到此這篇關(guān)于Spring整合quartz定時任務(wù) 的文章就介紹到這了,更多相關(guān)Spring整合quartz內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用EasyExcel模版導(dǎo)出詳細操作教程
業(yè)務(wù)中經(jīng)常需要按照一個特定的模板導(dǎo)出特定內(nèi)容,有些單元格還要求特殊的格式,所以下面這篇文章主要給大家介紹了關(guān)于Java使用EasyExcel模版導(dǎo)出的相關(guān)資料,需要的朋友可以參考下2023-10-10
永中文檔在線轉(zhuǎn)換服務(wù)Swagger調(diào)用說明
這篇文章主要為大家介紹了永中文檔在線轉(zhuǎn)換服務(wù)Swagger調(diào)用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Java關(guān)鍵字final、static使用總結(jié)
final方法不能被子類的方法覆蓋,但可以被繼承。用static修飾的代碼塊表示靜態(tài)代碼塊,當(dāng)Java虛擬機(JVM)加載類時,就會執(zhí)行該代碼塊,下面通過本文給大家分享Java關(guān)鍵字final、static使用總結(jié),感興趣的朋友一起看看吧2017-07-07
Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解
本文主要介紹了Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
java使用Dijkstra算法實現(xiàn)單源最短路徑
這篇文章主要為大家詳細介紹了java使用Dijkstra算法實現(xiàn)單源最短路徑,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01

