1、首先添加引用
2、添加配置文件信息。
server: port: 8081 elasticjob: regCenter: serverLists: 127.0.0.1:2181 namespace: elasticjob-lite-springboot job: simpleJob: cron: 0/5 * * * * ? shardingTotalCount: 3 shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou
3、配置zk的注册中心。
package com.uwith.elasticjob.config; import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration; import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnExpression("'${elasticjob.regCenter.serverLists}'.length() > 0") public class RegistryCenterConfig {
@Bean(initMethod \= "init")
public ZookeeperRegistryCenter regCenter(@Value("${elasticjob.regCenter.serverLists}") final String serverList, @Value("${elasticjob.regCenter.namespace}") final String namespace) {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
} }
4、配置一个简单job任务。
package com.uwith.elasticjob.config; import com.dangdang.ddframe.job.api.simple.SimpleJob; import com.dangdang.ddframe.job.config.JobCoreConfiguration; import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration; import com.dangdang.ddframe.job.lite.api.JobScheduler; import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration; import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler; import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class SimpleJobConfig {
@Autowired
private ZookeeperRegistryCenter regCenter; @Bean(initMethod = "init") public JobScheduler simpleJobScheduler(final SimpleJob simpleJob, @Value("${elasticjob.job.simpleJob.cron}") final String cron, @Value("${elasticjob.job.simpleJob.shardingTotalCount}") final int shardingTotalCount, @Value("${elasticjob.job.simpleJob.shardingItemParameters}") final String shardingItemParameters) { return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters)); }
private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {
// 定义作业核心配置
JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount). shardingItemParameters(shardingItemParameters).build(); // 定义SIMPLE类型配置 SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, jobClass.getCanonicalName()); // 定义Lite作业根配置 LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build(); return simpleJobRootConfig; } }
5、添加一个任务的执行类。
package com.uwith.elasticjob.job; import com.dangdang.ddframe.job.api.ShardingContext; import com.dangdang.ddframe.job.api.simple.SimpleJob; import org.springframework.stereotype.Component; @Component public class MyJob implements SimpleJob {
@Override
public void execute(ShardingContext shardingContext) { System.out.println(shardingContext.getShardingItem()); System.out.println(shardingContext.getShardingParameter()); } }
如果多个job执行的话,首先需要添加一个job类,也就是第5步操作。然后添加一个job配置类,也就是第4步操作。需要注意:
1、simpleJobScheduler方法得换一个,因为注入时相同会有问题。
2、如果多个job,simpleJobScheduler方法的参数(SimpleJob)simpleJob就不能随便写了,得写具体的job类,来区分完成注入。
zookeeper可参考: https://my.oschina.net/uwith/blog/3196640
elastic-job官网: https://shardingsphere.apache.org/elasticjob/current/cn/overview/