重构elasticJob项目结构
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,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.zookeeper.ZookeeperConfiguration;
|
||||
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
|
||||
import com.example.config.ZookeeperProperties;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
@ -1,4 +1,4 @@
|
||||
package com.example.autoconfig;
|
||||
package com.example.config;
|
||||
|
||||
import lombok.Getter;
|
||||
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.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.strategy.impl.AverageAllocationJobShardingStrategy;
|
@ -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.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author q
|
||||
* @author wujingtao
|
||||
* @date 2021/11/15
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class SpringBootElasticJobApplication {
|
||||
|
||||
public class ElasticJobTestApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootElasticJobApplication.class, args);
|
||||
SpringApplication.run(ElasticJobTestApplication.class, args);
|
||||
}
|
||||
}
|
||||
}
|
@ -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…
Reference in New Issue