当当(Elastic-job)分布式定时任务
生活随笔
收集整理的這篇文章主要介紹了
当当(Elastic-job)分布式定时任务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Elastic-job解決的問題
? ? ? ? Elastic-job解決以下問題,我們開發定時任務一般都是使用quartz或者spring-task(ScheduledExecutorService),無論是使用quartz還是spring-task,我們都會至少遇到兩個痛點:
Elastic-job的主要包括以下功能。
運用實例?
? ? ?1.引入依賴?? (基于gradle)
compile('com.dangdang:elastic-job-core:1.0.6') compile('com.dangdang:elastic-job-spring:1.0.6')? ? ?2.xml文件的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:reg="http://www.dangdang.com/schema/ddframe/reg"xmlns:job="http://www.dangdang.com/schema/ddframe/job"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.dangdang.com/schema/ddframe/reghttp://www.dangdang.com/schema/ddframe/reg/reg.xsdhttp://www.dangdang.com/schema/ddframe/jobhttp://www.dangdang.com/schema/ddframe/job/job.xsd"><!--配置作業注冊中心 serverLists:注冊中心地址(zookeeper或者其他的)maxRetries:最大重試次數--><reg:zookeeper id="regCenter" serverLists="${acw.zookeeper.address}" namespace="duiba-job"baseSleepTimeMilliseconds="1000" maxSleepTimeMilliseconds="3000" maxRetries="3" overwrite="true"/><!-- 配置定時任務shardingTotalCount:分幾個片段執行--><job:bean id="aMonthlyScheduleTaskJob" class="cn.com.duiba.manager.web.biz.job.impl.AMonthlyScheduleTaskJob"regCenter="regCenter" cron="0 0 1 1 * ?" shardingTotalCount="1" overwrite="true"/>? ? 3.JAVA代碼
import cn.com.duiba.manager.web.common.tools.Environment; import com.dangdang.ddframe.job.api.JobExecutionMultipleShardingContext; import com.dangdang.ddframe.job.plugin.job.type.simple.AbstractSimpleElasticJob; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value;public abstract class AbstractManagerJob extends AbstractSimpleElasticJob {private static final Logger LOG = LoggerFactory.getLogger(AbstractManagerJob.class);@Value("${duiba.job.skip:false}")private boolean skipJob;public void process(final JobExecutionMultipleShardingContext shardingContext) {if (Environment.isPrepub() || skipJob) {return;}try {doProcess(shardingContext);} catch (Exception e) {LOG.error("", e);}}public abstract void doProcess(final JobExecutionMultipleShardingContext shardingContext); } import cn.com.duiba.manager.web.biz.job.AbstractManagerJob; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import cn.com.duiba.developer.center.api.remoteservice.RemoteAppService;import com.dangdang.ddframe.job.api.JobExecutionMultipleShardingContext;@Component public class AMonthlyScheduleTaskJob extends AbstractManagerJob {@Autowiredprivate RemoteAppService remoteAppServiceNew;@Overridepublic void doProcess(JobExecutionMultipleShardingContext shardingContext) {aMonthlySchedule();}private void aMonthlySchedule() {remoteAppServiceNew.updatBudgetRemain();} }?
總結
以上是生活随笔為你收集整理的当当(Elastic-job)分布式定时任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hadoop 常用hdfs命令
- 下一篇: com.alibaba.excel.ex