您好,登錄后才能下訂單哦!
這篇文章主要講解了“java中基于線層池Spring-quartz如何實現動態增刪除改和暫停恢復定時任務”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“java中基于線層池Spring-quartz如何實現動態增刪除改和暫停恢復定時任務”吧!
注:本文中沒有定時任務增刪改查頁面、保存數據庫等操作的代碼。根據實際需求自行完善
pom.xml
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
spring-scheduler.xml
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"/> </beans>
ApplicationContext.xml (spring配置文件)
<!-- 增加quartz定時任務管理配置 --> <import resource="spring-scheduler.xml"/>
ScheduleJobMode.java
package com.scheduler; import java.io.Serializable; import java.util.Date; /** * 定時任務mode */ public class ScheduleJobMode implements Serializable { private static final long serialVersionUID = 1L; //任務調度參數key public static final String JOB_PARAM_KEY = "JOB_PARAM_KEY"; //任務id private String jobId; //spring bean名稱 bean名稱不能為空 private String beanName; //方法名 方法名稱不能為空 private String methodName; //參數 private String params; //cron表達式 private String cronExpression; //任務狀態 1:正常,2:暫停 private String status; //備注 private String remark; //創建者 private String createName; //創建時間 private Date createTime; //最近更新人 private String modifierName; //最近更新時間 private Date modifierTime; public void setJobId(String jobId) { this.jobId = jobId; } public String getJobId() { return jobId; } public String getBeanName() { return beanName; } public void setBeanName(String beanName) { this.beanName = beanName; } public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } public String getParams() { return params; } public void setParams(String params) { this.params = params; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public void setStatus(String status) { this.status = status; } public String getStatus() { return status; } public void setCronExpression(String cronExpression) { this.cronExpression = cronExpression; } public String getCronExpression() { return cronExpression; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getCreateTime() { return createTime; } public String getCreateName() { return createName; } public void setCreateName(String createName) { this.createName = createName; } public String getModifierName() { return modifierName; } public void setModifierName(String modifierName) { this.modifierName = modifierName; } public Date getModifierTime() { return modifierTime; } public void setModifierTime(Date modifierTime) { this.modifierTime = modifierTime; } }
ScheduleJobLogMode.java
package com.scheduler; import java.io.Serializable; import java.util.Date; /** * 定時執行日志 */ public class ScheduleJobLogMode implements Serializable { private static final long serialVersionUID = 1L; //日志id private String logId; //任務id private String jobId; //spring bean名稱 private String beanName; //方法名 private String methodName; //參數 private String params; //任務狀態 0:成功 1:失敗 private String status; //失敗信息 private String error; //耗時(單位:毫秒) private Integer times; //創建時間 private Date createTime; public String getLogId() { return logId; } public void setLogId(String logId) { this.logId = logId; } public String getJobId() { return jobId; } public void setJobId(String jobId) { this.jobId = jobId; } public String getBeanName() { return beanName; } public void setBeanName(String beanName) { this.beanName = beanName; } public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } public String getParams() { return params; } public void setParams(String params) { this.params = params; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getError() { return error; } public void setError(String error) { this.error = error; } public Integer getTimes() { return times; } public void setTimes(Integer times) { this.times = times; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
ScheduleUtils.java
package com.scheduler; import com.wawj.fg.common.scheduler.ScheduleJob; import com.wawj.fg.common.scheduler.support.TaskSupport; import com.scheduler.ScheduleJobMode; import org.quartz.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 定時任務工具類 */ public class ScheduleUtils { private static final Logger logger = LoggerFactory.getLogger(ScheduleUtils.class); private static final String JOB_NAME = "TASK_"; /** * 獲取觸發器key */ public static TriggerKey getTriggerKey(String jobId) { return TriggerKey.triggerKey(JOB_NAME + jobId); } /** * 獲取jobKey */ public static JobKey getJobKey(String jobId) { return JobKey.jobKey(JOB_NAME + jobId); } /** * 獲取表達式觸發器 */ public static CronTrigger getCronTrigger(Scheduler scheduler, String jobId) { try { return (CronTrigger) scheduler.getTrigger(getTriggerKey(jobId)); } catch (SchedulerException e) { logger.info("獲取定時任務CronTrigger出現異常", e); return null; } } /** * 創建定時任務 */ public static void createScheduleJob(Scheduler scheduler, ScheduleJobMode scheduleJob) { try { //構建job信息 JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(getJobKey(scheduleJob.getJobId())).build(); //表達式調度構建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()) .withMisfireHandlingInstructionDoNothing(); //按新的cronExpression表達式構建一個新的trigger CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(scheduleJob.getJobId())).withSchedule(scheduleBuilder).build(); //放入參數,運行時的方法可以獲取 jobDetail.getJobDataMap().put(ScheduleJobMode.JOB_PARAM_KEY, scheduleJob); scheduler.scheduleJob(jobDetail, trigger); //暫停任務 if (scheduleJob.getStatus().equals(TaskSupport.TASK_STATUS_STOPPED)) { pauseJob(scheduler, scheduleJob.getJobId()); } } catch (SchedulerException e) { logger.info("創建定時任務失敗", e); } } /** * 更新定時任務 */ public static void updateScheduleJob(Scheduler scheduler, ScheduleJobMode scheduleJob) { try { TriggerKey triggerKey = getTriggerKey(scheduleJob.getJobId()); //表達式調度構建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()) .withMisfireHandlingInstructionDoNothing(); CronTrigger trigger = getCronTrigger(scheduler, scheduleJob.getJobId()); if (null != trigger) { //按新的cronExpression表達式重新構建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); //參數 trigger.getJobDataMap().put(ScheduleJobMode.JOB_PARAM_KEY, scheduleJob); scheduler.rescheduleJob(triggerKey, trigger); //暫停任務 if (scheduleJob.getStatus().equals(TaskSupport.TASK_STATUS_STOPPED)) { pauseJob(scheduler, scheduleJob.getJobId()); } } else { throw new NullPointerException(); } } catch (SchedulerException e) { logger.info("更新定時任務失敗", e); } } /** * 立即執行任務 */ public static void run(Scheduler scheduler, ScheduleJobMode scheduleJob) { try { //參數 JobDataMap dataMap = new JobDataMap(); dataMap.put(ScheduleJobMode.JOB_PARAM_KEY, scheduleJob); scheduler.triggerJob(getJobKey(scheduleJob.getJobId()), dataMap); } catch (SchedulerException e) { logger.info("立即執行定時任務失敗", e); } } /** * 暫停任務 */ public static void pauseJob(Scheduler scheduler, String jobId) { try { scheduler.pauseJob(getJobKey(jobId)); } catch (SchedulerException e) { logger.info("暫停定時任務失敗", e); } } /** * 恢復任務 */ public static void resumeJob(Scheduler scheduler, String jobId) { try { scheduler.resumeJob(getJobKey(jobId)); } catch (SchedulerException e) { logger.info("暫停定時任務失敗", e); } } /** * 刪除定時任務 */ public static void deleteScheduleJob(Scheduler scheduler, String jobId) { try { scheduler.deleteJob(getJobKey(jobId)); } catch (SchedulerException e) { logger.info("刪除定時任務失敗", e); } } }
QuartzManager.java
package com.scheduler; import com.wawj.fg.common.scheduler.ScheduleUtils; import com.wawj.fg.model.baseinfo.scheduler.ScheduleJobMode; import org.quartz.CronTrigger; import org.quartz.Scheduler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.util.HashMap; import java.util.List; @Component public class QuartzManager { @Autowired private SchedulerFactoryBean schedulerFactoryBean; /** * 項目啟動時,初始化定時器 */ @PostConstruct public void init() { //查詢保存到數據的定時任務,本文沒有操作數據代碼,可自行完善 List<ScheduleJobMode> scheduleJobList = schedulerJobDao.queryList(new HashMap<String, Object>()); Scheduler scheduler = schedulerFactoryBean.getScheduler(); for (ScheduleJobMode scheduleJob : scheduleJobList) { CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getJobId()); //如果不存在,則創建 if (cronTrigger == null) { ScheduleUtils.createScheduleJob(scheduler, scheduleJob); } else { ScheduleUtils.updateScheduleJob(scheduler, scheduleJob); } } } /** * 創建定時器 */ public void createScheduleJob(ScheduleJobMode scheduleJob) { Scheduler scheduler = schedulerFactoryBean.getScheduler(); ScheduleUtils.createScheduleJob(scheduler, scheduleJob); } /** * 更新定時器 */ public void update(ScheduleJobMode scheduleJob) { Scheduler scheduler = schedulerFactoryBean.getScheduler(); ScheduleUtils.updateScheduleJob(scheduler, scheduleJob); } /** * 刪除定時器 */ public void deleteBatch(String[] jobIds) { Scheduler scheduler = schedulerFactoryBean.getScheduler(); for (String jobId : jobIds) { ScheduleUtils.deleteScheduleJob(scheduler, jobId); } } /** * 立刻執行一下定時器 */ public void run(String[] jobIds) { Scheduler scheduler = schedulerFactoryBean.getScheduler(); for (String jobId : jobIds) { //queryObject(jobId) 是從數據庫中查詢出對象 ScheduleUtils.run(scheduler, queryObject(jobId)); } } /** * 暫停定時任務 */ public void pause(String[] jobIds) { Scheduler scheduler = schedulerFactoryBean.getScheduler(); for (String jobId : jobIds) { ScheduleUtils.pauseJob(scheduler, jobId); } } /** * 恢復定時任務 */ public void resume(String[] jobIds) { Scheduler scheduler = schedulerFactoryBean.getScheduler(); for (String jobId : jobIds) { ScheduleUtils.resumeJob(scheduler, jobId); } } }
SpringUtils.java
package com.scheduler; import com.wawj.fg.util.SpringContextUtils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * Spring Context 工具類 */ @Component public class SpringUtils implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtils.applicationContext = applicationContext; } public static Object getBean(String name) { return applicationContext.getBean(name); } public static <T> T getBean(String name, Class<T> requiredType) { return applicationContext.getBean(name, requiredType); } public static boolean containsBean(String name) { return applicationContext.containsBean(name); } public static boolean isSingleton(String name) { return applicationContext.isSingleton(name); } public static Class<? extends Object> getType(String name) { return applicationContext.getType(name); } }
ScheduleRunnable.java
package com.scheduler; import java.lang.reflect.Method; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.util.ReflectionUtils; import com.scheduler;.SpringUtils; /** * 執行定時任務 */ public class ScheduleRunnable implements Runnable { private static final Logger logger = Logger.getLogger(ScheduleRunnable.class); //類名 private Object target; //方法名 private Method method; //參數 private String params; public ScheduleRunnable(String beanName, String methodName, String params) throws NoSuchMethodException{ this.target = SpringUtils.getBean(beanName); this.params = params; if(StringUtils.isNotBlank(params)){ this.method = target.getClass().getDeclaredMethod(methodName, String.class); }else{ this.method = target.getClass().getDeclaredMethod(methodName); } } @Override public void run() { try { ReflectionUtils.makeAccessible(method); if(StringUtils.isNotBlank(params)){ method.invoke(target, params); }else{ method.invoke(target); } }catch (Exception e) { logger.error("執行定時任務失敗",e); throw new RuntimeException(); } } }
ScheduleJob.java
package com.scheduler; import com.wawj.fg.common.scheduler.ScheduleRunnable; import com.wawj.fg.model.baseinfo.scheduler.ScheduleJobLogMode; import com.wawj.fg.model.baseinfo.scheduler.ScheduleJobMode; import com.wawj.fg.service.baseinfo.scheduler.ScheduleJobLogService; import com.wawj.fg.util.SpringContextUtils; import com.wawj.fg.util.UUIDUtil; import org.apache.log4j.Logger; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; /** * 定時任務 */ public class ScheduleJob extends QuartzJobBean { private static final Logger logger = Logger.getLogger(ScheduleJob.class); //創建線層池 (創建一個單線程化的線程池) private ExecutorService service = Executors.newSingleThreadExecutor(); @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { ScheduleJobMode scheduleJob = (ScheduleJobMode) context.getMergedJobDataMap() .get(ScheduleJobMode.JOB_PARAM_KEY); //獲取spring bean ScheduleJobLogService scheduleJobLogService = (ScheduleJobLogService) SpringContextUtils.getBean("scheduleJobLogService"); //數據庫保存執行記錄 ScheduleJobLogMode log = new ScheduleJobLogMode(); log.setLogId(UUIDUtil.getUUID()); log.setJobId(scheduleJob.getJobId()); log.setBeanName(scheduleJob.getBeanName()); log.setMethodName(scheduleJob.getMethodName()); log.setParams(scheduleJob.getParams()); log.setCreateTime(new Date()); //任務開始時間 long startTime = System.currentTimeMillis(); try { //執行任務 logger.info("任務準備執行,任務ID:" + scheduleJob.getJobId()); ScheduleRunnable task = new ScheduleRunnable(scheduleJob.getBeanName(), scheduleJob.getMethodName(), scheduleJob.getParams()); Future<?> future = service.submit(task); future.get(); //任務執行總時長 long times = System.currentTimeMillis() - startTime; log.setTimes((int) times); //任務狀態 0:成功 1:失敗 log.setStatus("0"); logger.info("任務執行完畢,任務ID:" + scheduleJob.getJobId() + " 總共耗時:" + times + "毫秒"); } catch (Exception e) { logger.error("任務執行失敗,任務ID:" + scheduleJob.getJobId(), e); //任務執行總時長 long times = System.currentTimeMillis() - startTime; log.setTimes((int) times); //任務狀態 0:成功 1:失敗 log.setStatus("1"); log.setError(scheduleJob.getBeanName() + "." + scheduleJob.getMethodName()); } finally { scheduleJobLogService.save(log); } } }
感謝各位的閱讀,以上就是“java中基于線層池Spring-quartz如何實現動態增刪除改和暫停恢復定時任務”的內容了,經過本文的學習后,相信大家對java中基于線層池Spring-quartz如何實現動態增刪除改和暫停恢復定時任務這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。