当前位置:网站首页>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:2181
Introduce 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 .
边栏推荐
- Andorid source build/envsetup. SH details to know
- Findbugs modification summary
- Rotating colored clover
- 自动融合,驰骋海外丨跨境电商YescomUSA携手云扩实现一站式自动化服务
- Will the flush SQL CDC parallelism affect the order? Generally, only 1 can be set for data synchronization.
- 证券开户有优惠吗究竟网上开户是否安全么?
- Mysql:sql overview and database system introduction | dark horse programmer
- @ConfigurationProperties使用不当引发的bug
- [advanced C language] file operation (I)
- Activity invitation | the Apache Doris community essay and speech solicitation activity has begun!
猜你喜欢
Some specifications based on zfoo development project
云呐|固定资产系统管理,nc系统管理固定资产在哪里
网工常见面试题分享:Telnet、TTL、路由器与交换机
网络方向哪个发展更好?数据通信工程师学习路线分享
@ConfigurationProperties使用不当引发的bug
Statistical query of SQL Server database
学位论文的引用
云呐|固定资产系统管理的优势,固定资产管理系统有何特点
Quick pow: how to quickly find power
What is the essential difference between get and post requests?
随机推荐
Solr basic operations 15
Quick Pow: 如何快速求幂
Summarize Flink runtime architecture in simple terms
How about stock online account opening and account opening process? Also, is it safe to open an account online?
Sofaregistry source code | data synchronization module analysis
面试官:为什么数据库连接很消耗资源?我竟然答不上来。。一下懵了!
Statistical query of SQL Server database
证券开户有优惠吗究竟网上开户是否安全么?
Getting started with qpainter: drawing the chess interface
Solr basic operations 14
【PHP】php压测,报错:通常每个套接字地址(协议/网络地址/端口)只允许使用
Summary of DOM knowledge points
俞敏洪:我的退与进;架构师必须了解的5种最佳软件架构模式;Redis夺命52连问|码农周刊VIP会员专属邮件周报 Vol.096
Is there any discount for securities account opening? Is it safe to open an account online?
Three postures of anti CSRF blasting
[PHP] PHP variable memory release
Solr basic operations 12
MySQL高级篇1
Sword finger offer II 037 Asteroid collision
利用 CertBot 申请 Let’s Encrypt SSL 证书