重构elasticJob项目结构

master
wujingtao 3 years ago
parent 389e35c6d2
commit d03e348099

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>elastic-job-springboot</artifactId>
<groupId>com.example</groupId>
<version>3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>elastic-job-springboot-core</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>

@ -1,4 +1,4 @@
package com.example.autoconfig; package com.example.config;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob; import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration; import com.dangdang.ddframe.job.config.JobCoreConfiguration;
@ -7,6 +7,7 @@ import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener; import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration; import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter; import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.example.jobInstance.ElasticDataflowJob;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;

@ -1,4 +1,4 @@
package com.example.autoconfig; package com.example.config;
import com.dangdang.ddframe.job.api.simple.SimpleJob; import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration; import com.dangdang.ddframe.job.config.JobCoreConfiguration;
@ -7,6 +7,7 @@ import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener; import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration; import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter; import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.example.jobInstance.ElasticSimpleJob;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@ -15,7 +16,6 @@ import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Map; import java.util.Map;
/** /**

@ -1,8 +1,9 @@
package com.example.autoconfig; package com.example.config;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter; import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration; import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.example.config.ZookeeperProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;

@ -1,4 +1,4 @@
package com.example.autoconfig; package com.example.config;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;

@ -1,4 +1,4 @@
package com.example.autoconfig; package com.example.jobInstance;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener; import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import com.dangdang.ddframe.job.lite.api.strategy.impl.AverageAllocationJobShardingStrategy; import com.dangdang.ddframe.job.lite.api.strategy.impl.AverageAllocationJobShardingStrategy;

@ -1,4 +1,4 @@
package com.example.autoconfig; package com.example.jobInstance;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener; import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import com.dangdang.ddframe.job.lite.api.strategy.impl.AverageAllocationJobShardingStrategy; import com.dangdang.ddframe.job.lite.api.strategy.impl.AverageAllocationJobShardingStrategy;

@ -1,10 +1,11 @@
package com.example.application.job.listener; package com.example.listener;
import cn.hutool.core.date.DateUtil;
import com.dangdang.ddframe.job.executor.ShardingContexts; import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener; import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
/** /**
* @author q * @author q
@ -19,11 +20,11 @@ public class MyNormalListener implements ElasticJobListener {
*/ */
@Override @Override
public void beforeJobExecuted(ShardingContexts shardingContexts) { public void beforeJobExecuted(ShardingContexts shardingContexts) {
log.info("我是 : {} 作业, {}开始执行!", shardingContexts.getJobName(), DateUtil.now()); log.info("我是 : {} 作业, {}开始执行!", shardingContexts.getJobName(), LocalDateTime.now());
} }
@Override @Override
public void afterJobExecuted(ShardingContexts shardingContexts) { public void afterJobExecuted(ShardingContexts shardingContexts) {
log.info("我是 : {} 作业, {}结束执行!", shardingContexts.getJobName(), DateUtil.now()); log.info("我是 : {} 作业, {}结束执行!", shardingContexts.getJobName(), LocalDateTime.now());
} }
} }

@ -4,13 +4,13 @@
"name": "elasticjob.zookeeper.namespace", "name": "elasticjob.zookeeper.namespace",
"type": "java.lang.String", "type": "java.lang.String",
"description": "Zookeeper 命名空间", "description": "Zookeeper 命名空间",
"sourceType": "com.example.autoconfig.ZookeeperProperties" "sourceType": "com.example.config.ZookeeperProperties"
}, },
{ {
"name": "elasticjob.zookeeper.server-list", "name": "elasticjob.zookeeper.server-list",
"type": "java.lang.String", "type": "java.lang.String",
"description": "Zookeeper 服务列表", "description": "Zookeeper 服务列表",
"sourceType": "com.example.autoconfig.ZookeeperProperties" "sourceType": "com.example.config.ZookeeperProperties"
} }
] ]
} }

@ -0,0 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.config.ZookeeperAutoConfig,\
com.example.config.DataflowJobAutoConfig,\
com.example.config.SimpleJobAutoConfig

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>elastic-job-springboot</artifactId>
<groupId>com.example</groupId>
<version>3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>elastic-job-springboot-test</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>elastic-job-springboot-core</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

@ -0,0 +1,26 @@
package com.example;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.example.jobInstance.ElasticSimpleJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author wujingtao
* @date 2021/11/15
*/
@Slf4j
@ElasticSimpleJob(
jobName = "DemoJob", cron = "0 0/1 * * * ?", shardingTotalCount = 1, overwrite = true, jobEvent = false)
@Component
public class DemoJob implements SimpleJob {
@Override
public void execute(ShardingContext shardingContext) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
log.info("定时任务 :{} 启动,{}", "DemoJob", formatter.format(LocalDateTime.now()));
}
}

@ -1,15 +1,15 @@
package com.example.application; package com.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
/** /**
* @author q * @author wujingtao
* @date 2021/11/15
*/ */
@SpringBootApplication @SpringBootApplication
public class SpringBootElasticJobApplication { public class ElasticJobTestApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SpringBootElasticJobApplication.class, args); SpringApplication.run(ElasticJobTestApplication.class, args);
} }
} }

@ -6,7 +6,12 @@
<groupId>com.example</groupId> <groupId>com.example</groupId>
<artifactId>elastic-job-springboot</artifactId> <artifactId>elastic-job-springboot</artifactId>
<version>2.0-SNAPSHOT</version> <packaging>pom</packaging>
<version>3.0-SNAPSHOT</version>
<modules>
<module>elastic-job-springboot-core</module>
<module>elastic-job-springboot-test</module>
</modules>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>8</maven.compiler.source>
@ -19,11 +24,11 @@
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<version>2.3.2.RELEASE</version> <version>2.3.2.RELEASE</version>
</dependency> </dependency>
<dependency> <!-- <dependency>-->
<groupId>org.springframework.boot</groupId> <!-- <groupId>org.springframework.boot</groupId>-->
<artifactId>spring-boot-starter-test</artifactId> <!-- <artifactId>spring-boot-starter-test</artifactId>-->
<version>2.3.2.RELEASE</version> <!-- <version>2.3.2.RELEASE</version>-->
</dependency> <!-- </dependency>-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId> <artifactId>spring-boot-configuration-processor</artifactId>
@ -40,16 +45,11 @@
<artifactId>elastic-job-lite-core</artifactId> <artifactId>elastic-job-lite-core</artifactId>
<version>2.1.5</version> <version>2.1.5</version>
</dependency> </dependency>
<dependency> <!-- <dependency>-->
<groupId>com.dangdang</groupId> <!-- <groupId>com.dangdang</groupId>-->
<artifactId>elastic-job-lite-spring</artifactId> <!-- <artifactId>elastic-job-lite-spring</artifactId>-->
<version>2.1.5</version> <!-- <version>2.1.5</version>-->
</dependency> <!-- </dependency>-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>${project.name}</finalName> <finalName>${project.name}</finalName>

@ -1,56 +0,0 @@
package com.example.autoconfig.sharding;
import com.dangdang.ddframe.job.lite.api.strategy.JobInstance;
import com.dangdang.ddframe.job.lite.api.strategy.JobShardingStrategy;
import org.springframework.util.CollectionUtils;
import java.util.*;
/**
* -
*
* @author q
*/
public class MyshardingStrategy implements JobShardingStrategy {
/**
* @param jobInstances
* @param jobName
* @param shardingTotalCount
* @return
*/
@Override
public Map<JobInstance, List<Integer>> sharding(List<JobInstance> jobInstances, String jobName, int shardingTotalCount) {
// 创建返回值容器
Map<JobInstance, List<Integer>> rtnMap = new HashMap<>(10);
// 创建队列模型
// fixme 这个队列可以自行进行选择
ArrayDeque<Integer> queue = new ArrayDeque<>(shardingTotalCount);
for (int i = 0; i < shardingTotalCount; i++) {
queue.add(i);
}
// 轮询
while (queue.size() > 0) {
for (JobInstance jobInstance : jobInstances) {
if (queue.size() > 0){
Integer shardingItem = queue.pop();
List<Integer> shardingItemList = rtnMap.get(jobInstance);
if (CollectionUtils.isEmpty(shardingItemList)){
ArrayList<Integer> list = new ArrayList<>(100);
list.add(shardingItem);
rtnMap.put(jobInstance, shardingItemList);
}else {
shardingItemList.add(shardingItem);
}
}
}
}
return rtnMap;
}
}

@ -1,4 +0,0 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.autoconfig.ZookeeperAutoConfig,\
com.example.autoconfig.DataflowJobAutoConfig,\
com.example.autoconfig.SimpleJobAutoConfig
Loading…
Cancel
Save