Springboot与ElasticJob

yaml配置

 	 	<dependency>
        	<groupId>com.dangdanggroupId>
           <artifactId>elastic-job-lite-springartifactId>
           <version>2.1.5version>
       dependency>
       <dependency>
           <groupId>org.slf4jgroupId>
           <artifactId>slf4j-log4j12artifactId>
       dependency>

配置文件

spring.application.name = elastic-job-springboot
logging.level.root = info

# zookeeper服务地址
zookeeper.connString = localhost:2181
# 名称空间
myjob.namespace = elastic-job-example
# 分片总数
myjob.count = 3
# cron表达式(定时策略)
myjob.cron = 0/5 * * * * ?

Elastic-Job任务类

/**
 * 数据查询任务
 **/
@Component
public class RepaymentJob implements SimpleJob {
    @Autowired
    private RepaymentService repaymentService;   //具体执行的代码

    @Override
    public void execute(ShardingContext shardingContext) {
        repaymentService.executeRepayment(LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE)
                ,shardingContext.getShardingTotalCount()
                ,shardingContext.getShardingItem());

    }
}

zookeeper注册中心 这段配置代码固定不变,直接使用即可

@Configuration
public class ZKRegistryCenterConfig {

	//zookeeper服务地址
    @Value("${zookeeper.connString}")
    private  String ZOOKEEPER_CONNECTION_STRING ;

    //定时任务命名空间
    @Value("${myjob.namespace}")
    private  String JOB_NAMESPACE;

    //zk的配置及创建注册中心
    @Bean(initMethod = "init")
    public  ZookeeperRegistryCenter setUpRegistryCenter(){
        //zk的配置
        ZookeeperConfiguration zookeeperConfiguration = new 
            ZookeeperConfiguration(ZOOKEEPER_CONNECTION_STRING, JOB_NAMESPACE);
        //创建注册中心
        ZookeeperRegistryCenter zookeeperRegistryCenter = new 
            ZookeeperRegistryCenter(zookeeperConfiguration);
        return zookeeperRegistryCenter;
    }
}

elastic-job配置类

@Configuration
public class ElasticJobConfig {

    @Autowired
    private RepaymentJob repaymentJob;

    @Autowired
    ZookeeperRegistryCenter registryCenter;

    @Value("${myjob.count}")
    private int shardingCount;

    @Value("${myjob.cron}")
    private String cron;

    /**
     * 配置任务详细信息
     * @param jobClass 任务执行类
     * @param cron  执行策略
     * @param shardingTotalCount 分片数量
     * @return
     */
    private LiteJobConfiguration createJobConfiguration(final Class<? extends SimpleJob> jobClass,
                                                        final String cron,
                                                        final int shardingTotalCount){
        //创建JobCoreConfigurationBuilder
        JobCoreConfiguration.Builder JobCoreConfigurationBuilder =
                JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount);

        JobCoreConfiguration jobCoreConfiguration = JobCoreConfigurationBuilder.build();
        //创建SimpleJobConfiguration
        SimpleJobConfiguration simpleJobConfiguration =
                new SimpleJobConfiguration(jobCoreConfiguration, jobClass.getCanonicalName());
        //创建LiteJobConfiguration
        LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfiguration).jobShardingStrategyClass("com.dangdang.ddframe.job.lite.api.strategy.impl.AverageAllocationJobShardingStrategy").overwrite(true)
                .build();
        return liteJobConfiguration;
    }

    @Bean(initMethod = "init")
    public SpringJobScheduler initSimpleElasticJob() {
        //创建SpringJobScheduler
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(repaymentJob, registryCenter,createJobConfiguration(repaymentJob.getClass(), cron, shardingCount));
        return springJobScheduler;
    }
}

你可能感兴趣的:(实际工作,spring,boot,后端,java)