kangarooxin edited a comment on issue #1859:
URL: 
https://github.com/apache/shardingsphere-elasticjob/issues/1859#issuecomment-939266278


   @TeslaCN 
   I have used it in my work,the main code is here,I'm glad to make contributes 
to this project.
   
   1. add config properties
   ```java
   @Getter
   @AllArgsConstructor(access = AccessLevel.PRIVATE)
   public final class JobConfiguration {
   
       private final String jobName;
   
       private final String cron;
   
       private final int fixedRate;
   
       private final TimeUnit fixedRateTimeUnit;
   
       .....
   }
   ```
   
   2. create scheduleJob with fixedRate
   ```java
   public final class ScheduleJobBootstrap implements JobBootstrap {
       
       .......
       
       /**
        * Schedule job.
        */
       public void schedule() {
           JobConfiguration jobConfig = jobScheduler.getJobConfig();
           if(!Strings.isNullOrEmpty(jobConfig.getCron())) {
               
jobScheduler.getJobScheduleController().scheduleJob(jobConfig.getCron());
           } else if(jobConfig.getFixedRate() > 0) {
               //create  job with fixedRate
               
jobScheduler.getJobScheduleController().scheduleJob(jobConfig.getFixedRate(), 
jobConfig.getFixedRateTimeUnit());
           } else {
               throw new IllegalArgumentException("Cron can not be empty.");
           }
       }
       
       ......
   }
   ```
   3. Reschedule job with fixedRate
   ```java
   public final class RescheduleListenerManager extends AbstractListenerManager 
{
       
       .......
       
       class CronSettingAndJobEventChangedJobListener extends 
AbstractJobListener {
           
           @Override
           protected void dataChanged(final String path, final Type eventType, 
final String data) {
               if (configNode.isConfigPath(path) && Type.NODE_CHANGED == 
eventType && !JobRegistry.getInstance().isShutdown(jobName)) {
                   JobConfiguration jobConfiguration = 
YamlEngine.unmarshal(data, JobConfigurationPOJO.class).toJobConfiguration();
                   if (StringUtils.isNotEmpty(jobConfiguration.getCron())) {
                       
JobRegistry.getInstance().getJobScheduleController(jobName).rescheduleJob(jobConfiguration.getCron());
                   } else if(jobConfiguration.getFixedRate() > 0) {
                       //reschedule job with fixedRate
                       
JobRegistry.getInstance().getJobScheduleController(jobName).rescheduleJob(jobConfiguration.getFixedRate(),
 jobConfiguration.getFixedRateTimeUnit());
                   } else {
                       
JobRegistry.getInstance().getJobScheduleController(jobName).rescheduleJob();
                   }
               }
           }
       }
   }
   ```
   4. add method in JobScheduleController
   ```java
   public final class JobScheduleController {
       .....
       /**
        * Schedule FixRate job.
        *
        * @param timeInterval
        * @param timeUnit
        */
       public void scheduleJob(final int timeInterval, final TimeUnit timeUnit) 
{
           try {
               if (!scheduler.checkExists(jobDetail.getKey())) {
                   scheduler.scheduleJob(jobDetail, 
createFixRateTrigger(timeInterval, timeUnit));
               }
               scheduler.start();
           } catch (final SchedulerException ex) {
               throw new JobSystemException(ex);
           }
       }
   
       /**
        * Reschedule FixRate job.
        *
        * @param timeInterval
        * @param timeUnit
        */
       public synchronized void rescheduleJob(int timeInterval, TimeUnit 
timeUnit) {
           try {
               CalendarIntervalTrigger trigger = (CalendarIntervalTrigger) 
scheduler.getTrigger(TriggerKey.triggerKey(triggerIdentity));
               if (!scheduler.isShutdown() && null != trigger) {
                   
scheduler.rescheduleJob(TriggerKey.triggerKey(triggerIdentity), 
createFixRateTrigger(timeInterval, timeUnit));
               }
           } catch (final SchedulerException ex) {
               throw new JobSystemException(ex);
           }
       }
   
       private Trigger createFixRateTrigger(int timeInterval, TimeUnit 
timeUnit) {
           return TriggerBuilder.newTrigger()
                   .withIdentity(triggerIdentity)
                   
.withSchedule(CalendarIntervalScheduleBuilder.calendarIntervalSchedule()
                           .withInterval((int) 
timeUnit.toSeconds(timeInterval), DateBuilder.IntervalUnit.SECOND)
                           .withMisfireHandlingInstructionDoNothing())
                   .build();
       }
       .......
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to