Java 分布式定时任务-dangdang



   com.dangdang
   elastic-job-lite-core
   ${elastic-job.version}
   
      
         curator-client
         org.apache.curator
      
      
         curator-framework
         org.apache.curator
      
      
         curator-recipes
         org.apache.curator
      
   


   com.dangdang
   elastic-job-lite-spring
   ${elastic-job.version}

@Configuration
public class ElasticRegCenterConfig {

    /**
     * 配置zookeeper
     *
     * @param serverList
     * @param namespace
     * @return
     */
    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter regCenter(
            @Value("${regCenter.serverList}") final String serverList,
            @Value("${regCenter.namespace}") final String namespace,
            @Value("${regCenter.maxRetries}") final Integer maxRetries,
            @Value("${regCenter.baseSleepTimeMilliseconds}") final Integer baseSleepTimeMilliseconds,
            @Value("${regCenter.maxSleepTimeMilliseconds}") final Integer maxSleepTimeMilliseconds
            ) {
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(serverList, namespace);
        //尝试连接次数
        zookeeperConfiguration.setMaxRetries(maxRetries);
        //http://elasticjob.io/docs/elastic-job-lite/02-guide/config-manual/
        //等待重试的间隔时间的初始值 //单位:毫秒
        zookeeperConfiguration.setBaseSleepTimeMilliseconds(baseSleepTimeMilliseconds);
        //等待重试的间隔时间的最大值  //单位:毫秒
        zookeeperConfiguration.setMaxSleepTimeMilliseconds(maxSleepTimeMilliseconds);
        return new ZookeeperRegistryCenter(zookeeperConfiguration);
    } 
}

====================  

xx.cron = 0 0/10 * * * ? 
@Slf4j
@RefreshScope
@Configuration
public class ElasticJobConfig {
    @Autowired
    private ZookeeperRegistryCenter regCenter;

    @Bean(initMethod = "init")
    public JobScheduler refreshTimeJobScheduler(ElsJObEvent1 simpleJob,
                                                @Value("${xx.cron}") String cron) {
        return new SpringJobScheduler(simpleJob, regCenter,
                getLiteJobConfiguration(simpleJob.getClass(), cron));
    }
    /**
     * 配置任务详细信息 
     */
    private LiteJobConfiguration getLiteJobConfiguration(final Class jobClass,
                                                         final String cron) {
        return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(
                JobCoreConfiguration.newBuilder(jobClass.getName(), cron, 1)
                        .shardingItemParameters(null).build()
                , jobClass.getCanonicalName())
        ).overwrite(true).build();
    }
}

===============

@Slf4j
@Component("ElsJObEvent1")
public class ElsJObEvent1 implements SimpleJob{
    @Autowired
    private CountEventService countEventService;

    @Deprecated
    @Override
    public void execute(ShardingContext shardingContext) {
        //todo
    }
}

 

 

你可能感兴趣的:(java)