ElasticJob-Lite provides a customized Spring Boot Starter, which can be used in conjunction with Spring Boot. Developers are free from configuring CoordinatorRegistryCenter, JobBootstrap by using ElasticJob Spring Boot Starter. What developers need to solve distributed scheduling problem are job implementations with a little configuration.

Job configuration

Implements ElasticJob

Job implementation is similar to other usage of ElasticJob. The difference is that jobs will be registered into the Spring IoC container.

Thread-Safety Issue

Bean is singleton by default. Consider setting Bean Scope to prototype if the instance of ElasticJob would be used by more than a JobBootstrap.

  1. @Component
  2. public class SpringBootDataflowJob implements DataflowJob<Foo> {
  3. @Override
  4. public List<Foo> fetchData(final ShardingContext shardingContext) {
  5. // fetch data
  6. }
  7. @Override
  8. public void processData(final ShardingContext shardingContext, final List<Foo> data) {
  9. // process data
  10. }
  11. }

Configure CoordinateRegistryCenter and Jobs

Configure the Zookeeper which will be used by ElasticJob via configuration files.

elasticjob.jobs is a Map. Using key as job name. Specific job type and configuration in value. The Starter will create instances of OneOffJobBootstrap or ScheduleJobBootstrap and register them into the Spring IoC container automatically.

Configuration reference:

  1. elasticjob:
  2. regCenter:
  3. serverLists: localhost:6181
  4. namespace: elasticjob-lite-springboot
  5. jobs:
  6. dataflowJob:
  7. elasticJobClass: org.apache.shardingsphere.elasticjob.dataflow.job.DataflowJob
  8. cron: 0/5 * * * * ?
  9. shardingTotalCount: 3
  10. shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou
  11. scriptJob:
  12. elasticJobType: SCRIPT
  13. cron: 0/10 * * * * ?
  14. shardingTotalCount: 3
  15. props:
  16. script.command.line: "echo SCRIPT Job: "

Job Start

Schedule Job

Just start Spring Boot Starter directly. The schedule jobs will startup when the Spring Boot Application is started.

One-off Job

When to execute OneOffJob is up to you. Developers can inject the OneOffJobBootstrap bean into where they plan to invoke. Trigger the job by invoking execute() method manually.

The bean name of OneOffJobBootstrap is specified by property “jobBootstrapBeanName”, Please refer to Spring Boot Starter Configuration.

  1. elasticjob:
  2. jobs:
  3. myOneOffJob:
  4. jobBootstrapBeanName: myOneOffJobBean
  5. ....
  1. @RestController
  2. public class OneOffJobController {
  3. // Inject via "@Resource"
  4. @Resource(name = "myOneOffJobBean")
  5. private OneOffJobBootstrap myOneOffJob;
  6. @GetMapping("/execute")
  7. public String executeOneOffJob() {
  8. myOneOffJob.execute();
  9. return "{\"msg\":\"OK\"}";
  10. }
  11. // Inject via "@Autowired"
  12. @Autowired
  13. @Qualifier(name = "myOneOffJobBean")
  14. private OneOffJobBootstrap myOneOffJob2;
  15. @GetMapping("/execute2")
  16. public String executeOneOffJob2() {
  17. myOneOffJob2.execute();
  18. return "{\"msg\":\"OK\"}";
  19. }
  20. }

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. elasticjob:
  2. regCenter:
  3. ...
  4. jobs:
  5. ...
  6. jobErrorHandlerType: LOG

Throw Strategy

  1. elasticjob:
  2. regCenter:
  3. ...
  4. jobs:
  5. ...
  6. jobErrorHandlerType: THROW

Ignore Strategy

  1. elasticjob:
  2. regCenter:
  3. ...
  4. jobs:
  5. ...
  6. jobErrorHandlerType: IGNORE

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. elasticjob:
  2. regCenter:
  3. ...
  4. jobs:
  5. ...
  6. jobErrorHandlerType: EMAIL
  7. props:
  8. email:
  9. host: host
  10. port: 465
  11. username: username
  12. password: password
  13. useSsl: true
  14. subject: ElasticJob error message
  15. from: from@xxx.xx
  16. to: to1@xxx.xx,to2@xxx.xx
  17. cc: cc@xxx.xx
  18. bcc: bcc@xxx.xx
  19. debug: false

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. elasticjob:
  2. regCenter:
  3. ...
  4. jobs:
  5. ...
  6. jobErrorHandlerType: WECHAT
  7. props:
  8. wechat:
  9. webhook: you_webhook
  10. connectTimeout: 3000
  11. readTimeout: 5000

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. elasticjob:
  2. regCenter:
  3. ...
  4. jobs:
  5. ...
  6. jobErrorHandlerType: DINGTALK
  7. props:
  8. dingtalk:
  9. webhook: you_webhook
  10. keyword: you_keyword
  11. secret: you_secret
  12. connectTimeout: 3000
  13. readTimeout: 5000