当前位置:网站首页>Distributed task scheduling elasticjob demo
Distributed task scheduling elasticjob demo
2022-06-30 00:28:00 【tang_ sy】
Code address :GitHub - augtsy/springboot-elasticJob-demo: ElasticJob Distributed task scheduling ElasticJob Distributed task scheduling . Contribute to augtsy/springboot-elasticJob-demo development by creating an account on GitHub.
https://github.com/augtsy/springboot-elasticJob-demo
One 、 Common open source products
Quartz、XXL-Job、ElasticJob etc.
- Quartz: The framework is the most widely used , It's all based on Java Realization ,Quartz The control of a single task is basically the ultimate , With its powerful function and application flexibility , Become the authority in the field of open source task scheduling and similar open source products such as Antares Cornerstone ;
- XXL-JOB: A lightweight distributed task scheduling platform , Its core design goal is rapid development 、 Learn easy 、 Lightweight 、 Easy to expand .XXL-JOB Support fragmentation , Support simple task dependencies , Support subtask dependencies , Cross platform is not supported .
- Elastic-Job: Support task sharding ( Consistency of work segments ), No choreography , Cross-platform is not supported ;
Two 、 Business scenario
Company task center , Provide scheduled task processing such as customer import and export .
3、 ... and 、 Code implementation
elastic-job rely on zookeeper, First of all, install zookeeper. Sure docker Start the image, etc .
properties To configure
info.app.name=elastic-job-demo
zookeeper.servers=172.19.8.76:2181Introduce dependencies
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-core</artifactId>
<version>2.1.5</version>
</dependency>
<!-- elastic-job-lite-spring -->
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>2.1.5</version>
</dependency>Registry configuration class
@Configuration
public class ElasticRegCenterConfig {
/**
* To configure zookeeper
*
* @param serverList
* @param namespace
* @return
*/
@Bean(initMethod = "init")
public ZookeeperRegistryCenter zookeeperRegistryCenter(
@Value("${zookeeper.servers}") final String serverList,
@Value("${info.app.name}") final String namespace) {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
}
}Annotation class
/**
* @Description: ElasticJob Enumeration configuration
* @Auther: tsy
* @Date: 2022/06/24/2:08 Afternoon
*/
@Inherited
@Documented
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.TYPE)
public @interface ElasticJobConfig {
String cron() default "";
String value() default "";
/**
* Total number of segments
*/
int shardingTotalCount() default 1;
/**
* Partition item parameters
*/
String shardingItemParameters() default "0=0";
}
SimpleJob Configuration class
/**
* @Description: SimpleJob To configure
* @Auther: tsy
* @Date: 2022/06/24/2:21 Afternoon
*/
@Slf4j
@Configuration
public class SimpleJobConfig {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private ZookeeperRegistryCenter zookeeperRegistryCenter;
@PostConstruct
public void startSimpleJob() {
applicationContext.getBeansWithAnnotation(ElasticJobConfig.class).forEach((className, obj) -> {
ElasticJobConfig config = obj.getClass().getAnnotation(ElasticJobConfig.class);
// expression
String cron = StringUtils.defaultIfBlank(config.cron(), config.value());
// Total number of segments
int shardingTotalCount = config.shardingTotalCount();
// Partition item parameters
String shardingItemParameters = config.shardingItemParameters();
JobListener elasticJobListener = new JobListener();
SimpleJob simpleJob = (SimpleJob) obj;
new SpringJobScheduler(simpleJob, zookeeperRegistryCenter,
getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters),
elasticJobListener).init();
});
}
/**
* Create a simple job configuration builder .
*
* @param jobName Job name
* @param cron Of the job start time cron expression
* @param shardingTotalCount Total number of job segments
* @return Simple job configuration builder
*/
private LiteJobConfiguration getLiteJobConfiguration(Class<?> jobName, String cron, int shardingTotalCount, String shardingItemParameters) {
return LiteJobConfiguration.newBuilder(
new SimpleJobConfiguration(
JobCoreConfiguration.newBuilder(jobName.getName(), cron, shardingTotalCount)
.shardingItemParameters(shardingItemParameters)
.build(),
jobName.getCanonicalName()))
.monitorExecution(true)
.overwrite(false).build();
}
}Timed task implementation
/**
* @Description: Business scheduled tasks
* @Auther: tsy
* @Date: 2022/06/24/2:36 Afternoon
*/
@Slf4j
@Component
@ElasticJobConfig(cron = "*/4 * * * * ?", shardingTotalCount = 5, shardingItemParameters = "0=0,1=1,2=2,3=3,4=4")
public class SimpleJobDemo implements SimpleJob {
@Override
public void execute(ShardingContext shardingContext) {
// Get the total number of slices
int shardingTotalCount = shardingContext.getShardingTotalCount();
// Get fragment item
int shardingItem = shardingContext.getShardingItem();
// Get partition item parameters
String shardingParameter = shardingContext.getShardingParameter();
log.info(" Total number of segments :{} Piecewise item :{} Partition item parameters :{}", shardingTotalCount, shardingItem, shardingParameter);
// Business logic ,ElasticJob It does not provide the function of data processing , The framework will only assign sharding items to each running job server , Developers need to deal with it themselves
}
}Four 、 test
Start only one instance , As a result, all partitions are assigned to this instance .

Start the other four instances , result 5 Pieces are evenly divided 5 A node .

5、 ... and 、 Registry node view

Postscript : Pretty good , generous with your criticism .
边栏推荐
- Solr basic operations 9
- Cloud native enthusiast weekly: cool collection of grafana monitoring panels
- After 8 years of polishing, "dream factory of game design" released an epic update!
- VIM plug in manager VIM plug installation method
- [QNX Hypervisor 2.2用户手册]6.2.2 Guest与Host之间通信
- How to seamlessly transition from traditional microservice framework to service grid ASM
- 字节、字、双字 关系
- Getting started with qpainter: drawing the chess interface
- 云呐|固定资产信息系统管理,信息化固定资产管理
- 【PHP】PHP变量内存释放
猜你喜欢

This PMP Exam (June 25), some people are happy and others are worried. That's why

What is the essential difference between get and post requests?

博途V16 更改PLC的型号和固件版本

HDCP Paring

Serpentine matrix (array simulates direction, D represents turning)

Some specifications based on zfoo development project

Five key elements of the data center

Stack space of JVM

How to write controller layer code gracefully?
![[dynamic programming] - linear DP](/img/cb/4ad8dce1da1d1110d6e79fadca4293.png)
[dynamic programming] - linear DP
随机推荐
Fine grained identification, classification, retrieval data set collation
EB-5 immigration in the United States reappears to be positive, and the reauthorization policy of the regional center is suspended
Five key elements of the data center
[review and Book delivery] 6 interesting R language projects for beginners
【毕业季|进击的技术er】工作七年的职场人,不希望你们再走弯路
[rust weekly library] Tokei - a utility for statistics of code lines and other information
Web APIs environment object - dark horse programmer
Move DataGridView up and down
Sword finger offer II 035 Minimum time difference
【编程题】迷宫问题
蛇形矩阵(数组模拟方向, d代表转弯)
mysql 死锁
Solr basic operation 8
关联性——典型相关分析
【服装软件】服装出产办理体系选型的准则有哪些?
将日志文件存储至 RAM 以降低物理存储损耗
数据库学习笔记(SQL03)
Sofaregistry source code | data synchronization module analysis
网络方向哪个发展更好?数据通信工程师学习路线分享
HDCP Paring
https://github.com/augtsy/springboot-elasticJob-demo