Spring整合Quartz開發(fā)代碼實(shí)例
我們使用Spring整合Quartz開發(fā),本實(shí)例采用數(shù)據(jù)庫模式的demo。
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"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!--加載數(shù)據(jù)庫連接的配置文件-->
<!--<context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
<!-- c3p0:數(shù)據(jù)源配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/quartz?Unicode=true&characterEncoding=UTF-8"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<property name="initialPoolSize" value="3"/>
<property name="minPoolSize" value="2"/>
<property name="maxPoolSize" value="10"/>
<property name="maxIdleTime" value="60"/>
<property name="acquireRetryDelay" value="1000"/>
<property name="acquireRetryAttempts" value="10"/>
<property name="preferredTestQuery" value="SELECT 1"/>
</bean>
<bean id="quartzScheduler" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="quartz.properties"></property>
<!-- <property name="triggers"></property>-->
</bean>
</beans>
public class SimpleJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println(new Date()+"執(zhí)行SimpleJob");
}
}
public class ApplicationContextTest {
public static Scheduler scheduler;
public static void main(String[] args) throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext-trigger.xml");
scheduler = (Scheduler) applicationContext.getBean("quartzScheduler");
//SimpleJob simpleJob = new SimpleJob();
scheduler.start();
//從數(shù)據(jù)庫中獲取相應(yīng)的job及調(diào)度信息
//JobDetail jobDetail = scheduler.getJobDetail(new JobKey("trigger1", "trigger1"));
//resumeJob(jobDetail.getKey().getName(), jobDetail.getKey().getGroup());
//添加job執(zhí)行
addJob("trigger1", "trigger1", "job1", "job2", "0/20 * * * * ?", SimpleJob.class, new HashMap<>());
Thread.sleep(60 * 1000);
//重新設(shè)置調(diào)度時(shí)間
System.out.println("重新設(shè)置調(diào)度時(shí)間");
rescheduleJob("trigger1","trigger1","0/10 * * * * ?");
Thread.sleep(60 * 1000);
//暫停調(diào)度
System.out.println("暫停調(diào)度");
pauseJob("trigger1","trigger1");
Thread.sleep(60 * 1000);
System.out.println("恢復(fù)調(diào)度");
resumeJob("trigger1","trigger1");
Thread.sleep(60 * 1000);
System.out.println("刪除調(diào)度");
removeJob("trigger1","trigger1");
Thread.sleep(60 * 1000);
System.out.println(scheduler);
}
/**
* 添加job執(zhí)行
*
* @param triggerKeyName
* @param triggerKeyGroup
* @param jobName
* @param jobGroup
* @param cronExpression
* @param jobClass
* @param jobData
* @return
* @throws Exception
*/
public static boolean addJob(String triggerKeyName, String triggerKeyGroup, String jobName, String jobGroup, String cronExpression,
Class<? extends Job> jobClass, Map<String, Object> jobData) throws Exception {
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(triggerKeyName, triggerKeyGroup).build();
Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).withIdentity(triggerKeyName, triggerKeyGroup).build();
if (jobData != null && jobData.size() > 0) {
JobDataMap jobDataMap = jobDetail.getJobDataMap();
jobDataMap.putAll(jobData); // JobExecutionContext context.getMergedJobDataMap().get("mailGuid");
}
scheduler.scheduleJob(jobDetail, trigger);
// if (!scheduler.isShutdown()) {
// scheduler.start();
// }
return true;
}
/**
* 重新設(shè)置job執(zhí)行
* @param triggerKeyName
* @param triggerKeyGroup
* @param cronExpression
* @return
* @throws SchedulerException
*/
public static boolean rescheduleJob(String triggerKeyName, String triggerKeyGroup, String cronExpression) throws SchedulerException {
TriggerKey triggerKey = TriggerKey.triggerKey(triggerKeyName, triggerKeyGroup);
if (scheduler.checkExists(triggerKey)) {
Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).withIdentity(triggerKey).build();
scheduler.rescheduleJob(triggerKey, trigger);
}
return true;
}
/**
* 刪除job
* @param triggerKeyName
* @param triggerKeyGroup
* @return
* @throws SchedulerException
*/
public static boolean removeJob(String triggerKeyName, String triggerKeyGroup) throws SchedulerException {
// TriggerKey : name + group
TriggerKey triggerKey = TriggerKey.triggerKey(triggerKeyName, triggerKeyGroup);
boolean result = false;
if (scheduler.checkExists(triggerKey)) {
result = scheduler.unscheduleJob(triggerKey);
}
return result;
}
/**
* 暫停job
* @param triggerKeyName
* @param triggerKeyGroup
* @return
* @throws SchedulerException
*/
public static boolean pauseJob(String triggerKeyName, String triggerKeyGroup) throws SchedulerException {
// TriggerKey : name + group
TriggerKey triggerKey = TriggerKey.triggerKey(triggerKeyName, triggerKeyGroup);
boolean result = false;
if (scheduler.checkExists(triggerKey)) {
scheduler.pauseTrigger(triggerKey);
result = true;
} else {
}
return result;
}
/**
* 重啟job
* @param triggerKeyName
* @param triggerKeyGroup
* @return
* @throws SchedulerException
*/
public static boolean resumeJob(String triggerKeyName, String triggerKeyGroup) throws SchedulerException {
TriggerKey triggerKey = TriggerKey.triggerKey(triggerKeyName, triggerKeyGroup);
boolean result = false;
if (scheduler.checkExists(triggerKey)) {
scheduler.resumeTrigger(triggerKey);
result = true;
} else {
}
return result;
}
}
quart.properties正常配置信息,然后點(diǎn)擊運(yùn)行即可。
本實(shí)例中當(dāng)運(yùn)行的任務(wù)在暫停的情況下,一旦重新恢復(fù),會(huì)將暫停期間的任務(wù)運(yùn)行如圖:

源碼鏈接: https://github.com/albert-liu435/springquartz
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring整合Quartz框架過程詳解
- 淺談SpringBoot集成Quartz動(dòng)態(tài)定時(shí)任務(wù)
- Spring整合Quartz定時(shí)任務(wù)并在集群、分布式系統(tǒng)中的應(yīng)用
- SpringBoot與Quartz集成實(shí)現(xiàn)分布式定時(shí)任務(wù)集群的代碼實(shí)例
- 詳解Quartz 與 Spring框架集成的三種方式
- springboot Quartz動(dòng)態(tài)修改cron表達(dá)式的方法
- Springboot整個(gè)Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)的示例代碼
- Quartz+Spring Boot實(shí)現(xiàn)動(dòng)態(tài)管理定時(shí)任務(wù)
相關(guān)文章
Spring中的之啟動(dòng)過程obtainFreshBeanFactory詳解
這篇文章主要介紹了Spring中的之啟動(dòng)過程obtainFreshBeanFactory詳解,在refresh時(shí),prepareRefresh后,馬上就調(diào)用了obtainFreshBeanFactory創(chuàng)建beanFactory以及掃描bean信息(beanDefinition),并通過BeanDefinitionRegistry注冊(cè)到容器中,需要的朋友可以參考下2024-02-02
Java日期時(shí)間字符串和毫秒相互轉(zhuǎn)換的方法
這篇文章主要為大家詳細(xì)介紹了Java日期時(shí)間字符串和毫秒相互轉(zhuǎn)換的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Java實(shí)現(xiàn)視頻時(shí)間維度剪切的工具類
這篇文章主要為大家詳細(xì)介紹了將視頻按照時(shí)間維度進(jìn)行剪切的Java工具類,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
Spring Cloud Gateway全局異常處理的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Cloud Gateway全局異常處理的相關(guān)資料,需要的朋友可以參考下2018-10-10
Java中的CyclicBarrier循環(huán)柵欄解析
這篇文章主要介紹了Java中的CyclicBarrier循環(huán)柵欄解析,從字面上的意思可以知道,這個(gè)類的中文意思是"循環(huán)柵欄",大概的意思就是一個(gè)可循環(huán)利用的屏障,它的作用就是會(huì)讓所有線程都等待完成后才會(huì)繼續(xù)下一步行動(dòng),需要的朋友可以參考下2023-12-12
springboot集成activemq的實(shí)例代碼
本篇文章主要介紹了springboot集成activemq的實(shí)例代碼,詳細(xì)的介紹了ActiveMQ和Spring-Boot 集成 ActiveMQ,有興趣的可以了解下。2017-05-05

