In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to dynamically add, delete, modify and pause recovery timing tasks based on line layer pool Spring-quartz in java". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to dynamically add, delete, modify and pause recovery timing tasks based on line layer pool Spring-quartz in java".
Note: there is no code for regular tasks to add, delete, change and check pages, save databases and other operations in this article. Self-improvement according to the actual needs
Pom.xml
Org.springframework spring-context-support 4.2.5.RELEASE org.quartz-scheduler quartz 2.2.1
Spring-scheduler.xml
ApplicationContext.xml (spring profile)
ScheduleJobMode.java
Package com.scheduler;import java.io.Serializable;import java.util.Date;/** * scheduled task mode * / public class ScheduleJobMode implements Serializable {private static final long serialVersionUID = 1L; / / Task scheduling parameter key public static final String JOB_PARAM_KEY = "JOB_PARAM_KEY"; / / Task id private String jobId; / / spring bean name bean name cannot be empty private String beanName / / method name cannot be empty private String methodName; / / Parameter private String params; / / cron expression private String cronExpression; / / Task status 1: normal, 2: pause private String status; / / remarks private String remark; / / creator private String createName; / / creation time private Date createTime; / / recent update private String modifierName / / Last updated 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 setCron_Expression (String cronExpression) {this.cronExpression = cronExpression;} public String getCron_Expression () {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;/** * regular log execution * / public class ScheduleJobLogMode implements Serializable {private static final long serialVersionUID = 1L; / / log id private String logId; / / task id private String jobId; / / spring bean name private String beanName; / / method name private String methodName; / / parameter private String params / / Task status 0: success 1: failed private String status; / / failed information private String error; / / time consuming (in milliseconds) private Integer times; / / creation time 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;/** * scheduled task tool class * / public class ScheduleUtils {private static final Logger logger = LoggerFactory.getLogger (ScheduleUtils.class); private static final String JOB_NAME = "TASK_" / * get trigger key * / public static TriggerKey getTriggerKey (String jobId) {return TriggerKey.triggerKey (JOB_NAME + jobId);} / * get jobKey * / public static JobKey getJobKey (String jobId) {return JobKey.jobKey (JOB_NAME + jobId) } / * get expression trigger * / public static CronTrigger getCronTrigger (Scheduler scheduler, String jobId) {try {return (CronTrigger) scheduler.getTrigger (getTriggerKey (jobId));} catch (SchedulerException e) {logger.info ("get scheduled task CronTrigger exception", e); return null }} / * create scheduled tasks * / public static void createScheduleJob (Scheduler scheduler, ScheduleJobMode scheduleJob) {try {/ / build job information JobDetail jobDetail = JobBuilder.newJob (ScheduleJob.class) .withidentity (getJobKey (scheduleJob.getJobId () .build () / / expression scheduling builder CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule (scheduleJob.getCron_Expression ()). WithMisfireHandlingInstructionDoNothing (); / / build a new cronExpression = TriggerBuilder.newTrigger (). WithIdentity (getTriggerKey (scheduleJob.getJobId (). Withschedule (scheduleBuilder). Build () / / put in parameters, the runtime method can get jobDetail.getJobDataMap (). Put (ScheduleJobMode.JOB_PARAM_KEY, scheduleJob); scheduler.scheduleJob (jobDetail, trigger); / / pause the task if (scheduleJob.getStatus (). Equals (TaskSupport.TASK_STATUS_STOPPED)) {pauseJob (scheduler, scheduleJob.getJobId ()) }} catch (SchedulerException e) {logger.info ("failed to create scheduled task", e);}} / * Update scheduled task * / public static void updateScheduleJob (Scheduler scheduler, ScheduleJobMode scheduleJob) {try {TriggerKey triggerKey = getTriggerKey (scheduleJob.getJobId ()) / / expression scheduling builder CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule (scheduleJob.getCron_Expression ()). WithMisfireHandlingInstructionDoNothing (); CronTrigger trigger = getCronTrigger (scheduler, scheduleJob.getJobId ()) If (null! = trigger) {/ / rebuild trigger trigger = trigger.getTriggerBuilder (). WithIdentity (triggerKey). Withschedule (scheduleBuilder). Build () according to the new cronExpression expression; / / parameter trigger.getJobDataMap (). Put (ScheduleJobMode.JOB_PARAM_KEY, scheduleJob); scheduler.rescheduleJob (triggerKey, trigger) / / suspend the task if (scheduleJob.getStatus () .equals (TaskSupport.TASK_STATUS_STOPPED)) {pauseJob (scheduler, scheduleJob.getJobId ());}} else {throw new NullPointerException () }} catch (SchedulerException e) {logger.info ("Update scheduled task failed", e);}} / * * execute task immediately * / public static void run (Scheduler scheduler, ScheduleJobMode scheduleJob) {try {/ / parameter JobDataMap dataMap = new JobDataMap () DataMap.put (ScheduleJobMode.JOB_PARAM_KEY, scheduleJob); scheduler.triggerJob (getJobKey (scheduleJob.getJobId ()), dataMap);} catch (SchedulerException e) {logger.info ("immediate execution of scheduled tasks failed", e) }} / * suspend task * / public static void pauseJob (Scheduler scheduler, String jobId) {try {scheduler.pauseJob (getJobKey (jobId));} catch (SchedulerException e) {logger.info ("pause scheduled task failed", e) }} / * restore task * / public static void resumeJob (Scheduler scheduler, String jobId) {try {scheduler.resumeJob (getJobKey (jobId));} catch (SchedulerException e) {logger.info ("pause scheduled task failed", e) }} / * delete scheduled tasks * / public static void deleteScheduleJob (Scheduler scheduler, String jobId) {try {scheduler.deleteJob (getJobKey (jobId));} catch (SchedulerException e) {logger.info ("failed to delete scheduled tasks", 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;@Componentpublic class QuartzManager {@ Autowired private SchedulerFactoryBean schedulerFactoryBean / * when the project starts, initialize the timer * / @ PostConstruct public void init () {/ / query the scheduled task of saving to the data. There is no data code in this article, so you can improve List scheduleJobList = schedulerJobDao.queryList (new HashMap ()); Scheduler scheduler = schedulerFactoryBean.getScheduler () For (ScheduleJobMode scheduleJob: scheduleJobList) {CronTrigger cronTrigger = ScheduleUtils.getCronTrigger (scheduler, scheduleJob.getJobId ()); / / if it does not exist, create if (cronTrigger = = null) {ScheduleUtils.createScheduleJob (scheduler, scheduleJob);} else {ScheduleUtils.updateScheduleJob (scheduler, scheduleJob) }} / * create timer * / public void createScheduleJob (ScheduleJobMode scheduleJob) {Scheduler scheduler = schedulerFactoryBean.getScheduler (); ScheduleUtils.createScheduleJob (scheduler, scheduleJob);} / * Update timer * / public void update (ScheduleJobMode scheduleJob) {Scheduler scheduler = schedulerFactoryBean.getScheduler (); ScheduleUtils.updateScheduleJob (scheduler, scheduleJob) } / * delete timer * / public void deleteBatch (String [] jobIds) {Scheduler scheduler = schedulerFactoryBean.getScheduler (); for (String jobId: jobIds) {ScheduleUtils.deleteScheduleJob (scheduler, jobId) }} / * immediately execute timer * / public void run (String [] jobIds) {Scheduler scheduler = schedulerFactoryBean.getScheduler (); for (String jobId: jobIds) {/ / queryObject (jobId) is to query the object ScheduleUtils.run (scheduler, queryObject (jobId)) from the database }} / * suspend scheduled tasks * / public void pause (String [] jobIds) {Scheduler scheduler = schedulerFactoryBean.getScheduler (); for (String jobId: jobIds) {ScheduleUtils.pauseJob (scheduler, jobId) }} / * recovery scheduled task * / 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;/** * SpringContext utility class * / @ Componentpublic 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 getBean (String name, Class 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 future = service.submit (task) Future.get (); / / Total task execution time long times = System.currentTimeMillis ()-startTime; log.setTimes ((int) times); / / Task status 0: success 1: failed log.setStatus ("0") Logger.info ("task execution completed, task ID:" + scheduleJob.getJobId () + "total time:" + times + "millisecond");} catch (Exception e) {logger.error ("task execution failed, task ID:" + scheduleJob.getJobId (), e); / / Total task execution time long times = System.currentTimeMillis ()-startTime Log.setTimes ((int) times); / / Task status 0: success 1: failed log.setStatus ("1"); log.setError (scheduleJob.getBeanName () + "." + scheduleJob.getMethodName ());} finally {scheduleJobLogService.save (log) } Thank you for your reading. The above is the content of "how to dynamically add, delete, modify and pause recovery scheduled tasks based on line layer pool Spring-quartz in java". After the study of this article, I believe you have a deeper understanding of how to dynamically add, delete, modify and pause recovery scheduled tasks based on line layer pool Spring-quartz in java, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.