xjzrc / elastic-job-lite-spring-boot-starter

elastic-job-lite-spring-boot-starter 让你可以使用spring-boot的方式开发依赖elastic-job的程序
https://github.com/xjzrc/elastic-job-spring-boot-starter
31 stars 18 forks source link
elastic-job-lite spring-boot spring-boot-starter

elastic-job-lite-spring-boot-starter

elastic-job-lite-spring-boot-starter 让你可以使用spring-boot的方式开发依赖elastic-job的程序


使用步骤(示例:「spring-boot-starter-demo」)

根据实际情况依赖最新版本(当前最新版2.0.0)

<dependency>
    <groupId>com.github.xjzrc.spring.boot</groupId>
    <artifactId>elastic-job-lite-spring-boot-starter</artifactId>
    <version>${lasted.release.version}</version>
</dependency>

Simple作业配置

public class SpringSimpleJob implements com.dangdang.ddframe.job.api.simple.SimpleJob {

    @Resource
    private FooRepository fooRepository;

    @Override
    public void execute(final ShardingContext shardingContext) {
        System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s",
                shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "SIMPLE"));
        List<Foo> data = fooRepository.findTodoData(shardingContext.getShardingParameter(), 10);
        for (Foo each : data) {
            fooRepository.setCompleted(each.getId());
        }
    }
}

dataflow作业配置

public class SpringDataflowJob implements com.dangdang.ddframe.job.api.dataflow.DataflowJob<Foo> {

    @Resource
    private FooRepository fooRepository;

    @Override
    public List<Foo> fetchData(final ShardingContext shardingContext) {
        System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s",
                shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "DATAFLOW FETCH"));
        return fooRepository.findTodoData(shardingContext.getShardingParameter(), 10);
    }

    @Override
    public void processData(final ShardingContext shardingContext, final List<Foo> data) {
        System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s",
                shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "DATAFLOW PROCESS"));
        for (Foo each : data) {
            fooRepository.setCompleted(each.getId());
        }
    }
}