概述
<pre class="java" name="code">package com.gxzhuangxing.seed.modules.release.web;
public class Test {
public void testGo(){
System.out.println("---------------------------------------");
}
}
<!-- 监测预警定时器配置
xujy -->
<!-- 定时装置所要执行的任务所在的类 -->
<bean id="test" class=".....Test"/>
<!-- 引用任务描述 -->
<bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<!-- 引用任务方法所在的类 -->
<ref bean="test" />
</property>
<property name="targetMethod">
<!-- 指定任务方法名称 -->
<value>testGo</value>
</property>
</bean>
<!-- 配置触发器 -->
<bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 这里不可以直接在属性jobDetail中引用taskJob,因为他要求的是一个jobDetail类型的对象,所以我们得通过MethodInvokingJobDetailFactoryBean来转一下 -->
<property name="jobDetail">
<!-- 引用任务描述bean -->
<ref bean="methodInvokingJobDetail" />
</property>
<!-- 每天的8点到21点每隔1分钟触发,具体说明见附录 -->
<property name="cronExpression">
<!-- 秒 分 时 日 月 周 年 -->
<value>*/10 * * * * ?</value>
</property>
</bean>
<!-- 配置任务工厂, 只能有一个这种bean -->
<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 添加触发器 -->
<property name="triggers">
<list>
<ref local="testTrigger" />
<!-- ... -->
</list>
</property>
</bean>
<!-- 监测预警定时器配置 end-->
1.配了下spring-biz.xml,总是报错,
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'methodInvokingJobDetail' defined in class path resource [config/spring/spring-biz.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError:org.quartz.JobExecutionException
Caused by:
java.lang.NoClassDefFoundError: org.quartz.JobExecutionException
网上说只有spring不行,要增加quartz-all-1.6.0.jar
2.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'methodInvokingJobDetail' defined in class path resource [config/spring/spring-biz.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/collections/SetUtils
Caused by:
java.lang.NoClassDefFoundError: org/apache/commons/collections/SetUtils
这个要增加commons-collections-3.1.jar(若3.1版本还出现该错误,建议3.2.1版本)
3.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean' defined in class path resource [config/spring/spring-biz.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
Caused by:
java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'methodInvokingJobDetail' defined in class path resource [config/spring/spring-biz.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError:org.quartz.JobExecutionException
Caused by:
java.lang.NoClassDefFoundError: org.quartz.JobExecutionException
网上说只有spring不行,要增加quartz-all-1.6.0.jar
2.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'methodInvokingJobDetail' defined in class path resource [config/spring/spring-biz.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/collections/SetUtils
Caused by:
java.lang.NoClassDefFoundError: org/apache/commons/collections/SetUtils
这个要增加commons-collections-3.1.jar(若3.1版本还出现该错误,建议3.2.1版本)
3.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean' defined in class path resource [config/spring/spring-biz.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
Caused by:
java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
这个要
增加jta.jar即可
二、动态定时器
<!-- 监测预警动态定时器配置
xujy -->
<!-- 这个类用来做需要完成的业务- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序
-->
<bean id="releaseController" class="com.gxzhuangxing.seed.modules.release.web.ReleaseController" lazy-init="false" init-method="init">
<!--这个对象一定要注入,这样类才能进行管理,还有在类型要用get set方法,不然会报错。-->
<property name="scheduler" ref="startQuertz" />
</bean>
<!-- 定义调用对象和调用对象的方法,这个配置和普通定时器的配置一样的 -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类
-->
<property name="targetObject">
<ref bean="releaseController" />
</property>
<!-- 调用类中的方法
-->
<property name="targetMethod">
<value>autoPush</value>
</property>
<property
name ="concurrent"
value ="false"
/>
</bean>
<!-- 定义触发时间 ,这里必须将时间设置成无限长,因为我们要去读取数据库的时间来做为定时器的触发时间-->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask" />
</property>
<!-- cron表达式
-->
<property name="cronExpression">
<!-- cron将时间设置成无限长
-->
<value>1 0 0 1 1 ? 2099</value>
</property>
</bean>
<bean id="startQuertz" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
<!-- 监测预警动态定时器配置
end-->
java:
<pre class="java" name="code">public class ReleaseController extends BaseController {
//这个是总管理类
private Scheduler scheduler;
@Autowired
private ReleaseDateService releaseDateService;
public void init(){
CronTriggerBean trigger;
try {
trigger = (CronTriggerBean) scheduler.getTrigger("cronTrigger",Scheduler.DEFAULT_GROUP);
String originConExpression = trigger.getCronExpression();
System.out.println(originConExpression);
//以上两句代码我没有用到,这个是可以获得现在定时器调度时间,可以用来和数据库的时间比较,如果一样,就不用调用了,我省了这步
String pushTime="*/5 * * * * ?";
//从数据库获得配置时间
ReleaseDate articlePush =
releaseDateService.get();
if(articlePush!=null){
Date date = articlePush.getReleaseDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = sdf.format(date).substring(11, 19);
//因为是要每天做一次定时任务,所以我截取了字符串进行了拼接格式
String[] strTime = strDate.split(":");
pushTime =
strTime[2]+" "+strTime[1]+" "+strTime[0]+" * * ? ";
}
//以下就是重新对时间的设置,这样就可以达到动态设置效果。
System.out.println("=========================================");
trigger.setCronExpression(pushTime);
scheduler.rescheduleJob("cronTrigger", Scheduler.DEFAULT_GROUP, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
public void autoPush(){
System.out.println("-------------------------------------------");
}
public Scheduler getScheduler() {
return scheduler;
}
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
}
最后
以上就是老迟到小丸子为你收集整理的spring quartz定时器,及其动态处理方式的全部内容,希望文章能够帮你解决spring quartz定时器,及其动态处理方式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复