在spring框架中通過 @Schedule 可以實現(xiàn)定時任務,通過該注解 cron 的屬性描述的規(guī)則,spring會去調用這個方法。
公司主營業(yè)務:網(wǎng)站建設、成都網(wǎng)站制作、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)建站是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出長寧免費做網(wǎng)站回饋大家。
spring已經(jīng)簡單粗暴的實現(xiàn)了定時任務,為什么要使用Quartz ?
如果你現(xiàn)在有很多個定時任務,規(guī)則不同,例如:
maven 依賴
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency>
以下是bootdo開源學習框架的源碼
我并沒有用到全部的字段,思路是先查詢DB,
封裝以下兩個對象
當觸發(fā)條件滿足時,會根據(jù)所設置的beanClass 找到該類(必須實現(xiàn)org.quartz.Job), 這時可以取出JobDetail 中的數(shù)據(jù),執(zhí)行具體業(yè)務邏輯
@Component public class WelcomeJob implements Job{ @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { //你的業(yè)務邏輯 } }
表結構
CREATE TABLE `sys_task` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `cronExpression` varchar(255) DEFAULT NULL COMMENT 'cron表達式', `methodName` varchar(255) DEFAULT NULL COMMENT '任務調用的方法名', `isConcurrent` varchar(255) DEFAULT NULL COMMENT '任務是否有狀態(tài)', `description` varchar(255) DEFAULT NULL COMMENT '任務描述', `updateBy` varchar(64) DEFAULT NULL COMMENT '更新者', `beanClass` varchar(255) DEFAULT NULL COMMENT '任務執(zhí)行時調用哪個類的方法 包名+類名', `createDate` datetime DEFAULT NULL COMMENT '創(chuàng)建時間', `jobStatus` varchar(255) DEFAULT NULL COMMENT '任務狀態(tài)', `jobGroup` varchar(255) DEFAULT NULL COMMENT '任務分組', `updateDate` datetime DEFAULT NULL COMMENT '更新時間', `createBy` varchar(64) DEFAULT NULL COMMENT '創(chuàng)建者', `springBean` varchar(255) DEFAULT NULL COMMENT 'Spring bean', `jobName` varchar(255) DEFAULT NULL COMMENT '任務名', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
配置類
import java.io.IOException; import java.util.Properties; import org.quartz.Scheduler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import com.txgl.common.quartz.factory.JobFactory; @Configuration public class QuartzConfigration { @Autowired JobFactory jobFactory; @Bean public SchedulerFactoryBean schedulerFactoryBean() { SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); try { schedulerFactoryBean.setOverwriteExistingJobs(true); schedulerFactoryBean.setQuartzProperties(quartzProperties()); schedulerFactoryBean.setJobFactory(jobFactory); } catch (IOException e) { e.printStackTrace(); } return schedulerFactoryBean; } // 指定quartz.properties @Bean public Properties quartzProperties() throws IOException { PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); propertiesFactoryBean.setLocation(new ClassPathResource("/config/quartz.properties")); propertiesFactoryBean.afterPropertiesSet(); return propertiesFactoryBean.getObject(); } // 創(chuàng)建schedule @Bean(name = "scheduler") public Scheduler scheduler() { return schedulerFactoryBean().getScheduler(); } }
QuartzManager的代碼是關鍵,通過注入Scheduler 對任務進行操作
import java.util.ArrayList; import java.util.List; import java.util.Set; import org.apache.log4j.Logger; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.DateBuilder; import org.quartz.DateBuilder.IntervalUnit; 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.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.stereotype.Service; import org.springframework.web.context.support.SpringBeanAutowiringSupport; import com.bootdo.common.domain.ScheduleJob; import com.bootdo.common.quartz.factory.*; import com.bootdo.common.utils.SpringContextHolder;; /** * * * @title: QuartzManager.java * @description: 計劃任務管理 * */ @Service public class QuartzManager { public final Logger log = Logger.getLogger(this.getClass()); // private SchedulerFactoryBean schedulerFactoryBean // =SpringContextHolder.getBean(SchedulerFactoryBean.class); // @Autowired // @Qualifier("schedulerFactoryBean") // private SchedulerFactoryBean schedulerFactoryBean; @Autowired private Scheduler scheduler; /** * 添加任務 * * @param scheduleJob * @throws SchedulerException */ public void addJob(ScheduleJob job) { try { // 創(chuàng)建jobDetail實例,綁定Job實現(xiàn)類 // 指明job的名稱,所在組的名稱,以及綁定job類 Class<? extends Job> jobClass = (Class<? extends Job>) (Class.forName(job.getBeanClass()).newInstance() .getClass()); JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(job.getJobName(), job.getJobGroup())// 任務名稱和組構成任務key .build(); // 定義調度觸發(fā)規(guī)則 // 使用cornTrigger規(guī)則 Trigger trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup())// 觸發(fā)器key .startAt(DateBuilder.futureDate(1, IntervalUnit.SECOND)) .withSchedule(CronScheduleBuilder.cronSchedule(job.getCronExpression())).startNow().build(); // 把作業(yè)和觸發(fā)器注冊到任務調度中 scheduler.scheduleJob(jobDetail, trigger); // 啟動 if (!scheduler.isShutdown()) { scheduler.start(); } } catch (Exception e) { e.printStackTrace(); } } // public void addJob(ScheduleJob job) throws SchedulerException { // if (job == null || !ScheduleJob.STATUS_RUNNING.equals(job.getJobStatus())) { // return; // } // // TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup()); // // CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); // // // 不存在,創(chuàng)建一個 // // if (null == trigger) { // Class<? extends Job> clazz = ScheduleJob.CONCURRENT_IS.equals(job.getIsConcurrent()) // ? QuartzJobFactory.class // : QuartzJobFactoryDisallowConcurrentExecution.class; // // JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(job.getJobName(), job.getJobGroup()).build(); // // jobDetail.getJobDataMap().put("scheduleJob", job); // // CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); // // trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup()) // .withSchedule(scheduleBuilder).build(); // // scheduler.scheduleJob(jobDetail, trigger); // } else { // // Trigger已存在,那么更新相應的定時設置 // // CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); // // // 按新的cronExpression表達式重新構建trigger // // trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); // // // 按新的trigger重新設置job執(zhí)行 // // scheduler.rescheduleJob(triggerKey, trigger); // } // } /** * 獲取所有計劃中的任務列表 * * @return * @throws SchedulerException */ public List<ScheduleJob> getAllJob() throws SchedulerException { GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup(); Set<JobKey> jobKeys = scheduler.getJobKeys(matcher); List<ScheduleJob> 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.setDescription("觸發(fā)器:" + 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); } } return jobList; } /** * 所有正在運行的job * * @return * @throws SchedulerException */ public List<ScheduleJob> getRunningJob() throws SchedulerException { List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs(); List<ScheduleJob> 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.setDescription("觸發(fā)器:" + 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); } return jobList; } /** * 暫停一個job * * @param scheduleJob * @throws SchedulerException */ public void pauseJob(ScheduleJob scheduleJob) throws SchedulerException { JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); scheduler.pauseJob(jobKey); } /** * 恢復一個job * * @param scheduleJob * @throws SchedulerException */ public void resumeJob(ScheduleJob scheduleJob) throws SchedulerException { JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); scheduler.resumeJob(jobKey); } /** * 刪除一個job * * @param scheduleJob * @throws SchedulerException */ public void deleteJob(ScheduleJob scheduleJob) throws SchedulerException { JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); scheduler.deleteJob(jobKey); } /** * 立即執(zhí)行job * * @param scheduleJob * @throws SchedulerException */ public void runAJobNow(ScheduleJob scheduleJob) throws SchedulerException { JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); scheduler.triggerJob(jobKey); } /** * 更新job時間表達式 * * @param scheduleJob * @throws SchedulerException */ public void updateJobCron(ScheduleJob scheduleJob) throws SchedulerException { 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).build(); scheduler.rescheduleJob(triggerKey, trigger); } }
service實現(xiàn)
import com.bootdo.common.config.Constant; import com.bootdo.common.dao.TaskDao; import com.bootdo.common.domain.ScheduleJob; import com.bootdo.common.domain.TaskDO; import com.bootdo.common.quartz.utils.QuartzManager; import com.bootdo.common.service.JobService; import com.bootdo.common.utils.ScheduleJobUtils; import org.quartz.SchedulerException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class JobServiceImpl implements JobService { @Autowired private TaskDao taskScheduleJobMapper; @Autowired QuartzManager quartzManager; @Override public TaskDO get(Long id) { return taskScheduleJobMapper.get(id); } @Override public List<TaskDO> list(Map<String, Object> map) { return taskScheduleJobMapper.list(map); } @Override public int count(Map<String, Object> map) { return taskScheduleJobMapper.count(map); } @Override public int save(TaskDO taskScheduleJob) { return taskScheduleJobMapper.save(taskScheduleJob); } @Override public int update(TaskDO taskScheduleJob) { return taskScheduleJobMapper.update(taskScheduleJob); } @Override public int remove(Long id) { try { TaskDO scheduleJob = get(id); quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob)); return taskScheduleJobMapper.remove(id); } catch (SchedulerException e) { e.printStackTrace(); return 0; } } @Override public int batchRemove(Long[] ids) { for (Long id : ids) { try { TaskDO scheduleJob = get(id); quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob)); } catch (SchedulerException e) { e.printStackTrace(); return 0; } } return taskScheduleJobMapper.batchRemove(ids); } @Override public void initSchedule() throws SchedulerException { // 這里獲取任務信息數(shù)據(jù) List<TaskDO> jobList = taskScheduleJobMapper.list(new HashMap<String, Object>(16)); for (TaskDO scheduleJob : jobList) { if ("1".equals(scheduleJob.getJobStatus())) { ScheduleJob job = ScheduleJobUtils.entityToData(scheduleJob); quartzManager.addJob(job); } } } @Override public void changeStatus(Long jobId, String cmd) throws SchedulerException { TaskDO scheduleJob = get(jobId); if (scheduleJob == null) { return; } if (Constant.STATUS_RUNNING_STOP.equals(cmd)) { quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob)); scheduleJob.setJobStatus(ScheduleJob.STATUS_NOT_RUNNING); } else { if (!Constant.STATUS_RUNNING_START.equals(cmd)) { } else { scheduleJob.setJobStatus(ScheduleJob.STATUS_RUNNING); quartzManager.addJob(ScheduleJobUtils.entityToData(scheduleJob)); } } update(scheduleJob); } @Override public void updateCron(Long jobId) throws SchedulerException { TaskDO scheduleJob = get(jobId); if (scheduleJob == null) { return; } if (ScheduleJob.STATUS_RUNNING.equals(scheduleJob.getJobStatus())) { quartzManager.updateJobCron(ScheduleJobUtils.entityToData(scheduleJob)); } update(scheduleJob); } }
啟動一個監(jiān)聽去初始化Quartz
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import com.bootdo.common.quartz.utils.QuartzManager; import com.bootdo.common.service.JobService; @Component @Order(value = 1) public class ScheduleJobInitListener implements CommandLineRunner { @Autowired JobService scheduleJobService; @Autowired QuartzManager quartzManager; @Override public void run(String... arg0) throws Exception { try { scheduleJobService.initSchedule(); } catch (Exception e) { e.printStackTrace(); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站標題:springboot+Quartz實現(xiàn)任務調度的示例代碼
網(wǎng)頁網(wǎng)址:http://jinyejixie.com/article28/pgecjp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設、品牌網(wǎng)站設計、動態(tài)網(wǎng)站、定制網(wǎng)站、建站公司、
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)