spring4+quartz2.2.3,定时任务弄好了,修改定时任务没折腾起,没找到合适的解决方案。
最终使用库spring-context-support 3.2.17.RELEASE + quartz 1.8.0解决,quart1.8.x版本的CronTriggerBean弄起来方便
关键代码如下:
1.maven文件
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2..RELEASE</version>
</dependency>
2.spring-quartz.xml文件
<bean id="scheduleInfoAction" class="com.aaa.controller.AutoTaskController">
<property name="scheduler" ref="schedulerFactory"/>
</bean> <bean id="schedulerJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="scheduleInfoAction"/>
<property name="targetMethod" value="execute"/>
<property name="concurrent" value="false"/>
</bean> <bean id="packetResolveTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" >
<property name="jobDetail" ref="schedulerJobDetail"/>
<property name="cronExpression">
<value>0/5 * * * * ?</value>
</property>
</bean>
<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="packetResolveTrigger"/>
</list>
</property>
</bean>
3.java代码
package com.aaa.controller; @Api("定时任务接口")
@Controller
@RequestMapping("/task")
public class AutoTaskController {
private Scheduler scheduler;
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
//定时任务执行
protected void execute() throws Exception { System.out.println("定时任务执行,解析:"+new Date()); }
//修改
public void changeCronTime(String ex) throws Exception{
//获取定时器
try {
CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger("packetResolveTrigger", Scheduler.DEFAULT_GROUP);
System.out.println(trigger.getCronExpression());
trigger.setCronExpression("0/1 * * * * ?");
scheduler.rescheduleJob("packetResolveTrigger", Scheduler.DEFAULT_GROUP, trigger);
} catch (SchedulerException e1) {
e1.printStackTrace();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
} 附录:附上案例:(注意xml的xmlns等配置)
<?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
default-lazy-init="false">
<bean id="job" class="com.hanboard.battery.controller.ScheduleTask">
<property name="scheduler" ref="schedulerFactory"/>
</bean>
<!--========================================jobs list配置==============================================================-->
<bean id="jobtask_packetResolve" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="job"/>
<property name="targetMethod" value="packetResolve"/>
<property name="concurrent" value="false"/>
</bean>
<bean id="jobtask_healthAnalysis" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="job"/>
<property name="targetMethod" value="healthAnalysis"/>
<property name="concurrent" value="false"/>
</bean> <!--========================================trigger list配置=============================================================-->
<bean id="packetResolveTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" >
<property name="jobDetail" ref="jobtask_packetResolve"/>
<property name="cronExpression">
<value>/ * * * * ?</value>
</property>
</bean>
<bean id="healthAnalysisTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" >
<property name="jobDetail" ref="jobtask_healthAnalysis"/>
<property name="cronExpression">
<value>/ * * * * ?</value>
</property>
</bean> <!--========================================triggers factory reference配置=================================================-->
<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="packetResolveTrigger"/>
<ref local="healthAnalysisTrigger"/>
</list>
</property>
</bean> </beans>