Quartz实现定时的DEMO,可修改定时策略
程序员文章站
2022-03-31 23:41:12
...
来源于网络 http://www.blogjava.net/xiaodaoxiaodao/articles/103437.html
给出完整的类和配置文件,除了一般是Spring 包外,另外需要下一个包spring-context-1.15.jar包,可在http://repo1.maven.org/maven2/ 找。
下面的内容拼到一个webProject里面,发布起来就可以看到效果。
1.Action
public class ScheduleInfoAction {
public void reScheduleJob() throws SchedulerException, ParseException {
// 运行时可通过动态注入的scheduler得到trigger
CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger(
"cronTrigger", Scheduler.DEFAULT_GROUP);
String dbCronExpression = getCronExpressionFromDB();
String originConExpression = trigger.getCronExpression();
// 判断从DB中取得的任务时间(dbCronExpression)和现在的quartz线程中的任务时间(originConExpression)是否相等
// 如果相等,则表示用户并没有重新设定数据库中的任务时间,这种情况不需要重新rescheduleJob
if(!originConExpression.equalsIgnoreCase(dbCronExpression)){
trigger.setCronExpression(dbCronExpression); //执行更新操作
scheduler.rescheduleJob("cronTrigger", Scheduler.DEFAULT_GROUP, trigger);
}
// 下面是具体的job内容,可自行设置
executeJobDetail();
}
private String getCronExpressionFromDB(){
String sql="from ScheduleInfo scheduleInfo where 1=1 ";
sql=sql+" and scheduleInfo.infoId = '"+"1" + "'";
List scheduleList = scheduleInfoManager.queryScheduleInListBySql(sql);
ScheduleInfo scheduleInfo = (ScheduleInfo)scheduleList.get(0);
String dbCronExpression = scheduleInfo.getCronExpression();
return dbCronExpression;
}
private void executeJobDetail(){
System.out.println("schedule action time:"+System.currentTimeMillis());
}
private Scheduler scheduler;
// 设值注入,通过setter方法传入被调用者的实例scheduler
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
private ScheduleInfoManager scheduleInfoManager;
// 设值注入,通过setter方法传入被调用者的实例scheduleInfoManager
public void setScheduleInfoManager(ScheduleInfoManager scheduleInfoManager){
this.scheduleInfoManager = scheduleInfoManager;
}
}
2.Bean
public class ScheduleInfo {
String cronExpression="0/10 * * * * ?";
public void setCronExpression(String cronExpression) {
this.cronExpression = cronExpression;
}
public String getCronExpression(){
return this.cronExpression ;
}
}
3.Service
public class ScheduleInfoManager {
long i =System.currentTimeMillis();
int j=0;
List lis=new ArrayList();
ScheduleInfo s=new ScheduleInfo();
public List queryScheduleInListBySql(String sql){
if(System.currentTimeMillis()-i<30*1000 ){
}else if(j==0){
j++;
s.setCronExpression("0 * * * * ?");
System.out.println("30秒到,更新参数:0 * * * * ?");
}
lis.add(s);
return lis;
}
}
4.Init
public class InitializingCronTrigger extends CronTriggerBean implements Serializable {
private ScheduleInfoManager scheduleInfoManager;
// 设值注入,通过setter方法传入被调用者的实例scheduleInfoManager
public void setScheduleInfoManager(ScheduleInfoManager scheduleInfoManager){
this.scheduleInfoManager = scheduleInfoManager;
// 因为在getCronExpressionFromDB使用到了scheduleInfoManager,所以
// 必须上一行代码设置scheduleInfoManager后进行getCronExpressionFromDB
String cronExpression = getCronExpressionFromDB (); // ①
// 因为extendsCronTriggerBean ,此处调用父类方法初始化cronExpression
try {
setCronExpression (cronExpression);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // ②
}
private String getCronExpressionFromDB(){
String sql="from ScheduleInfo scheduleInfo where 1=1 ";
sql=sql+" and scheduleInfo.infoId = '"+"1" + "'";
List scheduleList = scheduleInfoManager.queryScheduleInListBySql(sql);
ScheduleInfo scheduleInfo = (ScheduleInfo)scheduleList.get(0);
String dbCronExpression = scheduleInfo.getCronExpression();
System.out.println("inital:"+dbCronExpression);
return dbCronExpression;
}
5.applicationcontext.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-2.0.xsd">
<bean id="scheduleInfoManager" class="com.huateng.quartz.ScheduleInfoManager">
</bean>
<bean id="scheduleInfoAction" class="com.huateng.quartz.ScheduleInfoAction">
<property name="scheduler" ref="schedulerFactory"/>
<property name="scheduleInfoManager" ref="scheduleInfoManager"/>
</bean>
<bean id="schedulerJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="scheduleInfoAction"/>
<property name="targetMethod" value="reScheduleJob"/>
<property name="concurrent" value="false"/>
</bean>
<bean id="cronTrigger" class="com.huateng.trigger.InitializingCronTrigger">
<property name="jobDetail" ref="schedulerJobDetail"/>
<!--<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>-->
<property name="scheduleInfoManager" ref="scheduleInfoManager"/>
</bean>
<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="cronTrigger"/>
</list>
</property>
</bean>
</beans>
给出完整的类和配置文件,除了一般是Spring 包外,另外需要下一个包spring-context-1.15.jar包,可在http://repo1.maven.org/maven2/ 找。
下面的内容拼到一个webProject里面,发布起来就可以看到效果。
1.Action
public class ScheduleInfoAction {
public void reScheduleJob() throws SchedulerException, ParseException {
// 运行时可通过动态注入的scheduler得到trigger
CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger(
"cronTrigger", Scheduler.DEFAULT_GROUP);
String dbCronExpression = getCronExpressionFromDB();
String originConExpression = trigger.getCronExpression();
// 判断从DB中取得的任务时间(dbCronExpression)和现在的quartz线程中的任务时间(originConExpression)是否相等
// 如果相等,则表示用户并没有重新设定数据库中的任务时间,这种情况不需要重新rescheduleJob
if(!originConExpression.equalsIgnoreCase(dbCronExpression)){
trigger.setCronExpression(dbCronExpression); //执行更新操作
scheduler.rescheduleJob("cronTrigger", Scheduler.DEFAULT_GROUP, trigger);
}
// 下面是具体的job内容,可自行设置
executeJobDetail();
}
private String getCronExpressionFromDB(){
String sql="from ScheduleInfo scheduleInfo where 1=1 ";
sql=sql+" and scheduleInfo.infoId = '"+"1" + "'";
List scheduleList = scheduleInfoManager.queryScheduleInListBySql(sql);
ScheduleInfo scheduleInfo = (ScheduleInfo)scheduleList.get(0);
String dbCronExpression = scheduleInfo.getCronExpression();
return dbCronExpression;
}
private void executeJobDetail(){
System.out.println("schedule action time:"+System.currentTimeMillis());
}
private Scheduler scheduler;
// 设值注入,通过setter方法传入被调用者的实例scheduler
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
private ScheduleInfoManager scheduleInfoManager;
// 设值注入,通过setter方法传入被调用者的实例scheduleInfoManager
public void setScheduleInfoManager(ScheduleInfoManager scheduleInfoManager){
this.scheduleInfoManager = scheduleInfoManager;
}
}
2.Bean
public class ScheduleInfo {
String cronExpression="0/10 * * * * ?";
public void setCronExpression(String cronExpression) {
this.cronExpression = cronExpression;
}
public String getCronExpression(){
return this.cronExpression ;
}
}
3.Service
public class ScheduleInfoManager {
long i =System.currentTimeMillis();
int j=0;
List lis=new ArrayList();
ScheduleInfo s=new ScheduleInfo();
public List queryScheduleInListBySql(String sql){
if(System.currentTimeMillis()-i<30*1000 ){
}else if(j==0){
j++;
s.setCronExpression("0 * * * * ?");
System.out.println("30秒到,更新参数:0 * * * * ?");
}
lis.add(s);
return lis;
}
}
4.Init
public class InitializingCronTrigger extends CronTriggerBean implements Serializable {
private ScheduleInfoManager scheduleInfoManager;
// 设值注入,通过setter方法传入被调用者的实例scheduleInfoManager
public void setScheduleInfoManager(ScheduleInfoManager scheduleInfoManager){
this.scheduleInfoManager = scheduleInfoManager;
// 因为在getCronExpressionFromDB使用到了scheduleInfoManager,所以
// 必须上一行代码设置scheduleInfoManager后进行getCronExpressionFromDB
String cronExpression = getCronExpressionFromDB (); // ①
// 因为extendsCronTriggerBean ,此处调用父类方法初始化cronExpression
try {
setCronExpression (cronExpression);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // ②
}
private String getCronExpressionFromDB(){
String sql="from ScheduleInfo scheduleInfo where 1=1 ";
sql=sql+" and scheduleInfo.infoId = '"+"1" + "'";
List scheduleList = scheduleInfoManager.queryScheduleInListBySql(sql);
ScheduleInfo scheduleInfo = (ScheduleInfo)scheduleList.get(0);
String dbCronExpression = scheduleInfo.getCronExpression();
System.out.println("inital:"+dbCronExpression);
return dbCronExpression;
}
5.applicationcontext.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-2.0.xsd">
<bean id="scheduleInfoManager" class="com.huateng.quartz.ScheduleInfoManager">
</bean>
<bean id="scheduleInfoAction" class="com.huateng.quartz.ScheduleInfoAction">
<property name="scheduler" ref="schedulerFactory"/>
<property name="scheduleInfoManager" ref="scheduleInfoManager"/>
</bean>
<bean id="schedulerJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="scheduleInfoAction"/>
<property name="targetMethod" value="reScheduleJob"/>
<property name="concurrent" value="false"/>
</bean>
<bean id="cronTrigger" class="com.huateng.trigger.InitializingCronTrigger">
<property name="jobDetail" ref="schedulerJobDetail"/>
<!--<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>-->
<property name="scheduleInfoManager" ref="scheduleInfoManager"/>
</bean>
<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="cronTrigger"/>
</list>
</property>
</bean>
</beans>
上一篇: win10系统如何锁定桌面图标位置
推荐阅读
-
基于spring+quartz的分布式定时任务框架实现
-
Spring整合Quartz实现一个简单的定时任务
-
SpringBoot与Quartz集成实现分布式定时任务集群的代码实例
-
spring boot整合quartz实现多个定时任务的方法
-
spring boot整合quartz实现多个定时任务的方法
-
C#中Quartz的简单易懂定时任务实现
-
Spring Boot+Quartz实现一个实时管理的定时任务
-
简单的spring mvc 定时任务(quartz 带Demo)
-
Spring task quartz 定时任务的几种实现
-
python定时发暖心的微信消息给心爱的她,修改时间可以实现轰炸效果!