ElasticJob-Lite provides a custom Spring namespace, which can be used with the Spring. Through the way of DI (Dependency Injection), developers can easily use data sources and other objects that managed by the Spring container in their jobs, and use placeholders to get values ​​from property files.

Job Configuration

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:elasticjob="http://shardingsphere.apache.org/schema/elasticjob"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://shardingsphere.apache.org/schema/elasticjob
  8. http://shardingsphere.apache.org/schema/elasticjob/elasticjob.xsd
  9. ">
  10. <!-- Configure registry center for job -->
  11. <elasticjob:zookeeper id="regCenter" server-lists="yourhost:2181" namespace="my-job" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" />
  12. <!-- Configure job java bean -->
  13. <bean id="myJob" class="xxx.MyJob">
  14. <property name="fooService" ref="xxx.FooService" />
  15. </bean>
  16. <!-- Configure job scheduler base on java bean -->
  17. <elasticjob:job id="${myJob.id}" job-ref="myJob" registry-center-ref="regCenter" sharding-total-count="${myJob.shardingTotalCount}" cron="${myJob.cron}" />
  18. <!-- Configure job scheduler base on type -->
  19. <elasticjob:job id="${myScriptJob.id}" job-type="SCRIPT" registry-center-ref="regCenter" sharding-total-count="${myScriptJob.shardingTotalCount}" cron="${myScriptJob.cron}">
  20. <props>
  21. <prop key="script.command.line">${myScriptJob.scriptCommandLine}</prop>
  22. </props>
  23. </elasticjob:job>
  24. </beans>

Job Start

Schedule Job

If the Spring container start, the XML that configures the Spring namespace will be loaded, and the job will be automatically 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.

  1. <bean id="oneOffJob" class="org.apache.shardingsphere.elasticjob.lite.example.job.simple.SpringSimpleJob" />
  2. <elasticjob:job id="oneOffJobBean" job-ref="oneOffJob" ... />
  1. public final class SpringMain {
  2. public static void main(final String[] args) {
  3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/application-context.xml");
  4. OneOffJobBootstrap oneOffJobBootstrap = context.getBean("oneOffJobBean", OneOffJobBootstrap.class);
  5. oneOffJobBootstrap.execute();
  6. }
  7. }

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 SnapshotService for open listener port to dump.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:elasticjob="http://shardingsphere.apache.org/schema/elasticjob"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://shardingsphere.apache.org/schema/elasticjob
  8. http://shardingsphere.apache.org/schema/elasticjob/elasticjob.xsd
  9. ">
  10. <!--Create registry center -->
  11. <elasticjob:zookeeper id="regCenter" server-lists="yourhost:2181" namespace="dd-job" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" />
  12. <!--Configure the task snapshot export service -->
  13. <elasticjob:snapshot id="jobSnapshot" registry-center-ref="regCenter" dump-port="9999" />
  14. </beans>

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

The following example shows how to configure the error-handling policy through the Spring namespace.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:elasticjob="http://shardingsphere.apache.org/schema/elasticjob"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://shardingsphere.apache.org/schema/elasticjob
  8. http://shardingsphere.apache.org/schema/elasticjob/elasticjob.xsd
  9. ">
  10. <!-- Log Strategy -->
  11. <elasticjob:job ... job-error-handler-type="LOG" />
  12. <!-- Throw Strategy -->
  13. <elasticjob:job ... job-error-handler-type="THROW" />
  14. <!-- Ignore Strategy -->
  15. <elasticjob:job ... job-error-handler-type="IGNORE" />
  16. <!-- Email Notification Strategy -->
  17. <elasticjob:job ... job-error-handler-type="EMAIL">
  18. <props>
  19. <prop key="email.host">${host}</prop>
  20. <prop key="email.port">${port}</prop>
  21. <prop key="email.username">${username}</prop>
  22. <prop key="email.password">${password}</prop>
  23. <prop key="email.useSsl">${useSsl}</prop>
  24. <prop key="email.subject">${subject}</prop>
  25. <prop key="email.from">${from}</prop>
  26. <prop key="email.to">${to}</prop>
  27. <prop key="email.cc">${cc}</prop>
  28. <prop key="email.bcc">${bcc}</prop>
  29. <prop key="email.debug">${debug}</prop>
  30. </props>
  31. </elasticjob:job>
  32. <!-- Wechat Enterprise Notification Strategy -->
  33. <elasticjob:job ... job-error-handler-type="WECHAT">
  34. <props>
  35. <prop key="wechat.webhook">${webhook}</prop>
  36. <prop key="wechat.connectTimeoutMilliseconds">${connectTimeoutMilliseconds}</prop>
  37. <prop key="wechat.readTimeoutMilliseconds">${readTimeoutMilliseconds}</prop>
  38. </props>
  39. </elasticjob:job>
  40. <!-- Dingtalk Notification Strategy -->
  41. <elasticjob:job ... job-error-handler-type="DINGTALK">
  42. <props>
  43. <prop key="dingtalk.webhook">${webhook}</prop>
  44. <prop key="dingtalk.keyword">${keyword}</prop>
  45. <prop key="dingtalk.secret">${secret}</prop>
  46. <prop key="dingtalk.connectTimeoutMilliseconds">${connectTimeoutMilliseconds}</prop>
  47. <prop key="dingtalk.readTimeoutMilliseconds">${readTimeoutMilliseconds}</prop>
  48. </props>
  49. </elasticjob:job>
  50. </beans>