添加依赖
<dependency> <groupId>org.apache.shardingsphere.elasticjob</groupId> <artifactId>elasticjob-lite-core</artifactId> <version>3.0.0-alpha</version> </dependency>
配置文件添加
elasticJob: regCenter: serverLists: localhost:2181 namespace: live-task elastic-job: jobs: - jobName: testJob cron: '*/5 * * * * ?'
添加ElasticJob配置类:
注册中心
package com.wjc.server.config; import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration; import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author wjc * @description * @date 2020/10/23 */ @Configuration public class ElasticRegCenterConfig { @Bean(initMethod = "init") public CoordinatorRegistryCenter regCenter( @Value("${elasticJob.regCenter.serverLists}") final String serverList, @Value("${elasticJob.regCenter.namespace}") final String namespace) { return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace)); } }
配置对应实体类
package com.wjc.server.config; import lombok.Data; /** * @author wjc * @description * @date 2020/10/23 */ @Data public class JobEntity { /** * 任务名称 */ private String jobName; /** * 分片数量 */ private int shardingTotalCount = 1; /** * cron表达式 */ private String cron; /** * 当前分片参数 */ private String shardingItemParameters; /** * 当前任务参数 */ private String jobParameter; /** * 任务类型 0.SimpleJob 1.DataflowJob 2.ScriptJob */ private Integer type = 0; /** * 任务说明 */ private String description; }
获取任务配置
package com.wjc.server.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; /** * @author wjc * @description * @date 2020/10/23 */ @Data @Component @ConfigurationProperties(prefix = "elastic-job") public class JobEntityConfig { private List<JobEntity> jobs = new ArrayList<>(); }
最终配置类
package com.wjc.server.config; import org.apache.shardingsphere.elasticjob.api.ElasticJob; import org.apache.shardingsphere.elasticjob.api.JobConfiguration; import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap; import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.List; /** * @author wjc * @description * @date 2020/10/23 */ @Configuration public class ElasticJobConfig { @Autowired private CoordinatorRegistryCenter regCenter; @Autowired private ApplicationContext applicationContext; @Autowired private JobEntityConfig jobEntityConfig; @PostConstruct public void executor(){ List<JobConfiguration> list = getJobConfiguration(); list.forEach(job -> new ScheduleJobBootstrap(regCenter, (ElasticJob)applicationContext.getBean(job.getJobName()), job).schedule()); } public List<JobConfiguration> getJobConfiguration(){ List<JobEntity> jobEntityList = jobEntityConfig.getJobs(); List<JobConfiguration> list = new ArrayList<>(jobEntityList.size()); jobEntityList.forEach(jobEntity -> list.add(JobConfiguration.newBuilder(jobEntity.getJobName(), jobEntity.getShardingTotalCount()).cron(jobEntity.getCron()).description(jobEntity.getDescription()).shardingItemParameters(jobEntity.getShardingItemParameters()).build())); return list; } }