Job configuration

ElasticJob-Lite uses the builder mode to create job configuration objects. The code example is as follows:

  1. JobConfiguration jobConfig = JobConfiguration.newBuilder("myJob", 3).cron("0/5 * * * * ?").shardingItemParameters("0=Beijing,1=Shanghai,2=Guangzhou").build();

Job start

ElasticJob-Lite scheduler is divided into two types: timed scheduling and one-time scheduling. Each scheduler needs three parameters: registry configuration, job object (or job type), and job configuration when it starts.

Timed scheduling

  1. public class JobDemo {
  2. public static void main(String[] args) {
  3. // Class-based Scheduling Jobs
  4. new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createJobConfiguration()).schedule();
  5. // Type-based Scheduling Jobs
  6. new ScheduleJobBootstrap(createRegistryCenter(), "MY_TYPE", createJobConfiguration()).schedule();
  7. }
  8. private static CoordinatorRegistryCenter createRegistryCenter() {
  9. CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration("zk_host:2181", "elastic-job-demo"));
  10. regCenter.init();
  11. return regCenter;
  12. }
  13. private static JobConfiguration createJobConfiguration() {
  14. // Create job configuration
  15. ...
  16. }
  17. }

One-Off scheduling

  1. public class JobDemo {
  2. public static void main(String[] args) {
  3. OneOffJobBootstrap jobBootstrap = new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createJobConfiguration());
  4. // One-time scheduling can be called multiple times
  5. jobBootstrap.execute();
  6. jobBootstrap.execute();
  7. jobBootstrap.execute();
  8. }
  9. private static CoordinatorRegistryCenter createRegistryCenter() {
  10. CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration("zk_host:2181", "elastic-job-demo"));
  11. regCenter.init();
  12. return regCenter;
  13. }
  14. private static JobConfiguration createJobConfiguration() {
  15. // Create job configuration
  16. ...
  17. }
  18. }

Job Dump

Using ElasticJob may meet some distributed problem which is not easy to observe.

Because of developer can not debug in production environment, ElasticJob provide dump command to export job runtime information for debugging.

Please refer to Operation Manual for more details.

The example below is how to configure spring namespace for open listener port to dump.

  1. public class JobMain {
  2. public static void main(final String[] args) {
  3. SnapshotService snapshotService = new SnapshotService(regCenter, 9888).listen();
  4. }
  5. private static CoordinatorRegistryCenter createRegistryCenter() {
  6. // create registry center
  7. }
  8. }

Configuration error handler strategy

In the process of using ElasticJob-Lite, when the job is abnormal, the following error handling strategies can be used.

Error handler strategy nameDescriptionBuilt-inDefaultExtra config
Log StrategyLog error and do not interrupt jobYesYes
Throw StrategyThrow system exception and interrupt jobYes
Ignore StrategyIgnore exception and do not interrupt jobYes
Email Notification StrategySend email message notification and do not interrupt jobYes
Wechat Enterprise Notification StrategySend wechat message notification and do not interrupt jobYes
Dingtalk Notification StrategySend dingtalk message notification and do not interrupt jobYes

Log Strategy

  1. public class JobDemo {
  2. public static void main(String[] args) {
  3. // Scheduling Jobs
  4. new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
  5. // One-time Scheduling Jobs
  6. new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
  7. }
  8. private static JobConfiguration createScheduleJobConfiguration() {
  9. // Create scheduling job configuration, and the use of log strategy
  10. return JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("LOG").build();
  11. }
  12. private static JobConfiguration createOneOffJobConfiguration() {
  13. // Create one-time job configuration, and the use of log strategy
  14. return JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("LOG").build();
  15. }
  16. private static CoordinatorRegistryCenter createRegistryCenter() {
  17. // create registry center
  18. ...
  19. }
  20. }

Throw Strategy

  1. public class JobDemo {
  2. public static void main(String[] args) {
  3. // Scheduling Jobs
  4. new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
  5. // One-time Scheduling Jobs
  6. new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
  7. }
  8. private static JobConfiguration createScheduleJobConfiguration() {
  9. // Create scheduling job configuration, and the use of throw strategy.
  10. return JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("THROW").build();
  11. }
  12. private static JobConfiguration createOneOffJobConfiguration() {
  13. // Create one-time job configuration, and the use of throw strategy
  14. return JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("THROW").build();
  15. }
  16. private static CoordinatorRegistryCenter createRegistryCenter() {
  17. // create registry center
  18. ...
  19. }
  20. }

Ignore Strategy

  1. public class JobDemo {
  2. public static void main(String[] args) {
  3. // Scheduling Jobs
  4. new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
  5. // One-time Scheduling Jobs
  6. new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
  7. }
  8. private static JobConfiguration createScheduleJobConfiguration() {
  9. // Create scheduling job configuration, and the use of ignore strategy.
  10. return JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("IGNORE").build();
  11. }
  12. private static JobConfiguration createOneOffJobConfiguration() {
  13. // Create one-time job configuration, and the use of ignore strategy.
  14. return JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("IGNORE").build();
  15. }
  16. private static CoordinatorRegistryCenter createRegistryCenter() {
  17. // create registry center.
  18. ...
  19. }
  20. }

Email Notification Strategy

Please refer to here for more details.

Maven POM:

  1. <dependency>
  2. <groupId>org.apache.shardingsphere.elasticjob</groupId>
  3. <artifactId>elasticjob-error-handler-email</artifactId>
  4. <version>${latest.release.version}</version>
  5. </dependency>
  1. public class JobDemo {
  2. public static void main(String[] args) {
  3. // Scheduling Jobs
  4. new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
  5. // One-time Scheduling Jobs
  6. new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
  7. }
  8. private static JobConfiguration createScheduleJobConfiguration() {
  9. // Create scheduling job configuration, and the use of email notification strategy.
  10. JobConfiguration jobConfig = JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("EMAIL").build();
  11. setEmailProperties(jobConfig);
  12. return jobConfig;
  13. }
  14. private static JobConfiguration createOneOffJobConfiguration() {
  15. // Create one-time job configuration, and the use of email notification strategy.
  16. JobConfiguration jobConfig = JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("EMAIL").build();
  17. setEmailProperties(jobConfig);
  18. return jobConfig;
  19. }
  20. private static void setEmailProperties(final JobConfiguration jobConfig) {
  21. // Set the mail configuration.
  22. jobConfig.getProps().setProperty(EmailPropertiesConstants.HOST, "host");
  23. jobConfig.getProps().setProperty(EmailPropertiesConstants.PORT, "465");
  24. jobConfig.getProps().setProperty(EmailPropertiesConstants.USERNAME, "username");
  25. jobConfig.getProps().setProperty(EmailPropertiesConstants.PASSWORD, "password");
  26. jobConfig.getProps().setProperty(EmailPropertiesConstants.FROM, "from@xxx.xx");
  27. jobConfig.getProps().setProperty(EmailPropertiesConstants.TO, "to1@xxx.xx,to1@xxx.xx");
  28. }
  29. private static CoordinatorRegistryCenter createRegistryCenter() {
  30. // create registry center.
  31. ...
  32. }
  33. }

Wechat Enterprise Notification Strategy

Please refer to here for more details.

Maven POM:

  1. <dependency>
  2. <groupId>org.apache.shardingsphere.elasticjob</groupId>
  3. <artifactId>elasticjob-error-handler-wechat</artifactId>
  4. <version>${latest.release.version}</version>
  5. </dependency>
  1. public class JobDemo {
  2. public static void main(String[] args) {
  3. // Scheduling Jobs.
  4. new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
  5. // One-time Scheduling Jobs.
  6. new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
  7. }
  8. private static JobConfiguration createScheduleJobConfiguration() {
  9. // Create scheduling job configuration, and the use of wechat enterprise notification strategy.
  10. JobConfiguration jobConfig = JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("WECHAT").build();
  11. setWechatProperties(jobConfig);
  12. return jobConfig;
  13. }
  14. private static JobConfiguration createOneOffJobConfiguration() {
  15. // Create one-time job configuration, and the use of wechat enterprise notification strategy.
  16. JobConfiguration jobConfig = JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("WECHAT").build();
  17. setWechatProperties(jobConfig);
  18. return jobConfig;
  19. }
  20. private static void setWechatProperties(final JobConfiguration jobConfig) {
  21. // Set the configuration for the enterprise wechat.
  22. jobConfig.getProps().setProperty(WechatPropertiesConstants.WEBHOOK, "you_webhook");
  23. }
  24. private static CoordinatorRegistryCenter createRegistryCenter() {
  25. // create registry center.
  26. ...
  27. }
  28. }

Dingtalk Notification Strategy

Please refer to here for more details.

Maven POM:

  1. <dependency>
  2. <groupId>org.apache.shardingsphere.elasticjob</groupId>
  3. <artifactId>elasticjob-error-handler-dingtalk</artifactId>
  4. <version>${latest.release.version}</version>
  5. </dependency>
  1. public class JobDemo {
  2. public static void main(String[] args) {
  3. // Scheduling Jobs.
  4. new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
  5. // One-time Scheduling Jobs.
  6. new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
  7. }
  8. private static JobConfiguration createScheduleJobConfiguration() {
  9. // Create scheduling job configuration, and the use of dingtalk notification strategy.
  10. JobConfiguration jobConfig = JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("DINGTALK").build();
  11. setDingtalkProperties(jobConfig);
  12. return jobConfig;
  13. }
  14. private static JobConfiguration createOneOffJobConfiguration() {
  15. // Create one-time job configuration, and the use of dingtalk notification strategy.
  16. JobConfiguration jobConfig = JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("DINGTALK").build();
  17. setDingtalkProperties(jobConfig);
  18. return jobConfig;
  19. }
  20. private static void setDingtalkProperties(final JobConfiguration jobConfig) {
  21. // Set the configuration of the dingtalk.
  22. jobConfig.getProps().setProperty(DingtalkPropertiesConstants.WEBHOOK, "you_webhook");
  23. jobConfig.getProps().setProperty(DingtalkPropertiesConstants.KEYWORD, "you_keyword");
  24. jobConfig.getProps().setProperty(DingtalkPropertiesConstants.SECRET, "you_secret");
  25. }
  26. private static CoordinatorRegistryCenter createRegistryCenter() {
  27. // create registry center.
  28. ...
  29. }
  30. }