快速入门

1. 使用API配置启动

引入maven依赖

<dependency>
    <groupId>org.apache.shardingsphere.elasticjob</groupId>
    <artifactId>elastic-job-lite-core</artifactId>
    <version>${latest.release.version}</version>
</dependency>

作业开发

public class MyElasticJob implements SimpleJob {
    
    @Override
    public void execute(ShardingContext context) {
        switch (context.getShardingItem()) {
            case 0: 
                // do something by sharding item 0
                break;
            case 1: 
                // do something by sharding item 1
                break;
            case 2: 
                // do something by sharding item 2
                break;
            // case n: ...
        }
    }
}

作业配置

    // 定义作业核心配置
    JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder("demoSimpleJob", "0/15 * * * * ?", 10).build();
    // 定义 SIMPLE 类型配置
    SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, SimpleDemoJob.class.getCanonicalName());
    // 定义作业根配置
    JobConfiguration jobConfig = JobConfiguration.newBuilder(simpleJobConfig).build();

启动作业

public class JobDemo {
    
    public static void main(String[] args) {
        new JobScheduler(createRegistryCenter(), createJobConfiguration()).init();
    }
    
    private static CoordinatorRegistryCenter createRegistryCenter() {
        CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration("zk_host:2181", "elastic-job-demo"));
        regCenter.init();
        return regCenter;
    }
    
    private static JobConfiguration createJobConfiguration() {
        // 创建作业配置
        // ...
    }
}

2. 使用Spring配置启动

<dependency>
    <groupId>org.apache.shardingsphere.elasticjob</groupId>
    <artifactId>elastic-job-lite-spring</artifactId>
    <version>${latest.release.version}</version>
</dependency>

作业开发

同使用API配置中作业开发

作业配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:reg="http://elasticjob.shardingsphere.apache.org/schema/reg"
    xmlns:job="http://elasticjob.shardingsphere.apache.org/schema/job"
    xmlns:monitor="http://elasticjob.shardingsphere.apache.org/schema/monitor"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://elasticjob.shardingsphere.apache.org/schema/reg
                        http://elasticjob.shardingsphere.apache.org/schema/reg/reg.xsd
                        http://elasticjob.shardingsphere.apache.org/schema/job
                        http://elasticjob.shardingsphere.apache.org/schema/job/job.xsd
                        http://elasticjob.shardingsphere.apache.org/schema/monitor
                        http://elasticjob.shardingsphere.apache.org/schema/monitor/monitor.xsd
                        ">
    <!--配置作业注册中心 -->
    <reg:zookeeper id="regCenter" server-lists="yourhost:2181" namespace="elastic-job" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" />
   
    <!--配置任务监控 -->
    <monitor:embed id="monitor1" registry-center-ref="regCenter" monitor-port="9999"/>    

    <!--配置作业类 -->
    <bean id="simpleJob" class="xxx.MyElasticJob" />
    
    <!--配置作业 -->
    <job:simple id="oneOffElasticJob" job-ref="simpleJob" registry-center-ref="regCenter" reconcile-interval-minutes="10" cron="0/10 * * * * ?" sharding-total-count="3" sharding-item-parameters="0=A,1=B,2=C" />
</beans>

启动作业

将配置Spring命名空间的xml通过Spring启动,作业将自动加载。

详细的规则配置请参考配置手册