标签:tick resume delegate 分组 实体 tac 计划 存在 name
Spring4整合quartz2.2.3,quartz动态任务
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
?Copyright 蕃薯耀 2017年9月6日
http://www.cnblogs.com/fanshuyao/
可以直接看总结篇: http://www.cnblogs.com/fanshuyao/p/7484843.html
quartz下载
http://www.quartz-scheduler.org/downloads/
一、quartz动态任务管理类:
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Set;
-
- import org.quartz.CronScheduleBuilder;
- import org.quartz.CronTrigger;
- import org.quartz.Job;
- import org.quartz.JobBuilder;
- import org.quartz.JobDetail;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobKey;
- import org.quartz.Scheduler;
- import org.quartz.SchedulerException;
- import org.quartz.Trigger;
- import org.quartz.TriggerBuilder;
- import org.quartz.TriggerKey;
- import org.quartz.impl.matchers.GroupMatcher;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
-
- import cn.imovie.common.utils.CC;
- import cn.imovie.common.utils.JasonUtils;
- import cn.imovie.common.utils.SpringUtils;
- import cn.imovie.entity.task.ScheduleJob;
- import cn.imovie.service.ScheduleJobService;
-
- public class SchedulerManage {
-
- private Logger log = LoggerFactory.getLogger(SchedulerManage.class);
-
- private Scheduler scheduler = (Scheduler) SpringUtils.getBeanById("scheduler");
-
- @Autowired
- private ScheduleJobService scheduleJobService;
-
-
- @SuppressWarnings("unchecked")
- public void addJob(ScheduleJob scheduleJob){
- TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
- try {
-
- CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
- if (null == trigger) {
- JobDetail jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(scheduleJob.getClazz()))
- .withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).build();
- jobDetail.getJobDataMap().put("scheduleJob", scheduleJob);
-
- CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());
-
- trigger = TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).withSchedule(cronScheduleBuilder.withMisfireHandlingInstructionDoNothing()).build();
- scheduler.scheduleJob(jobDetail, trigger);
- log.info(CC.LOG_PREFIX + "新增任务:"+JasonUtils.Object2String(scheduleJob));
- }else {
-
-
- CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());
-
- trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder.withMisfireHandlingInstructionDoNothing()).build();
-
- scheduler.rescheduleJob(triggerKey, trigger);
- log.info(CC.LOG_PREFIX + "任务"+JasonUtils.Object2String(scheduleJob)+"已经存在,更新trigger");
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- public void pauseJob(ScheduleJob scheduleJob){
- JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
- try {
- scheduler.pauseJob(jobKey);
- log.info(CC.LOG_PREFIX + "暂停任务:"+JasonUtils.Object2String(scheduleJob));
- } catch (SchedulerException e) {
- e.printStackTrace();
- }
- }
-
-
- public void pauseAll(){
- try {
- scheduler.pauseAll();
- log.info(CC.LOG_PREFIX + "暂停所有任务");
- } catch (SchedulerException e) {
- e.printStackTrace();
- }
- }
-
-
- public void resumeJob(ScheduleJob scheduleJob){
- JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
- try {
- scheduler.resumeJob(jobKey);
- log.info(CC.LOG_PREFIX + "恢复任务:"+JasonUtils.Object2String(scheduleJob));
- } catch (SchedulerException e) {
- e.printStackTrace();
- }
- }
-
-
-
- public void resumeAll(){
- try {
- scheduler.resumeAll();
- log.info(CC.LOG_PREFIX + "恢复所有任务");
- } catch (SchedulerException e) {
- e.printStackTrace();
- }
- }
-
-
- public void deleteJob(ScheduleJob scheduleJob){
- JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
- try {
- scheduler.pauseJob(jobKey);
- scheduler.deleteJob(jobKey);
- log.info(CC.LOG_PREFIX + "删除任务:"+JasonUtils.Object2String(scheduleJob));
- } catch (SchedulerException e) {
- e.printStackTrace();
- }
- }
-
-
- public void triggerJob(ScheduleJob scheduleJob){
- JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
- try {
- scheduler.triggerJob(jobKey);
- log.info(CC.LOG_PREFIX + "运行任务:"+JasonUtils.Object2String(scheduleJob));
- } catch (SchedulerException e) {
- e.printStackTrace();
- }
- }
-
-
- public void updateJob(ScheduleJob scheduleJob){
- try {
- TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
-
-
- CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
-
-
- CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());
-
-
- trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder.withMisfireHandlingInstructionDoNothing()).build();
-
- scheduler.rescheduleJob(triggerKey, trigger);
- log.info(CC.LOG_PREFIX + "更新任务:"+JasonUtils.Object2String(scheduleJob));
- } catch (SchedulerException e) {
- e.printStackTrace();
- }
- }
-
-
-
- public List<ScheduleJob> getScheduleJobList(){
- List<ScheduleJob> jobList = null;
- try {
- GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
- Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
- jobList = new ArrayList<ScheduleJob>();
- for (JobKey jobKey : jobKeys) {
- List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
- for (Trigger trigger : triggers) {
- ScheduleJob job = new ScheduleJob();
- job.setJobName(jobKey.getName());
- job.setJobGroup(jobKey.getGroup());
- job.setClazz(jobKey.getClass().toString());
- job.setJobDesc("触发器:" + trigger.getKey());
- Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
- job.setJobStatus(triggerState.name());
- if (trigger instanceof CronTrigger) {
- CronTrigger cronTrigger = (CronTrigger) trigger;
- String cronExpression = cronTrigger.getCronExpression();
- job.setCronExpression(cronExpression);
- }
- jobList.add(job);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return jobList;
- }
-
-
- public List<ScheduleJob> getScheduleJobRunningList(){
- List<ScheduleJob> jobList = null;
- try {
- List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
- jobList = new ArrayList<ScheduleJob>(executingJobs.size());
- for (JobExecutionContext executingJob : executingJobs) {
- ScheduleJob job = new ScheduleJob();
- JobDetail jobDetail = executingJob.getJobDetail();
- JobKey jobKey = jobDetail.getKey();
- Trigger trigger = executingJob.getTrigger();
- job.setJobName(jobKey.getName());
- job.setJobGroup(jobKey.getGroup());
- job.setClazz(jobKey.getClass().toString());
- job.setJobDesc("触发器:" + trigger.getKey());
- Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
- job.setJobStatus(triggerState.name());
- if (trigger instanceof CronTrigger) {
- CronTrigger cronTrigger = (CronTrigger) trigger;
- String cronExpression = cronTrigger.getCronExpression();
- job.setCronExpression(cronExpression);
- }
- jobList.add(job);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return jobList;
- }
-
- public Scheduler getScheduler() {
- return scheduler;
- }
-
- public void setScheduler(Scheduler scheduler) {
- this.scheduler = scheduler;
- }
-
-
-
- }
注意事项:
1、SchedulerFactoryBean 在spring.xml配置了,但不能直接注入,就算加上@Component注解也不行,所以使用SpringUtils 工具类获取。
- <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
2、获取到的SchedulerFactoryBean 并不是SchedulerFactoryBean,而是scheduler
- private Scheduler scheduler = (Scheduler) SpringUtils.getBeanById("scheduler");
二、ScheduleJob 类:可以保存计划任务的信息,也可以做为实体参数
三、Spring.xml 文件配置
- <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
-
- <property name="overwriteExistingJobs" value="true" />
-
- <property name="startupDelay" value="10" />
-
- <property name="autoStartup" value="true" />
- <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
- <property name="configLocation" value="classpath:spring-quartz.properties" />
- </bean>
-
- <bean id="schedulerManage" class="cn.imovie.manage.task.SchedulerManage"></bean>
-
四、spring-quartz.properties 文件配置:
- #配置见:http:
- #============================================================================
- # Configure Main Scheduler Properties
- #============================================================================
-
- org.quartz.scheduler.instanceName = MyClusteredScheduler
- org.quartz.scheduler.instanceId = AUTO
-
- #============================================================================
- # Configure ThreadPool
- #============================================================================
-
- org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
- org.quartz.threadPool.threadCount = 25
- org.quartz.threadPool.threadPriority = 5
-
- #============================================================================
- # Configure JobStore
- #============================================================================
-
- org.quartz.jobStore.misfireThreshold = 60000
-
- org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
- org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
- org.quartz.jobStore.useProperties = false
- org.quartz.jobStore.dataSource = myDS
- org.quartz.jobStore.tablePrefix = QRTZ_
-
- org.quartz.jobStore.isClustered = true
- org.quartz.jobStore.clusterCheckinInterval = 20000
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
?Copyright 蕃薯耀 2017年9月6日
http://www.cnblogs.com/fanshuyao/
Spring4整合quartz2.2.3,quartz动态任务
标签:tick resume delegate 分组 实体 tac 计划 存在 name
原文地址:http://www.cnblogs.com/fanshuyao/p/7484784.html