elastic job demo

其他技术

2018-09-15

44

0

技术:elastic job + java8

运行环境:IDEA 15.2 + jdk8 + windows 10

demo功能:提供一个elastic job 简单demo

假设您已经安装了

添加maven 依赖


	com.dangdang
	elastic-job-lite-core
	2.1.5

添加简单job

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;

/**
 * @author liwei5.
 */
public class MySimpleJob implements SimpleJob {
    @Override
    public void execute(ShardingContext shardingContext) {
        System.out.println("我在执行任务...");
    }
}

注册、配置job

public class Startup {
    public static void main(String[] args) {
        new JobScheduler(registryCenter(), configuration()).init();
    }

    //注册job
    private static CoordinatorRegistryCenter registryCenter() {
        String serverLists = "127.0.0.1:2181";
        String namespace = "elastic-job";
        CoordinatorRegistryCenter registryCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverLists, namespace));
        registryCenter.init();
        return registryCenter;
    }

    //添加job 配置
    private static LiteJobConfiguration configuration() {
        String jobName = "simpleJob";
        String cron = "0/15 * * * * ?";
        int shardingTotalCount = 3;
        JobCoreConfiguration coreConfiguration = JobCoreConfiguration.newBuilder(jobName, cron, shardingTotalCount).build();
        String jobClass = MySimpleJob.class.getCanonicalName();
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(coreConfiguration, jobClass);

        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration
                .newBuilder(simpleJobConfiguration)
                .jobShardingStrategyClass(AverageAllocationJobShardingStrategy.class.getCanonicalName())
                .build();
        return simpleJobRootConfig;
    }
}

运行job

 

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think