当前位置:网站首页>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 .
边栏推荐
- 【每日一题】二叉树的前后序遍历
- 利用 CertBot 申请 Let’s Encrypt SSL 证书
- leetcode-1. 两数之和
- Byte, word, doubleword relationship
- Statistical query of SQL Server database
- Mysql:sql overview and database system introduction | dark horse programmer
- 云呐|固定资产系统管理,nc系统管理固定资产在哪里
- Cloud native enthusiast weekly: cool collection of grafana monitoring panels
- 传统微服务框架如何无缝过渡到服务网格 ASM
- Floating point communication
猜你喜欢

自动融合,驰骋海外丨跨境电商YescomUSA携手云扩实现一站式自动化服务

TwinCAT 3 el7211 module controls Beifu servo

Root cause of glideexception: failed decodepath{directbytebuffer- > gifdrawable- > drawable}

Three postures of anti CSRF blasting

面试官:为什么数据库连接很消耗资源?我竟然答不上来。。一下懵了!
![[review and Book delivery] 6 interesting R language projects for beginners](/img/d9/b785c92f503b78977b47a7feb88776.jpg)
[review and Book delivery] 6 interesting R language projects for beginners

Serpentine matrix (array simulates direction, D represents turning)

HDCP Paring

数据中台的五个关键要素

Finding a job in 2022 is the "last lesson" for graduates
随机推荐
[review and Book delivery] 6 interesting R language projects for beginners
Solr basic operations 13
剑指 Offer II 035. 最小时间差
自动融合,驰骋海外丨跨境电商YescomUSA携手云扩实现一站式自动化服务
【UML】UML的几种关系(依赖-关联-聚合-组合-继承-实现)
SSH key disclosure (module B competition topic) -- Application Service Vulnerability scanning and utilization
DataGridView上移 下移行
【毕业季|进击的技术er】工作七年的职场人,不希望你们再走弯路
MySQL basics 1
Which securities company is better and which platform is safer for stock speculation account opening
HDCP Paring
Applying for let's encrypt SSL certificate with certbot
[advanced C language] file operation (II)
微信支付 APP端 第三弹 申请退款
固定资产管理系统多少钱,固定资产管理系统价格
Quick Pow: 如何快速求幂
leetcode 416. Partition equal subset sum partition equal subset sum (medium)
MySQL primary key constraint deletion
[advanced C language] file operation (I)
vsftp 与 TFTP 与 samba 与 nfs 复习
https://github.com/augtsy/springboot-elasticJob-demo