├── README.md ├── elastic-job-lite-starter ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── spring.factories │ │ └── java │ │ └── com │ │ └── paascloud │ │ └── elastic │ │ └── lite │ │ ├── GlobalConstant.java │ │ ├── JobParameter.java │ │ ├── ZookeeperRegistryProperties.java │ │ ├── autoconfigure │ │ ├── RegistryCenterConfiguration.java │ │ └── ElasticJobAutoConfiguration.java │ │ ├── job │ │ └── AbstractBaseDataflowJob.java │ │ └── annotation │ │ └── ElasticJobConfig.java └── pom.xml ├── elastic-job-lite-starter-demo ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── paascloud │ │ │ └── elastic │ │ │ ├── demo │ │ │ ├── Foo.java │ │ │ ├── DataflowJobDemoListener.java │ │ │ ├── SimpleJobDemo.java │ │ │ └── DataflowJobDemo.java │ │ │ └── Application.java │ │ └── resources │ │ └── application.yml └── pom.xml ├── .gitignore ├── pom.xml └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | # elastic-job-lite-starter-master 2 | -------------------------------------------------------------------------------- /elastic-job-lite-starter/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # Auto Configure 2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 3 | com.paascloud.elastic.lite.autoconfigure.RegistryCenterConfiguration,\ 4 | com.paascloud.elastic.lite.autoconfigure.ElasticJobAutoConfiguration -------------------------------------------------------------------------------- /elastic-job-lite-starter-demo/src/main/java/com/paascloud/elastic/demo/Foo.java: -------------------------------------------------------------------------------- 1 | package com.paascloud.elastic.demo; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * The class Foo. 8 | * 9 | * @author paascloud.net @gmail.com 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | public class Foo { 14 | /** 15 | * The Id. 16 | */ 17 | private Long id; 18 | } 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Java template 3 | # Compiled class file 4 | *.class 5 | 6 | # Log file 7 | *.log 8 | 9 | # BlueJ files 10 | *.ctxt 11 | 12 | # Mobile Tools for Java (J2ME) 13 | .mtj.tmp/ 14 | 15 | # Package Files # 16 | *.jar 17 | *.war 18 | *.ear 19 | *.zip 20 | *.tar.gz 21 | *.rar 22 | 23 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 24 | hs_err_pid* -------------------------------------------------------------------------------- /elastic-job-lite-starter-demo/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | paascloud: 4 | job: 5 | zookeeper: 6 | server-lists: paascloud-provider-zk:2181 7 | namespace: elastic-job-lite-demo 8 | spring: 9 | application: 10 | name: @pom.artifactId@ 11 | datasource: 12 | driver-class-name: com.mysql.jdbc.Driver 13 | url: jdbc:mysql://paascloud-db-mysql:3306/paascloud_job?characterEncoding=utf8&useSSL=false 14 | username: root 15 | password: 123456 16 | -------------------------------------------------------------------------------- /elastic-job-lite-starter/src/main/java/com/paascloud/elastic/lite/GlobalConstant.java: -------------------------------------------------------------------------------- 1 | package com.paascloud.elastic.lite; 2 | 3 | /** 4 | * The class Global constant. 5 | * @author paascloud.net@gmail.com 6 | */ 7 | public class GlobalConstant { 8 | 9 | /** 10 | * The class Symbol. 11 | * @author paascloud.net@gmail.com 12 | */ 13 | public static final class Symbol { 14 | /** 15 | * The constant COMMA. 16 | */ 17 | public static final String COMMA = ","; 18 | public final static String SIGN = "="; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /elastic-job-lite-starter-demo/src/main/java/com/paascloud/elastic/Application.java: -------------------------------------------------------------------------------- 1 | package com.paascloud.elastic; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * The class Application. 8 | * 9 | * @author paascloud.net @gmail.com 10 | */ 11 | @SpringBootApplication 12 | public class Application { 13 | 14 | /** 15 | * The entry point of application. 16 | * 17 | * @param args the input arguments 18 | */ 19 | public static void main(String[] args) { 20 | SpringApplication.run(Application.class, args); 21 | } 22 | } -------------------------------------------------------------------------------- /elastic-job-lite-starter/src/main/java/com/paascloud/elastic/lite/JobParameter.java: -------------------------------------------------------------------------------- 1 | package com.paascloud.elastic.lite; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | 7 | 8 | /** 9 | * The class Job task model. 10 | * 11 | * @author paascloud.net @gmail.com 12 | */ 13 | @Data 14 | public class JobParameter implements Serializable { 15 | private static final long serialVersionUID = -610797345091216847L; 16 | 17 | /** 18 | * 每次查询数据量 19 | */ 20 | private int fetchNum; 21 | /** 22 | * 取模条件 23 | */ 24 | int shardingItem; 25 | /** 26 | * 取模条件 27 | */ 28 | int shardingTotalCount; 29 | 30 | public JobParameter setShardingItem(final int shardingItem) { 31 | this.shardingItem = shardingItem; 32 | return this; 33 | } 34 | 35 | public JobParameter setShardingTotalCount(final int shardingTotalCount) { 36 | this.shardingTotalCount = shardingTotalCount; 37 | return this; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /elastic-job-lite-starter-demo/src/main/java/com/paascloud/elastic/demo/DataflowJobDemoListener.java: -------------------------------------------------------------------------------- 1 | package com.paascloud.elastic.demo; 2 | 3 | import com.dangdang.ddframe.job.executor.ShardingContexts; 4 | import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener; 5 | import lombok.extern.slf4j.Slf4j; 6 | 7 | /** 8 | * The class Handle user token job listener. 9 | * 10 | * @author paascloud.net @gmail.com 11 | */ 12 | @Slf4j 13 | public class DataflowJobDemoListener implements ElasticJobListener { 14 | 15 | /** 16 | * Before job executed. 17 | * 18 | * @param shardingContexts the sharding contexts 19 | */ 20 | @Override 21 | public void beforeJobExecuted(ShardingContexts shardingContexts) { 22 | log.info("beforeJobExecuted - shardingContexts={}", shardingContexts); 23 | } 24 | 25 | /** 26 | * After job executed. 27 | * 28 | * @param shardingContexts the sharding contexts 29 | */ 30 | @Override 31 | public void afterJobExecuted(ShardingContexts shardingContexts) { 32 | log.info("afterJobExecuted - shardingContexts={}", shardingContexts); 33 | } 34 | } -------------------------------------------------------------------------------- /elastic-job-lite-starter-demo/src/main/java/com/paascloud/elastic/demo/SimpleJobDemo.java: -------------------------------------------------------------------------------- 1 | package com.paascloud.elastic.demo; 2 | 3 | import com.dangdang.ddframe.job.api.ShardingContext; 4 | import com.dangdang.ddframe.job.api.simple.SimpleJob; 5 | import com.paascloud.elastic.lite.annotation.ElasticJobConfig; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.text.SimpleDateFormat; 9 | import java.util.Date; 10 | 11 | /** 12 | * The class Simple job demo. 13 | * 14 | * @author paascloud.net @gmail.com 15 | */ 16 | @ElasticJobConfig(cron = "0/4 * * * * ?", shardingTotalCount = 1, shardingItemParameters = "0=Beijing,1=Shanghai,2=Guangzhou") 17 | @Component 18 | public class SimpleJobDemo implements SimpleJob { 19 | /** 20 | * Execute. 21 | * 22 | * @param shardingContext the sharding context 23 | */ 24 | @Override 25 | public void execute(ShardingContext shardingContext) { 26 | System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s", 27 | shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "SimpleJob FETCH")); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /elastic-job-lite-starter-demo/src/main/java/com/paascloud/elastic/demo/DataflowJobDemo.java: -------------------------------------------------------------------------------- 1 | package com.paascloud.elastic.demo; 2 | 3 | import com.google.common.collect.Lists; 4 | import com.paascloud.elastic.lite.JobParameter; 5 | import com.paascloud.elastic.lite.annotation.ElasticJobConfig; 6 | import com.paascloud.elastic.lite.job.AbstractBaseDataflowJob; 7 | import lombok.extern.slf4j.Slf4j; 8 | import org.springframework.stereotype.Component; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * The class Dataflow job demo. 14 | * 15 | * @author paascloud.net @gmail.com 16 | */ 17 | @ElasticJobConfig(cron = "0/50 * * * * ? ", listener = DataflowJobDemoListener.class, jobParameter = "fetchNum=200,taskType=SENDING_MESSAGE") 18 | @Component 19 | @Slf4j 20 | public class DataflowJobDemo extends AbstractBaseDataflowJob { 21 | private List list = Lists.newArrayList(new Foo(1L), new Foo(2L)); 22 | 23 | @Override 24 | protected List fetchJobData(final JobParameter jobTaskParameter) { 25 | log.info("fetchJobData - jobTaskParameter={}", jobTaskParameter); 26 | return list; 27 | } 28 | 29 | @Override 30 | protected void processJobData(final List taskList) { 31 | log.info("processJobData - taskList={}", taskList); 32 | list.clear(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /elastic-job-lite-starter/src/main/java/com/paascloud/elastic/lite/ZookeeperRegistryProperties.java: -------------------------------------------------------------------------------- 1 | package com.paascloud.elastic.lite; 2 | 3 | import lombok.Data; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | 6 | /** 7 | * The class Zookeeper registry properties. 8 | * 9 | * @author paascloud.net @gmail.com 10 | */ 11 | @Data 12 | @ConfigurationProperties(prefix = "paascloud.zk") 13 | public class ZookeeperRegistryProperties { 14 | 15 | /** 16 | * 连接Zookeeper服务器的列表 17 | * 包括IP地址和端口号 18 | * 多个地址用逗号分隔 19 | * 如: host1:2181,host2:2181 20 | */ 21 | private String zkAddressList; 22 | 23 | /** 24 | * Zookeeper的命名空间 25 | */ 26 | private String namespace; 27 | 28 | /** 29 | * 等待重试的间隔时间的初始值 30 | * 单位:毫秒 31 | */ 32 | private int baseSleepTimeMilliseconds = 1000; 33 | 34 | /** 35 | * 等待重试的间隔时间的最大值 36 | * 单位:毫秒 37 | */ 38 | private int maxSleepTimeMilliseconds = 3000; 39 | 40 | /** 41 | * 最大重试次数 42 | */ 43 | private int maxRetries = 3; 44 | 45 | /** 46 | * 连接超时时间 47 | * 单位:毫秒 48 | */ 49 | private int connectionTimeoutMilliseconds = 15000; 50 | 51 | /** 52 | * 会话超时时间 53 | * 单位:毫秒 54 | */ 55 | private int sessionTimeoutMilliseconds = 60000; 56 | 57 | /** 58 | * 连接Zookeeper的权限令牌 59 | * 缺省为不需要权限验 60 | */ 61 | private String digest; 62 | } -------------------------------------------------------------------------------- /elastic-job-lite-starter-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elastic-job-lite-starter-parent 7 | com.liuzm.paascloud 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | elastic-job-lite-starter-demo 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-web 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-actuator 21 | 22 | 23 | com.liuzm.paascloud 24 | elastic-job-lite-starter 25 | 1.0 26 | 27 | 28 | com.alibaba 29 | druid-spring-boot-starter 30 | 1.1.0 31 | 32 | 33 | mysql 34 | mysql-connector-java 35 | 5.1.39 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /elastic-job-lite-starter/src/main/java/com/paascloud/elastic/lite/autoconfigure/RegistryCenterConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.paascloud.elastic.lite.autoconfigure; 2 | 3 | import com.dangdang.ddframe.job.api.ElasticJob; 4 | import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration; 5 | import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; 6 | import com.paascloud.elastic.lite.ZookeeperRegistryProperties; 7 | import com.paascloud.elastic.lite.annotation.ElasticJobConfig; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 10 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 11 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 12 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.context.annotation.Configuration; 15 | 16 | /** 17 | * The class Registry center configuration. 18 | * 19 | * @author paascloud.net @gmail.com 20 | */ 21 | @Configuration 22 | @ConditionalOnClass(ElasticJob.class) 23 | @ConditionalOnBean(annotation = ElasticJobConfig.class) 24 | @EnableConfigurationProperties(ZookeeperRegistryProperties.class) 25 | public class RegistryCenterConfiguration { 26 | 27 | private final ZookeeperRegistryProperties regCenterProperties; 28 | 29 | /** 30 | * Instantiates a new Registry center configuration. 31 | * 32 | * @param regCenterProperties the reg center properties 33 | */ 34 | @Autowired 35 | public RegistryCenterConfiguration(ZookeeperRegistryProperties regCenterProperties) { 36 | this.regCenterProperties = regCenterProperties; 37 | } 38 | 39 | /** 40 | * Reg center zookeeper registry center. 41 | * 42 | * @return the zookeeper registry center 43 | */ 44 | @Bean(initMethod = "init") 45 | @ConditionalOnMissingBean 46 | public ZookeeperRegistryCenter regCenter() { 47 | ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(regCenterProperties.getZkAddressList(), regCenterProperties.getNamespace()); 48 | zookeeperConfiguration.setBaseSleepTimeMilliseconds(regCenterProperties.getBaseSleepTimeMilliseconds()); 49 | zookeeperConfiguration.setConnectionTimeoutMilliseconds(regCenterProperties.getConnectionTimeoutMilliseconds()); 50 | zookeeperConfiguration.setMaxSleepTimeMilliseconds(regCenterProperties.getMaxSleepTimeMilliseconds()); 51 | zookeeperConfiguration.setSessionTimeoutMilliseconds(regCenterProperties.getSessionTimeoutMilliseconds()); 52 | zookeeperConfiguration.setMaxRetries(regCenterProperties.getMaxRetries()); 53 | zookeeperConfiguration.setDigest(regCenterProperties.getDigest()); 54 | return new ZookeeperRegistryCenter(zookeeperConfiguration); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /elastic-job-lite-starter/src/main/java/com/paascloud/elastic/lite/job/AbstractBaseDataflowJob.java: -------------------------------------------------------------------------------- 1 | package com.paascloud.elastic.lite.job; 2 | 3 | import com.dangdang.ddframe.job.api.ShardingContext; 4 | import com.dangdang.ddframe.job.api.dataflow.DataflowJob; 5 | import com.google.common.base.Splitter; 6 | import com.paascloud.elastic.lite.GlobalConstant; 7 | import com.paascloud.elastic.lite.JobParameter; 8 | import lombok.extern.slf4j.Slf4j; 9 | import org.modelmapper.ModelMapper; 10 | 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | /** 15 | * The class Abstract base dataflow job. 16 | * 17 | * @param the type parameter 18 | * 19 | * @author paascloud.net @gmail.com 20 | */ 21 | @Slf4j 22 | public abstract class AbstractBaseDataflowJob implements DataflowJob { 23 | 24 | /** 25 | * Fetch job data list. 26 | * 27 | * @param jobTaskParameter the job task parameter 28 | * 29 | * @return the list 30 | */ 31 | protected abstract List fetchJobData(JobParameter jobTaskParameter); 32 | 33 | /** 34 | * Process job data. 35 | * 36 | * @param taskList the task list 37 | */ 38 | protected abstract void processJobData(List taskList); 39 | 40 | /** 41 | * Fetch data list. 42 | * 43 | * @param shardingContext the sharding context 44 | * 45 | * @return the list 46 | */ 47 | @Override 48 | public List fetchData(ShardingContext shardingContext) { 49 | String jobName = shardingContext.getJobName(); 50 | int shardingItem = shardingContext.getShardingItem(); 51 | int shardingTotalCount = shardingContext.getShardingTotalCount(); 52 | String taskId = shardingContext.getTaskId(); 53 | String parameter = shardingContext.getJobParameter(); 54 | final Map map = Splitter.on(GlobalConstant.Symbol.COMMA).withKeyValueSeparator(GlobalConstant.Symbol.SIGN).split(parameter); 55 | JobParameter jobTaskParameter = new ModelMapper().map(map, JobParameter.class); 56 | jobTaskParameter.setShardingItem(shardingItem).setShardingTotalCount(shardingTotalCount); 57 | log.info("扫描worker任务列表开始,jobName={}, shardingItem={}, shardingTotalCount={}, taskId={}", jobName, shardingItem, shardingTotalCount, taskId); 58 | long startTimestamp = System.currentTimeMillis(); 59 | List taskLst = fetchJobData(jobTaskParameter); 60 | int taskNo = taskLst != null ? taskLst.size() : 0; 61 | long endTimestamp = System.currentTimeMillis(); 62 | log.info("扫描worker任务列表结束共计加载[{}]个任务, 耗时=[{}]",taskNo, (endTimestamp - startTimestamp)); 63 | return taskLst; 64 | } 65 | 66 | /** 67 | * Process data. 68 | * 69 | * @param shardingContext the sharding context 70 | * @param workerTask the worker task 71 | */ 72 | @Override 73 | public void processData(ShardingContext shardingContext, List workerTask) { 74 | log.info("任务[" + workerTask.get(0).getClass().getName() + "]开始执行..."); 75 | long startTimestamp = System.currentTimeMillis(); 76 | processJobData(workerTask); 77 | long endTimestamp = System.currentTimeMillis(); 78 | log.info("任务[" + workerTask.get(0).getClass().getName() + "]执行完毕:耗时=[{}]", (endTimestamp - startTimestamp)); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.liuzm.paascloud 6 | elastic-job-lite-starter-parent 7 | 1.0 8 | pom 9 | 10 | 11 | elastic-job-lite-starter 12 | elastic-job-lite-starter-demo 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-parent 18 | 1.4.7.RELEASE 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 1.8 26 | 1.8 27 | 2.1.5 28 | 2.12.0 29 | 3.5.1 30 | 2.10.4 31 | 3.0.1 32 | 33 | 34 | 35 | 36 | paascloud-lib-rep 37 | http://nexus.paascloud.net/content/repositories/paascloud-lib-rep/ 38 | 39 | 40 | nexus-snapshots 41 | User Project Snapshot 42 | http://nexus.paascloud.net/content/repositories/snapshots/ 43 | true 44 | 45 | 46 | 47 | 48 | 49 | 50 | elastic-job-common-core 51 | com.dangdang 52 | ${elastic-job-lite.version} 53 | 54 | 55 | elastic-job-lite-core 56 | com.dangdang 57 | ${elastic-job-lite.version} 58 | 59 | 60 | elastic-job-lite-spring 61 | com.dangdang 62 | ${elastic-job-lite.version} 63 | 64 | 65 | org.apache.curator 66 | curator-test 67 | 2.10.0 68 | 69 | 70 | org.apache.curator 71 | curator-client 72 | 2.10.0 73 | 74 | 75 | org.apache.curator 76 | curator-recipes 77 | 2.10.0 78 | 79 | 80 | org.apache.curator 81 | curator-framework 82 | 2.10.0 83 | 84 | 85 | org.projectlombok 86 | lombok 87 | 1.16.18 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /elastic-job-lite-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elastic-job-lite-starter-parent 7 | com.liuzm.paascloud 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | elastic-job-lite-starter 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot 18 | provided 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-autoconfigure 23 | provided 24 | 25 | 26 | com.dangdang 27 | elastic-job-common-core 28 | 29 | 30 | com.dangdang 31 | elastic-job-lite-core 32 | 33 | 34 | com.dangdang 35 | elastic-job-lite-spring 36 | 37 | 38 | curator-framework 39 | org.apache.curator 40 | 41 | 42 | curator-recipes 43 | org.apache.curator 44 | 45 | 46 | curator-client 47 | org.apache.curator 48 | 49 | 50 | org.projectlombok 51 | lombok 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-configuration-processor 56 | true 57 | 58 | 59 | org.modelmapper 60 | modelmapper 61 | 1.1.0 62 | 63 | 64 | 65 | 66 | 67 | org.apache.maven.plugins 68 | maven-compiler-plugin 69 | ${maven-compiler-plugin.version} 70 | 71 | ${java.version} 72 | ${java.version} 73 | ${java.version} 74 | ${java.version} 75 | 76 | 77 | 78 | 79 | org.apache.maven.plugins 80 | maven-javadoc-plugin 81 | ${maven-javadoc-plugin.version} 82 | 83 | UTF-8 84 | true 85 | UTF-8 86 | UTF-8 87 | 88 | 89 | 90 | attach-javadocs 91 | 92 | jar 93 | 94 | 95 | 96 | 97 | 98 | 99 | org.apache.maven.plugins 100 | maven-source-plugin 101 | ${maven-source-plugin.version} 102 | 103 | 104 | attach-sources 105 | 106 | jar 107 | 108 | 109 | 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /elastic-job-lite-starter/src/main/java/com/paascloud/elastic/lite/annotation/ElasticJobConfig.java: -------------------------------------------------------------------------------- 1 | package com.paascloud.elastic.lite.annotation; 2 | 3 | import com.dangdang.ddframe.job.lite.api.listener.AbstractDistributeOnceElasticJobListener; 4 | import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.lang.annotation.*; 8 | 9 | /** 10 | * The interface Elastic job config. 11 | * 12 | * @author paascloud.net @gmail.com 13 | */ 14 | @Target(ElementType.TYPE) 15 | @Retention(RetentionPolicy.RUNTIME) 16 | @Documented 17 | @Component 18 | public @interface ElasticJobConfig { 19 | 20 | /** 21 | * cron表达式,用于控制作业触发时间 22 | * 23 | * @return String string 24 | */ 25 | String cron(); 26 | 27 | /** 28 | * 作业分片总 29 | * 30 | * @return int int 31 | */ 32 | int shardingTotalCount() default 1; 33 | 34 | /** 35 | * 分片序列号和个性化参数对照表. 36 | * 分片序列号和参数用等号分隔, 多个键值对用逗号分隔. 类似map. 37 | * 分片序列号从0开始, 不可大于或等于作业分片总数. 38 | * 如: 39 | * 0=a,1=b,2=c 40 | * 41 | * @return String string 42 | */ 43 | String shardingItemParameters() default ""; 44 | 45 | /** 46 | * 作业自定义参数. 47 | * 作业自定义参数,可通过传递该参数为作业调度的业务方法传参,用于实现带参数的作业 48 | * 例:每次获取的数据量、作业实例从数据库读取的主键等 49 | * 50 | * @return String string 51 | */ 52 | String jobParameter() default ""; 53 | 54 | /** 55 | * 是否开启任务执行失效转移,开启表示如果作业在一次任务执行中途宕机, 56 | * 允许将该次未完成的任务在另一作业节点上补偿执行 57 | * 58 | * @return boolean boolean 59 | */ 60 | boolean failover() default false; 61 | 62 | /** 63 | * 是否开启错过任务重新执行 64 | * 65 | * @return boolean boolean 66 | */ 67 | boolean misfire() default true; 68 | 69 | /** 70 | * 作业描述信息. 71 | * 72 | * @return String string 73 | */ 74 | String description() default ""; 75 | 76 | /** 77 | * 配置jobProperties定义的枚举控制Elastic-Job的实现细节 78 | * JOB_EXCEPTION_HANDLER用于扩展异常处理类 79 | * 80 | * @return String string 81 | */ 82 | String jobExceptionHandler() default ""; 83 | 84 | /** 85 | * 配置jobProperties定义的枚举控制Elastic-Job的实现细节 86 | * EXECUTOR_SERVICE_HANDLER用于扩展作业处理线程池类 87 | * 88 | * @return String string 89 | */ 90 | String executorServiceHandler() default ""; 91 | 92 | /** 93 | * 是否流式处理数据 94 | * 如果流式处理数据, 则fetchData不返回空结果将持续执行作业 95 | * 如果非流式处理数据, 则处理数据完成后作业结 96 | * 97 | * @return boolean boolean 98 | */ 99 | boolean streamingProcess() default false; 100 | 101 | /** 102 | * 脚本型作业执行命令行 103 | * 104 | * @return String string 105 | */ 106 | String scriptCommandLine() default ""; 107 | 108 | /** 109 | * 监控作业运行时状态 110 | * 每次作业执行时间和间隔时间均非常短的情况,建议不监控作业运行时状态以提升效率。 111 | * 因为是瞬时状态,所以无必要监控。请用户自行增加数据堆积监控。并且不能保证数据重复选取,应在作业中实现幂等性。 112 | * 每次作业执行时间和间隔时间均较长的情况,建议监控作业运行时状态,可保证数据不会重复选取。 113 | * 114 | * @return String boolean 115 | */ 116 | boolean monitorExecution() default true; 117 | 118 | /** 119 | * 作业监控端口 120 | * 建议配置作业监控端口, 方便开发者dump作业信息。 121 | * 使用方法: echo “dump” | nc 127.0.0.1 9888 122 | * 123 | * @return int int 124 | */ 125 | int monitorPort() default -1; 126 | 127 | /** 128 | * 最大允许的本机与注册中心的时间误差秒数 129 | * 如果时间误差超过配置秒数则作业启动时将抛异常 130 | * 配置为-1表示不校验时间误差 131 | * 132 | * @return int int 133 | */ 134 | int maxTimeDiffSeconds() default -1; 135 | 136 | /** 137 | * 作业分片策略实现类全路径 138 | * 默认使用平均分配策略 139 | * 详情参见:作业分片策略http://elasticjob.io/docs/elastic-job-lite/02-guide/job-sharding-strategy 140 | * 141 | * @return String string 142 | */ 143 | String jobShardingStrategyClass() default ""; 144 | 145 | /** 146 | * 修复作业服务器不一致状态服务调度间隔时间,配置为小于1的任意值表示不执行修复 147 | * 单位:分钟 148 | * 149 | * @return int int 150 | */ 151 | int reconcileIntervalMinutes() default 10; 152 | 153 | /** 154 | * 作业事件追踪的数据源Bean引用 155 | * 156 | * @return String string 157 | */ 158 | String eventTraceRdbDataSource() default "dataSource"; 159 | 160 | /** 161 | * 本地配置是否可覆盖注册中心配置. 162 | * 如果可覆盖, 每次启动作业都以本地配置为准. 163 | * 164 | * @return boolean boolean 165 | */ 166 | boolean overwrite() default true; 167 | 168 | /** 169 | * 作业是否禁止启动 170 | * 可用于部署作业时,先禁止启动,部署结束后统一启动 171 | * 172 | * @return boolean boolean 173 | */ 174 | boolean disabled() default false; 175 | 176 | /** 177 | * 每台作业节点均执行的监听 178 | * 若作业处理作业服务器的文件,处理完成后删除文件,可考虑使用每个节点均执行清理任务。 179 | * 此类型任务实现简单,且无需考虑全局分布式任务是否完成,请尽量使用此类型监听器。 180 | * 181 | * @return Class class 182 | */ 183 | Class listener() default ElasticJobListener.class; 184 | 185 | /** 186 | * 分布式场景中仅单一节点执行的监听 187 | * 若作业处理数据库数据,处理完成后只需一个节点完成数据清理任务即可。 188 | * 此类型任务处理复杂,需同步分布式环境下作业的状态同步,提供了超时设置来避免作业不同步导致的死锁,请谨慎使用。 189 | * 190 | * @return Class class 191 | */ 192 | Class distributedListener() default AbstractDistributeOnceElasticJobListener.class; 193 | 194 | /** 195 | * 最后一个作业执行前的执行方法的超时时间 196 | * 单位:毫秒 197 | * 198 | * @return long long 199 | */ 200 | long startedTimeoutMilliseconds() default Long.MAX_VALUE; 201 | 202 | /** 203 | * 最后一个作业执行后的执行方法的超时时间 204 | * 单位:毫秒 205 | * 206 | * @return long long 207 | */ 208 | long completedTimeoutMilliseconds() default Long.MAX_VALUE; 209 | } 210 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and 10 | distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright 13 | owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities 16 | that control, are controlled by, or are under common control with that entity. 17 | For the purposes of this definition, "control" means (i) the power, direct or 18 | indirect, to cause the direction or management of such entity, whether by 19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the 20 | outstanding shares, or (iii) beneficial ownership of such entity. 21 | 22 | "You" (or "Your") shall mean an individual or Legal Entity exercising 23 | permissions granted by this License. 24 | 25 | "Source" form shall mean the preferred form for making modifications, including 26 | but not limited to software source code, documentation source, and configuration 27 | files. 28 | 29 | "Object" form shall mean any form resulting from mechanical transformation or 30 | translation of a Source form, including but not limited to compiled object code, 31 | generated documentation, and conversions to other media types. 32 | 33 | "Work" shall mean the work of authorship, whether in Source or Object form, made 34 | available under the License, as indicated by a copyright notice that is included 35 | in or attached to the work (an example is provided in the Appendix below). 36 | 37 | "Derivative Works" shall mean any work, whether in Source or Object form, that 38 | is based on (or derived from) the Work and for which the editorial revisions, 39 | annotations, elaborations, or other modifications represent, as a whole, an 40 | original work of authorship. For the purposes of this License, Derivative Works 41 | shall not include works that remain separable from, or merely link (or bind by 42 | name) to the interfaces of, the Work and Derivative Works thereof. 43 | 44 | "Contribution" shall mean any work of authorship, including the original version 45 | of the Work and any modifications or additions to that Work or Derivative Works 46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 47 | by the copyright owner or by an individual or Legal Entity authorized to submit 48 | on behalf of the copyright owner. For the purposes of this definition, 49 | "submitted" means any form of electronic, verbal, or written communication sent 50 | to the Licensor or its representatives, including but not limited to 51 | communication on electronic mailing lists, source code control systems, and 52 | issue tracking systems that are managed by, or on behalf of, the Licensor for 53 | the purpose of discussing and improving the Work, but excluding communication 54 | that is conspicuously marked or otherwise designated in writing by the copyright 55 | owner as "Not a Contribution." 56 | 57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf 58 | of whom a Contribution has been received by Licensor and subsequently 59 | incorporated within the Work. 60 | 61 | 2. Grant of Copyright License. 62 | 63 | Subject to the terms and conditions of this License, each Contributor hereby 64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 65 | irrevocable copyright license to reproduce, prepare Derivative Works of, 66 | publicly display, publicly perform, sublicense, and distribute the Work and such 67 | Derivative Works in Source or Object form. 68 | 69 | 3. Grant of Patent License. 70 | 71 | Subject to the terms and conditions of this License, each Contributor hereby 72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 73 | irrevocable (except as stated in this section) patent license to make, have 74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 75 | such license applies only to those patent claims licensable by such Contributor 76 | that are necessarily infringed by their Contribution(s) alone or by combination 77 | of their Contribution(s) with the Work to which such Contribution(s) was 78 | submitted. If You institute patent litigation against any entity (including a 79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 80 | Contribution incorporated within the Work constitutes direct or contributory 81 | patent infringement, then any patent licenses granted to You under this License 82 | for that Work shall terminate as of the date such litigation is filed. 83 | 84 | 4. Redistribution. 85 | 86 | You may reproduce and distribute copies of the Work or Derivative Works thereof 87 | in any medium, with or without modifications, and in Source or Object form, 88 | provided that You meet the following conditions: 89 | 90 | You must give any other recipients of the Work or Derivative Works a copy of 91 | this License; and 92 | You must cause any modified files to carry prominent notices stating that You 93 | changed the files; and 94 | You must retain, in the Source form of any Derivative Works that You distribute, 95 | all copyright, patent, trademark, and attribution notices from the Source form 96 | of the Work, excluding those notices that do not pertain to any part of the 97 | Derivative Works; and 98 | If the Work includes a "NOTICE" text file as part of its distribution, then any 99 | Derivative Works that You distribute must include a readable copy of the 100 | attribution notices contained within such NOTICE file, excluding those notices 101 | that do not pertain to any part of the Derivative Works, in at least one of the 102 | following places: within a NOTICE text file distributed as part of the 103 | Derivative Works; within the Source form or documentation, if provided along 104 | with the Derivative Works; or, within a display generated by the Derivative 105 | Works, if and wherever such third-party notices normally appear. The contents of 106 | the NOTICE file are for informational purposes only and do not modify the 107 | License. You may add Your own attribution notices within Derivative Works that 108 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 109 | provided that such additional attribution notices cannot be construed as 110 | modifying the License. 111 | You may add Your own copyright statement to Your modifications and may provide 112 | additional or different license terms and conditions for use, reproduction, or 113 | distribution of Your modifications, or for any such Derivative Works as a whole, 114 | provided Your use, reproduction, and distribution of the Work otherwise complies 115 | with the conditions stated in this License. 116 | 117 | 5. Submission of Contributions. 118 | 119 | Unless You explicitly state otherwise, any Contribution intentionally submitted 120 | for inclusion in the Work by You to the Licensor shall be under the terms and 121 | conditions of this License, without any additional terms or conditions. 122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 123 | any separate license agreement you may have executed with Licensor regarding 124 | such Contributions. 125 | 126 | 6. Trademarks. 127 | 128 | This License does not grant permission to use the trade names, trademarks, 129 | service marks, or product names of the Licensor, except as required for 130 | reasonable and customary use in describing the origin of the Work and 131 | reproducing the content of the NOTICE file. 132 | 133 | 7. Disclaimer of Warranty. 134 | 135 | Unless required by applicable law or agreed to in writing, Licensor provides the 136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, 137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 138 | including, without limitation, any warranties or conditions of TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 140 | solely responsible for determining the appropriateness of using or 141 | redistributing the Work and assume any risks associated with Your exercise of 142 | permissions under this License. 143 | 144 | 8. Limitation of Liability. 145 | 146 | In no event and under no legal theory, whether in tort (including negligence), 147 | contract, or otherwise, unless required by applicable law (such as deliberate 148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 149 | liable to You for damages, including any direct, indirect, special, incidental, 150 | or consequential damages of any character arising as a result of this License or 151 | out of the use or inability to use the Work (including but not limited to 152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 153 | any and all other commercial damages or losses), even if such Contributor has 154 | been advised of the possibility of such damages. 155 | 156 | 9. Accepting Warranty or Additional Liability. 157 | 158 | While redistributing the Work or Derivative Works thereof, You may choose to 159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 160 | other liability obligations and/or rights consistent with this License. However, 161 | in accepting such obligations, You may act only on Your own behalf and on Your 162 | sole responsibility, not on behalf of any other Contributor, and only if You 163 | agree to indemnify, defend, and hold each Contributor harmless for any liability 164 | incurred by, or claims asserted against, such Contributor by reason of your 165 | accepting any such warranty or additional liability. 166 | 167 | END OF TERMS AND CONDITIONS 168 | 169 | APPENDIX: How to apply the Apache License to your work 170 | 171 | To apply the Apache License to your work, attach the following boilerplate 172 | notice, with the fields enclosed by brackets "{}" replaced with your own 173 | identifying information. (Don't include the brackets!) The text should be 174 | enclosed in the appropriate comment syntax for the file format. We also 175 | recommend that a file or class name and description of purpose be included on 176 | the same "printed page" as the copyright notice for easier identification within 177 | third-party archives. 178 | 179 | Copyright 2018 无痕 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. -------------------------------------------------------------------------------- /elastic-job-lite-starter/src/main/java/com/paascloud/elastic/lite/autoconfigure/ElasticJobAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.paascloud.elastic.lite.autoconfigure; 2 | 3 | import com.dangdang.ddframe.job.api.ElasticJob; 4 | import com.dangdang.ddframe.job.api.JobType; 5 | import com.dangdang.ddframe.job.api.dataflow.DataflowJob; 6 | import com.dangdang.ddframe.job.api.script.ScriptJob; 7 | import com.dangdang.ddframe.job.api.simple.SimpleJob; 8 | import com.dangdang.ddframe.job.config.JobCoreConfiguration; 9 | import com.dangdang.ddframe.job.config.JobTypeConfiguration; 10 | import com.dangdang.ddframe.job.config.dataflow.DataflowJobConfiguration; 11 | import com.dangdang.ddframe.job.config.script.ScriptJobConfiguration; 12 | import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration; 13 | import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration; 14 | import com.dangdang.ddframe.job.executor.handler.JobProperties; 15 | import com.dangdang.ddframe.job.lite.api.listener.AbstractDistributeOnceElasticJobListener; 16 | import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener; 17 | import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration; 18 | import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler; 19 | import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; 20 | import com.paascloud.elastic.lite.annotation.ElasticJobConfig; 21 | import org.apache.commons.lang3.StringUtils; 22 | import org.springframework.beans.factory.config.BeanDefinition; 23 | import org.springframework.beans.factory.support.BeanDefinitionBuilder; 24 | import org.springframework.beans.factory.support.DefaultListableBeanFactory; 25 | import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; 26 | import org.springframework.context.ApplicationContext; 27 | import org.springframework.context.ConfigurableApplicationContext; 28 | import org.springframework.context.annotation.Configuration; 29 | import org.springframework.util.CollectionUtils; 30 | 31 | import javax.annotation.PostConstruct; 32 | import javax.annotation.Resource; 33 | import javax.sql.DataSource; 34 | import java.util.ArrayList; 35 | import java.util.List; 36 | import java.util.Map; 37 | import java.util.Objects; 38 | 39 | /** 40 | * The class Elastic job auto configuration. 41 | * 42 | * @author paascloud.net @gmail.com 43 | */ 44 | @Configuration 45 | @ConditionalOnExpression("'${elaticjob.zookeeper.server-lists}'.length() > 0") 46 | public class ElasticJobAutoConfiguration { 47 | 48 | @Resource 49 | private ZookeeperRegistryCenter regCenter; 50 | 51 | @Resource 52 | private ApplicationContext applicationContext; 53 | 54 | /** 55 | * Init. 56 | */ 57 | @PostConstruct 58 | public void init() { 59 | //获取作业任务 60 | Map elasticJobMap = applicationContext.getBeansOfType(ElasticJob.class); 61 | //循环解析任务 62 | for (ElasticJob elasticJob : elasticJobMap.values()) { 63 | Class jobClass = elasticJob.getClass(); 64 | //获取作业任务注解配置 65 | ElasticJobConfig elasticJobConfig = jobClass.getAnnotation(ElasticJobConfig.class); 66 | //获取Lite作业配置 67 | LiteJobConfiguration liteJobConfiguration = getLiteJobConfiguration(getJobType(elasticJob), jobClass, elasticJobConfig); 68 | //获取作业事件追踪的数据源配置 69 | JobEventRdbConfiguration jobEventRdbConfiguration = getJobEventRdbConfiguration(elasticJobConfig.eventTraceRdbDataSource()); 70 | //获取作业监听器 71 | ElasticJobListener[] elasticJobListeners = createElasticJobListeners(elasticJobConfig); 72 | elasticJobListeners = Objects.isNull(elasticJobListeners) ? new ElasticJobListener[0] : elasticJobListeners; 73 | //注册作业 74 | if (Objects.isNull(jobEventRdbConfiguration)) { 75 | new SpringJobScheduler(elasticJob, regCenter, liteJobConfiguration, elasticJobListeners).init(); 76 | } else { 77 | new SpringJobScheduler(elasticJob, regCenter, liteJobConfiguration, jobEventRdbConfiguration, elasticJobListeners).init(); 78 | } 79 | } 80 | } 81 | 82 | /** 83 | * 获取作业事件追踪的数据源配置 84 | * 85 | * @param eventTraceRdbDataSource 作业事件追踪的数据源Bean引用 86 | * @return JobEventRdbConfiguration 87 | */ 88 | private JobEventRdbConfiguration getJobEventRdbConfiguration(String eventTraceRdbDataSource) { 89 | if (StringUtils.isBlank(eventTraceRdbDataSource)) { 90 | return null; 91 | } 92 | if (!applicationContext.containsBean(eventTraceRdbDataSource)) { 93 | throw new RuntimeException("not exist datasource [" + eventTraceRdbDataSource + "] !"); 94 | } 95 | DataSource dataSource = (DataSource) applicationContext.getBean(eventTraceRdbDataSource); 96 | return new JobEventRdbConfiguration(dataSource); 97 | } 98 | 99 | /** 100 | * 获取作业任务类型 101 | * 102 | * @param elasticJob 作业任务 103 | * @return JobType 104 | */ 105 | private JobType getJobType(ElasticJob elasticJob) { 106 | if (elasticJob instanceof SimpleJob) { 107 | return JobType.SIMPLE; 108 | } else if (elasticJob instanceof DataflowJob) { 109 | return JobType.DATAFLOW; 110 | } else if (elasticJob instanceof ScriptJob) { 111 | return JobType.SCRIPT; 112 | } else { 113 | throw new RuntimeException("unknown JobType [" + elasticJob.getClass() + "]!"); 114 | } 115 | } 116 | 117 | /** 118 | * 构建任务核心配置 119 | * 120 | * @param jobName 任务执行名称 121 | * @param elasticJobConfig 任务配置 122 | * @return JobCoreConfiguration 123 | */ 124 | private JobCoreConfiguration getJobCoreConfiguration(String jobName, ElasticJobConfig elasticJobConfig) { 125 | JobCoreConfiguration.Builder builder = JobCoreConfiguration.newBuilder(jobName, elasticJobConfig.cron(), elasticJobConfig.shardingTotalCount()) 126 | .shardingItemParameters(elasticJobConfig.shardingItemParameters()) 127 | .jobParameter(elasticJobConfig.jobParameter()) 128 | .failover(elasticJobConfig.failover()) 129 | .misfire(elasticJobConfig.misfire()) 130 | .description(elasticJobConfig.description()); 131 | if (StringUtils.isNotBlank(elasticJobConfig.jobExceptionHandler())) { 132 | builder.jobProperties(JobProperties.JobPropertiesEnum.JOB_EXCEPTION_HANDLER.getKey(), elasticJobConfig.jobExceptionHandler()); 133 | } 134 | if (StringUtils.isNotBlank(elasticJobConfig.executorServiceHandler())) { 135 | builder.jobProperties(JobProperties.JobPropertiesEnum.EXECUTOR_SERVICE_HANDLER.getKey(), elasticJobConfig.executorServiceHandler()); 136 | } 137 | return builder.build(); 138 | } 139 | 140 | /** 141 | * 构建Lite作业 142 | * 143 | * @param jobType 任务类型 144 | * @param jobClass 任务执行类 145 | * @param elasticJobConfig 任务配置 146 | * @return LiteJobConfiguration 147 | */ 148 | private LiteJobConfiguration getLiteJobConfiguration(final JobType jobType, final Class jobClass, ElasticJobConfig elasticJobConfig) { 149 | 150 | //构建核心配置 151 | JobCoreConfiguration jobCoreConfiguration = getJobCoreConfiguration(jobClass.getName(), elasticJobConfig); 152 | 153 | //构建任务类型配置 154 | JobTypeConfiguration jobTypeConfiguration = getJobTypeConfiguration(jobCoreConfiguration, jobType, jobClass.getCanonicalName(), 155 | elasticJobConfig.streamingProcess(), elasticJobConfig.scriptCommandLine()); 156 | 157 | //构建Lite作业 158 | return LiteJobConfiguration.newBuilder(Objects.requireNonNull(jobTypeConfiguration)) 159 | .monitorExecution(elasticJobConfig.monitorExecution()) 160 | .monitorPort(elasticJobConfig.monitorPort()) 161 | .maxTimeDiffSeconds(elasticJobConfig.maxTimeDiffSeconds()) 162 | .jobShardingStrategyClass(elasticJobConfig.jobShardingStrategyClass()) 163 | .reconcileIntervalMinutes(elasticJobConfig.reconcileIntervalMinutes()) 164 | .disabled(elasticJobConfig.disabled()) 165 | .overwrite(elasticJobConfig.overwrite()).build(); 166 | 167 | } 168 | 169 | /** 170 | * 获取任务类型配置 171 | * 172 | * @param jobCoreConfiguration 作业核心配置 173 | * @param jobType 作业类型 174 | * @param jobClass 作业类 175 | * @param streamingProcess 是否流式处理数据 176 | * @param scriptCommandLine 脚本型作业执行命令行 177 | * @return JobTypeConfiguration 178 | */ 179 | private JobTypeConfiguration getJobTypeConfiguration(JobCoreConfiguration jobCoreConfiguration, JobType jobType, 180 | String jobClass, boolean streamingProcess, String scriptCommandLine) { 181 | switch (jobType) { 182 | case DATAFLOW: 183 | return new DataflowJobConfiguration(jobCoreConfiguration, jobClass, streamingProcess); 184 | case SCRIPT: 185 | return new ScriptJobConfiguration(jobCoreConfiguration, scriptCommandLine); 186 | case SIMPLE: 187 | default: 188 | return new SimpleJobConfiguration(jobCoreConfiguration, jobClass); 189 | } 190 | } 191 | 192 | /** 193 | * 获取监听器 194 | * 195 | * @param elasticJobConfig 任务配置 196 | * @return ElasticJobListener[] 197 | */ 198 | private ElasticJobListener[] createElasticJobListeners(ElasticJobConfig elasticJobConfig) { 199 | List elasticJobListeners = new ArrayList<>(2); 200 | 201 | //注册每台作业节点均执行的监听 202 | ElasticJobListener elasticJobListener = createElasticJobListener(elasticJobConfig.listener()); 203 | if (Objects.nonNull(elasticJobListener)) { 204 | elasticJobListeners.add(elasticJobListener); 205 | } 206 | 207 | //注册分布式监听者 208 | AbstractDistributeOnceElasticJobListener distributedListener = createAbstractDistributeOnceElasticJobListener(elasticJobConfig.distributedListener(), 209 | elasticJobConfig.startedTimeoutMilliseconds(), elasticJobConfig.completedTimeoutMilliseconds()); 210 | if (Objects.nonNull(distributedListener)) { 211 | elasticJobListeners.add(distributedListener); 212 | } 213 | 214 | if (CollectionUtils.isEmpty(elasticJobListeners)) { 215 | return null; 216 | } 217 | 218 | //集合转数组 219 | ElasticJobListener[] elasticJobListenerArray = new ElasticJobListener[elasticJobListeners.size()]; 220 | for (int i = 0; i < elasticJobListeners.size(); i++) { 221 | elasticJobListenerArray[i] = elasticJobListeners.get(i); 222 | } 223 | return elasticJobListenerArray; 224 | } 225 | 226 | /** 227 | * 创建每台作业节点均执行的监听 228 | * 229 | * @param listener 监听者 230 | * @return ElasticJobListener 231 | */ 232 | private ElasticJobListener createElasticJobListener(Class listener) { 233 | //判断是否配置了监听者 234 | if (listener.isInterface()) { 235 | return null; 236 | } 237 | //判断监听者是否已经在spring容器中存在 238 | if (applicationContext.containsBean(listener.getSimpleName())) { 239 | return applicationContext.getBean(listener.getSimpleName(), ElasticJobListener.class); 240 | } 241 | //不存在则创建并注册到Spring容器中 242 | return registerElasticJobListener(listener); 243 | } 244 | 245 | /** 246 | * 创建分布式监听者到spring容器 247 | * 248 | * @param distributedListener 监听者 249 | * @param startedTimeoutMilliseconds 最后一个作业执行前的执行方法的超时时间 单位:毫秒 250 | * @param completedTimeoutMilliseconds 最后一个作业执行后的执行方法的超时时间 单位:毫秒 251 | * @return AbstractDistributeOnceElasticJobListener 252 | */ 253 | private AbstractDistributeOnceElasticJobListener createAbstractDistributeOnceElasticJobListener(Class distributedListener, 254 | long startedTimeoutMilliseconds, 255 | long completedTimeoutMilliseconds) { 256 | //判断是否配置了监听者 257 | if (Objects.equals(distributedListener, AbstractDistributeOnceElasticJobListener.class)) { 258 | return null; 259 | } 260 | //判断监听者是否已经在spring容器中存在 261 | if (applicationContext.containsBean(distributedListener.getSimpleName())) { 262 | return applicationContext.getBean(distributedListener.getSimpleName(), AbstractDistributeOnceElasticJobListener.class); 263 | } 264 | //不存在则创建并注册到Spring容器中 265 | return registerAbstractDistributeOnceElasticJobListener(distributedListener, startedTimeoutMilliseconds, completedTimeoutMilliseconds); 266 | } 267 | 268 | /** 269 | * 注册每台作业节点均执行的监听到spring容器 270 | * 271 | * @param listener 监听者 272 | * @return ElasticJobListener 273 | */ 274 | private ElasticJobListener registerElasticJobListener(Class listener) { 275 | BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(listener); 276 | beanDefinitionBuilder.setScope(BeanDefinition.SCOPE_PROTOTYPE); 277 | getDefaultListableBeanFactory().registerBeanDefinition(listener.getSimpleName(), beanDefinitionBuilder.getBeanDefinition()); 278 | return applicationContext.getBean(listener.getSimpleName(), listener); 279 | } 280 | 281 | /** 282 | * 注册分布式监听者到spring容器 283 | * 284 | * @param distributedListener 监听者 285 | * @param startedTimeoutMilliseconds 最后一个作业执行前的执行方法的超时时间 单位:毫秒 286 | * @param completedTimeoutMilliseconds 最后一个作业执行后的执行方法的超时时间 单位:毫秒 287 | * @return AbstractDistributeOnceElasticJobListener 288 | */ 289 | private AbstractDistributeOnceElasticJobListener registerAbstractDistributeOnceElasticJobListener(Class distributedListener, 290 | long startedTimeoutMilliseconds, 291 | long completedTimeoutMilliseconds) { 292 | BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(distributedListener); 293 | beanDefinitionBuilder.setScope(BeanDefinition.SCOPE_PROTOTYPE); 294 | beanDefinitionBuilder.addConstructorArgValue(startedTimeoutMilliseconds); 295 | beanDefinitionBuilder.addConstructorArgValue(completedTimeoutMilliseconds); 296 | getDefaultListableBeanFactory().registerBeanDefinition(distributedListener.getSimpleName(), beanDefinitionBuilder.getBeanDefinition()); 297 | return applicationContext.getBean(distributedListener.getSimpleName(), distributedListener); 298 | } 299 | 300 | /** 301 | * 获取beanFactory 302 | * 303 | * @return DefaultListableBeanFactory 304 | */ 305 | private DefaultListableBeanFactory getDefaultListableBeanFactory() { 306 | return (DefaultListableBeanFactory) ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); 307 | } 308 | 309 | } 310 | --------------------------------------------------------------------------------