Spring整合Quartz开发代码实例
作者:海之浪子 发布时间:2022-03-12 16:37:26
标签:Spring,整合,Quartz,开发
我们使用Spring整合Quartz开发,本实例采用数据库模式的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">
<!--加载数据库连接的配置文件-->
<!--<context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
<!-- c3p0:数据源配置 -->
<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()+"执行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();
//从数据库中获取相应的job及调度信息
//JobDetail jobDetail = scheduler.getJobDetail(new JobKey("trigger1", "trigger1"));
//resumeJob(jobDetail.getKey().getName(), jobDetail.getKey().getGroup());
//添加job执行
addJob("trigger1", "trigger1", "job1", "job2", "0/20 * * * * ?", SimpleJob.class, new HashMap<>());
Thread.sleep(60 * 1000);
//重新设置调度时间
System.out.println("重新设置调度时间");
rescheduleJob("trigger1","trigger1","0/10 * * * * ?");
Thread.sleep(60 * 1000);
//暂停调度
System.out.println("暂停调度");
pauseJob("trigger1","trigger1");
Thread.sleep(60 * 1000);
System.out.println("恢复调度");
resumeJob("trigger1","trigger1");
Thread.sleep(60 * 1000);
System.out.println("删除调度");
removeJob("trigger1","trigger1");
Thread.sleep(60 * 1000);
System.out.println(scheduler);
}
/**
* 添加job执行
*
* @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;
}
/**
* 重新设置job执行
* @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正常配置信息,然后点击运行即可。
本实例中当运行的任务在暂停的情况下,一旦重新恢复,会将暂停期间的任务运行如图:
源码链接: https://github.com/albert-liu435/springquartz
来源:https://www.cnblogs.com/haizhilangzi/p/12218767.html


猜你喜欢
- Java中的try-catch-finally异常处理一、异常处理异常(Exception):是在运行发生的不正常情况。原始异常处理:if(
- 目录1.C 语言包含的数据类型2.C语言的基本数据类型3.示例代码1.C 语言包含的数据类型如下图所示:2.C语言的基本数据类型short、
- 一、前言二、案例需求1.编写login.html登录页面,username&password两个输入框2.使用Druid数据库连接池
- 前言做过java web开发的小伙伴大多数时候都需要链接数据库,这个时候就需要配置数据库引擎DriverClassName参数,这样我们的j
- 最近在配置OpenCV的时候,由于使用的是VS2019,结果找不到Microsoft.Cpp.X64.user这个文件。导致每次新建项目都得
- 通常我们用惯的ListView每一项的布局都是相同的,只是控件所绑定的数据不同。但单单只是如此并不能满
- 背景公司开发框架增加了web系统license授权证书校验模块,实行一台机器一个授权证书,初步方案是增加 * 针对全局请求进行拦截校验,评估
- 本文实例为大家分享了Java使用组件编写窗口下载网上文件的具体代码,供大家参考,具体内容如下如图实现代码:package com.rain.
- java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致。下面会讲解Spring中自定义注解的简单流程,其中会
- 在java中,static是一个修饰符,用于修饰类的成员方法、类的成员变量,另外可以编写static代码块来优化程序性能;被static关键
- 本文实例为大家分享了java常用工具类的具体代码,供大家参考,具体内容如下Random随机数工具类package com.jarvis.ba
- mybatis自带对枚举的处理类org.apache.ibatis.type.EnumOrdinalTypeHandler<E>
- starter起步依赖starter起步依赖是springboot一种非常重要的机制,它打包了某些场景下需要用到依赖,将其统一集成到star
- 使用HTTPclient访问url获得数据最近项目上有个小功能需要调用第三方的http接口取数据,用到了HTTPclient,算是做个笔记吧
- 通过java代码规范来优化程序,优化内存使用情况,防止内存泄露可供程序利用的资源(内存、CPU时间、网络带宽等)是有限的,优化的目的就是让程
- 目录基本查询延迟查询属性类型筛选复合from子句多级排序分组联合查询-join合并-zip()分区(分页)并行linq取消长时间运行的并行l
- 本文实例为大家分享了java动态模拟时钟的具体代码,供大家参考,具体内容如下应用名称:java动态模拟时钟用到的知识:javaGUI,jav
- 前言本文基于itext7实现pdf加水印和合并的操作。实际上在我们实际项目应用中,对于pdf的操作也是比较常见的,我上一个项目中就有将结果转
- android读取assets文件下的内容,一般都是使用getAsset.open()方法,并将文件的路径作为参数传入,而当我们解析一个目录
- 一、前言最近在看android fragment与Activity进行数据传递的部分,看到了接口回调的内容,今天来总结一下。二、回调的含义和