Spring zz2 dynamic task instance details, springquartz2

Source: Internet
Author: User

Spring zz2 dynamic task instance details, springquartz2

Examples of Spring Quartz2 Dynamic Tasks

The SimpleScheduleBuilder class in Quartz is used here. Non-CronScheduleBuilder and CronScheduleBuilder are Cron expressions. For details, refer to Baidu.

Implementation Code:

/*** Add a task ** @ param scheduleJob * @ throws Exception */@ Override @ handle ("unchecked") public void addJobSimple (ScheduleJob scheduleJob) throws Exception {TriggerKey triggerKey = TriggerKey. triggerKey (scheduleJob. getJobName (), scheduleJob. getJobGroup (); // Trigger trigger Trigger = schedgger. 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); SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder. simpleSchedule (); if ("seconds ". equals (scheduleJob. getTimeType () | "second ". specified signorecase (scheduleJob. getTimeType () {simpleScheduleBuilder. withIntervalInSeconds (scheduleJob. getTimeValue ()). repeatForever ();} else if ("points ". equals (scheduleJob. getTimeType () | "Minute ". equals (scheduleJob. getTimeType () | "minute ". specified signorecase (scheduleJob. getTimeType () {simpleScheduleBuilder. withIntervalInMinutes (scheduleJob. getTimeValue ()). repeatForever ();} else if ("Hour ". equals (scheduleJob. getTimeType () | "Hour ". equals (scheduleJob. getTimeType () | "hour ". specified signorecase (scheduleJob. getTimeType () {simpleScheduleBuilder. withIntervalInHours (scheduleJob. getTimeValue ()). repeatForever ();} else if ("day ". equals (scheduleJob. getTimeType () | "date ". specified signorecase (scheduleJob. getTimeType () {simpleScheduleBuilder. withIntervalInHours (scheduleJob. getTimeValue ()). repeatForever ();} ScheduleBuilder <SimpleTrigger> schedBuilder = simpleScheduleBuilder. withMisfireHandlingInstructionNextWithRemainingCount (); trigger = TriggerBuilder. newTrigger (). withIdentity (scheduleJob. getJobName (), scheduleJob. getJobGroup ()). withSchedule (schedBuilder ). build (); trigger = TriggerBuilder. newTrigger (). withIdentity (scheduleJob. getJobName (), scheduleJob. getJobGroup ()). withSchedule (schedBuilder ). build (); scheduler. scheduleJob (jobDetail, trigger); log.info (CC. LOG_PREFIX + "add simple task:" + JasonUtils. object2String (scheduleJob);} else {this. updateJobCron (scheduleJob );}}

Note that, when a scheduled task is executed only once, you need to carefully read the code. The preceding simple scheduled task was just like me at the beginning:

simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue()); 

The result is executed only once, but what I need is continuous loop execution, and finally changed:

simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue()).repeatForever(); 

It can be seen that there is a repeatForever (), which indicates that the repeatForever is always repeated.

If you do not want to execute multiple times, use:. withRepeatCount (triggerRepeatCount) as follows:

simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue()).withRepeatCount(triggerRepeatCount); 

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.