job 类
import java.util.Date;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
//@Configurable
//@EnableScheduling
@Component
public class quartzTest {
// @Scheduled(cron="0/10 * * * * ? ")
public void quartz ()
{
System.out.println("=========10秒执行一次============="+new Date());
}
}
任务管理器
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import com.bootdo.common.quartz.quartzTest;
@Configuration
@EnableScheduling
public class JobConfig {
/**
* attention:
* Details:配置定时任务
*/
@Bean(name = "detailFactoryBean")
public MethodInvokingJobDetailFactoryBean detailFactoryBean(quartzTest quartzTest) {// ScheduleTask为需要执行的任务
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
/*
* 是否并发执行
* 例如每5s执行一次任务,但是当前任务还没有执行完,就已经过了5s了,
* 如果此处为true,则下一个任务会执行,如果此处为false,则下一个任务会等待上一个任务执行完后,再开始执行
*/
jobDetail.setConcurrent(false);
jobDetail.setName("xxxx");// 设置任务的名字
jobDetail.setGroup("xxx");// 设置任务的分组,这些属性都可以存储在数据库中,在多任务的时候使用
/*
* 为需要执行的实体类对应的对象
*/
jobDetail.setTargetObject(quartzTest);
/*
* quartz为需要执行的方法
* 通过这几个配置,告诉JobDetailFactoryBean我们需要执行定时执行quartzTest类中的quartz方法
*/
jobDetail.setTargetMethod("quartz");
return jobDetail;
}
/**
* attention:
* Details:配置定时任务的触发器,也就是什么时候触发执行定时任务
*/
@Bean(name = "cronJobTrigger")
public CronTriggerFactoryBean cronJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail) {
CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
tigger.setJobDetail(jobDetail.getObject());
tigger.setCronExpression("0/10 * * * * ? ");// 初始时的cron表达式
tigger.setName("xxxx");// trigger的name
return tigger;
}
/**
* attention:
* Details:定义quartz调度工厂
*/
@Bean
public SchedulerFactoryBean jobSchedulerFactoryBean(Trigger cronJobTrigger) {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
// 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
bean.setOverwriteExistingJobs(true);
// 延时启动,应用启动1秒后
bean.setStartupDelay(1);
// 注册触发器
bean.setTriggers(cronJobTrigger);
return bean;
}
}
控制类
public class xxxx{
@Autowired
@Qualifier("jobSchedulerFactoryBean")
private SchedulerFactoryBean jobSchedulerFactoryBean;
@Autowired
@Qualifier("cronJobTrigger")
private CronTriggerFactoryBean cronJobTrigger;
public void stop() throws SchedulerException{
jobSchedulerFactoryBean.stop();
}
public void start() throws SchedulerException{
jobSchedulerFactoryBean.start();
}
//修改Cron
public void setCron(String newCron)
{
cronJobTrigger.setCronExpression(newCron); //配置cron
cronJobTrigger.afterPropertiesSet(); //重置cronTrigger
CronTrigger object = cronJobTrigger.getObject();
TriggerKey key = object.getKey();
Scheduler scheduler = jobSchedulerFactoryBean.getScheduler();
scheduler.rescheduleJob(key, object); //scheduler重置
}
public Scheduler getScheduler() throws SchedulerException{
Scheduler s= jobSchedulerFactoryBean.getScheduler();
}
}