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


   I have used it in my work,hope to add this feature in further.
   
   1. add config properties
   `
   @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
   `
   public final class ScheduleJobBootstrap implements JobBootstrap {
       
       private final JobScheduler jobScheduler;
       
       public ScheduleJobBootstrap(final CoordinatorRegistryCenter regCenter, 
final ElasticJob elasticJob, final JobConfiguration jobConfig) {
           jobScheduler = new JobScheduler(regCenter, elasticJob, jobConfig);
       }
       
       public ScheduleJobBootstrap(final CoordinatorRegistryCenter regCenter, 
final String elasticJobType, final JobConfiguration jobConfig) {
           jobScheduler = new JobScheduler(regCenter, elasticJobType, 
jobConfig);
       }
       
       /**
        * 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.");
           }
       }
       
       @Override
       public void shutdown() {
           jobScheduler.shutdown();
       }
   }
   `
   3. Reschedule job with fixedRate
   `
   public final class RescheduleListenerManager extends AbstractListenerManager 
{
       
       private final ConfigurationNode configNode;
       
       private final String jobName;
       
       public RescheduleListenerManager(final CoordinatorRegistryCenter 
regCenter, final String jobName) {
           super(regCenter, jobName);
           this.jobName = jobName;
           configNode = new ConfigurationNode(jobName);
       }
       
       @Override
       public void start() {
           addDataListener(new CronSettingAndJobEventChangedJobListener());
       }
       
       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();
                   }
               }
           }
       }
   }
   `


-- 
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