Import Maven Dependency

  1. <dependency>
  2. <groupId>org.apache.shardingsphere.elasticjob</groupId>
  3. <artifactId>elasticjob-lite-spring-namespace</artifactId>
  4. <version>${elasticjob.latest.version}</version>
  5. </dependency>

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. <!-- Configure DataSource -->
  15. <bean id="tracingDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
  16. <property name="driverClassName" value="${driver.class.name}" />
  17. <property name="url" value="${url}" />
  18. <property name="username" value="${username}" />
  19. <property name="password" value="${password}" />
  20. </bean>
  21. <!-- Configure event tracing -->
  22. <elasticjob:rdb-tracing id="elasticJobTrace" data-source-ref="elasticJobTracingDataSource" />
  23. <!-- Configure job -->
  24. <elasticjob:job id="${myJob.id}" job-ref="myJob" registry-center-ref="regCenter" tracing-ref="elasticJobTrace" sharding-total-count="3" cron="0/1 * * * * ?" />
  25. </beans>

Job Start

If the Spring container start, the XML that configures the Spring namespace will be loaded, and the job will be automatically started.