标签:property 文件 try ext date() job info exce date
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.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.17.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(); } } }
标签:property 文件 try ext date() job info exce date
原文地址:http://www.cnblogs.com/xiaoliu66007/p/7410767.html