zookeeper注册中心配置
1 package com.zwh.pay.account.worker; 2 3 import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration; 4 import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; 5 import org.springframework.beans.factory.annotation.Value; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.Configuration; 8 9 /** 10 * Created by Administrator on 2017/7/10. 11 */ 12 @Configuration 13 public class RegistryCenterConfig { 14 15 @Bean(initMethod = "init") 16 public ZookeeperRegistryCenter createZookeeperRegistryCenter(@Value("${regCenter.serverList}") final String serverLists, @Value("${regCenter.namespace}") final String namespace){ 17 ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration 18 (serverLists,namespace); 19 return new ZookeeperRegistryCenter(zookeeperConfiguration); 20 } 21 }
开启事件追踪器
1 package com.zwh.pay.account.worker; 2 3 import com.dangdang.ddframe.job.event.JobEventConfiguration; 4 import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.Configuration; 8 9 import javax.sql.DataSource; 10 11 /** 12 * Created by Administrator on 2017/7/10. 13 */ 14 @Configuration 15 public class JobEventConfig { 16 17 @Autowired 18 private DataSource dataSource; 19 20 @Bean("jobEventConfiguration") 21 public JobEventConfiguration createJobEventConfiguration(){ 22 return new JobEventRdbConfiguration(dataSource); 23 } 24 }
配置任务启动类
1 package com.zwh.pay.account.worker; 2 3 import com.dangdang.ddframe.job.config.JobCoreConfiguration; 4 import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration; 5 import com.dangdang.ddframe.job.event.JobEventConfiguration; 6 import com.dangdang.ddframe.job.lite.api.JobScheduler; 7 import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration; 8 import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler; 9 import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.beans.factory.annotation.Value; 12 import org.springframework.context.annotation.Bean; 13 import org.springframework.context.annotation.Configuration; 14 15 /** 16 * 17 * ClassName: JobStart <br/> 18 * Function: TODO 定时任务启动类. <br/> 19 * Reason: TODO ADD REASON(可选). <br/> 20 * date: 2017年9月30日 上午10:31:26 <br/> 21 * 22 * @author zhangwenhao 23 * @version 1.0 24 * @since JDK 1.8 25 */ 26 @Configuration 27 public class JobStart { 28 29 @Autowired 30 private ZookeeperRegistryCenter zookeeperRegistryCenter; 31 32 @Autowired 33 private JobEventConfiguration jobEventConfiguration; 34 35 @Bean("fundCarryJob") 36 public FundCarryJob fundCarryJob() { 37 return new FundCarryJob(); 38 } 39 40 private LiteJobConfiguration getLiteJobConfiguration(String cron, int 41 shardingTotalCount, @SuppressWarnings("rawtypes") Class clazz) { 42 return LiteJobConfiguration.newBuilder( 43 new SimpleJobConfiguration( 44 JobCoreConfiguration.newBuilder( 45 clazz.getName(), cron, 46 shardingTotalCount).build(), 47 clazz.getCanonicalName())) 48 .overwrite(true).build(); 49 } 50 51 @Bean(initMethod = "init", name = "fundCarryJobScheduler") 52 public JobScheduler fundCarryJobScheduler(final FundCarryJob fundCarryJob, 53 @Value("${fundCarryJob.cron}") String cron, 54 @Value("${fundCarryJob.shardingTotalCount}") 55 int shardingTotalCount) { 56 57 return new SpringJobScheduler(fundCarryJob, 58 zookeeperRegistryCenter, 59 getLiteJobConfiguration(cron, shardingTotalCount, 60 FundCarryJob.class), jobEventConfiguration); 61 } 62 63 }
定时任务执行
package com.zwh.pay.account.worker; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import com.dangdang.ddframe.job.api.ShardingContext; import com.dangdang.ddframe.job.api.simple.SimpleJob; import com.hivescm.pay.account.service.AccountSeqFlowService; /** * ClassName:FundCarryJob <br/> * Function: TODO 定时任务执行. <br/> * Reason: TODO ADD REASON. <br/> * Date: 2017年9月29日 下午5:55:51 <br/> * @author zhangwenhao * @version 1.0 * @since JDK 1.8 * @see */ public class FundCarryJob implements SimpleJob{ private Logger logger = LoggerFactory.getLogger(FundCarryJob.class); @Autowired @Override public void execute(ShardingContext arg0) { } }
配置文件
1 #elastic job config 2 regCenter.serverList=10.12.31.100:2181,10.12.31.105:2181,10.12.31.109:2181 3 regCenter.namespace=fundCarryJob 4 5 fundCarryJob.cron=*/5 * * * * ? 6 #fundCarryJob.cron=0 0 0 * * ? 7 fundCarryJob.shardingTotalCount=1