spring环境:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/jee
http://www.springframework.org/schema/jee/spring-jee-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- *******************************cronJob start******************************* -->
<bean id="cronJobDetail"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- 指定具体的job类 -->
<property name="jobClass" value="com.chenjun.quartz_1.springenv.CronJobDetail" />
<!-- 指定job的名称 -->
<property name="name" value="CRON_JOB" />
</bean>
<bean name="cronJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="cronJobDetail"></property>
<!-- 每1秒执行一次 -->
<property name="cronExpression" value="0/1 * * * * ?" />
</bean>
<!-- *******************************cronJob end******************************* -->
<bean id="schedulerFactory"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronJobTrigger" />
</list>
</property>
</bean>
</beans>
JOB类
package com.chenjun.quartz_1.springenv;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class SimpleJob extends QuartzJobBean
{
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException
{
String message = context.getJobDetail().getJobDataMap().getString("message");
System.out.println(message);
}
}
JobDetail类
package com.chenjun.quartz_1.springenv;
import java.text.ParseException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.StatefulJob;
import org.quartz.impl.StdScheduler;
public class CronJobDetail implements StatefulJob
{
/*** 日志 */
private static final Log LOG = LogFactory.getLog(StatefulJob.class);
public void execute(JobExecutionContext context) throws JobExecutionException
{
StdScheduler stdScheduler = (StdScheduler)context.getScheduler();
CronTrigger cronTrigger = (CronTrigger)context.getTrigger();
System.out.println("JOB执行.."+cronTrigger.getCronExpression());
CronTrigger trigger = null;
try
{
trigger = (CronTrigger) stdScheduler.getTrigger("cronJobTrigger", Scheduler.DEFAULT_GROUP);
CronTrigger newTriggerIns = new CronTrigger();
newTriggerIns.setJobName(cronTrigger.getJobName());
newTriggerIns.setName("abc");
//模拟从数据库里面查出的时间表达式
newTriggerIns.setCronExpression(new CronExpression("0/5 * * * * ?"));
stdScheduler.rescheduleJob("cronJobTrigger", null, newTriggerIns);
}
catch (SchedulerException e)
{
e.printStackTrace();
}
catch (ParseException e)
{
e.printStackTrace();
}
}
}
启动类:
package com.chenjun.quartz_1.springenv;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SimpleSpringQuartz
{
public static void main(String[] args)
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
}
}
结果:
搞定了