First, the purpose of this paper
This paper will integrate elastical-job and spring-·boot, and build a task monitoring platform to manage tasks through elastical-job-console. As for the core concepts such as sharding and configuration items involved, please refer to the official website documents. This article is a quick build project, let the scheduled task to run up, the end of the source code address.
2, Elastic job related
Elasticjob. IO /index_zh.ht… elasticJob. IO /index_zh.ht…
Making address github.com/elasticjob/…
3. Set up scheduled task projects
- Creating a Boot project with Spring Initializr is as simple as adding web dependencies and then adding the elastic-job-Lite-core core package
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-core</artifactId>
<version>2.1.5</version>
</dependency>
Copy the code
- Integrate with Spring to add elastic-job-Lite-Spring package
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>2.1.5</version>
</dependency>
Copy the code
- Write periodic task class MyElasticJob, implement SimpleJob interface
package com.morning.morningshiro.jobs;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.morning.morningshiro.dao.entity.UserEntity;
import javax.annotation.Resource;
import java.util.List;
public class MyElasticJob implements SimpleJob {
@Override
public void execute(ShardingContext context) {
// Execute the task according to the sharded item
System.out.println(context.toString());
switch (context.getShardingItem()) {
case 0:
break;
case 1:
// do something by sharding item 1
break;
case 2:
// do something by sharding item 2
break;
// case n: ...}}}Copy the code
- Task listener class JobListener
package com.morning.morningshiro.jobs; import com.dangdang.ddframe.job.executor.ShardingContexts; import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener; public class JobListener implements ElasticJobListener { @Override public void beforeJobExecuted(ShardingContexts shardingContexts) { System.out.println(shardingContexts.toString()); } @Override public void afterJobExecuted(ShardingContexts shardingContexts) { System.out.println(shardingContexts.toString()); }}Copy the code
- Write the spring-elastic-job.xml file and place it in
resources
Under the file
<?xml version="1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:reg="http://www.dangdang.com/schema/ddframe/reg"
xmlns:job="http://www.dangdang.com/schema/ddframe/job"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.dangdang.com/schema/ddframe/reg http://www.dangdang.com/schema/ddframe/reg/reg.xsd http://www.dangdang.com/schema/ddframe/job http://www.dangdang.com/schema/ddframe/job/job.xsd ">
<! -- Configure the Job Registry -->
<! --namespace is the name registered with zK -->
<reg:zookeeper id="regCenter" server-lists="127.0.0.1:2181" namespace="dd-job" base-sleep-time-milliseconds="1000"
max-sleep-time-milliseconds="3000" max-retries="3"/>
<bean id="myElasticJob" class="com.morning.morningshiro.jobs.MyElasticJob"/>
<! -- Configuration job -->
<! --overwrite Overwrite the original scheduled task -->
<! Task id id -- -- >
<job:simple id="job1" job-ref="myElasticJob" overwrite="true" registry-center-ref="regCenter"
cron="0/30 * * * *?" sharding-total-count="4" sharding-item-parameters="0=USER,1=TWO" description="test">
<job:listener class="com.morning.morningshiro.jobs.JobListener"/>
</job:simple>
</beans>
Copy the code
- The startup class imports the XML configuration file
@SpringBootApplication
@ImportResource(locations = {"classpath:spring-elastic-job.xml"})
public class MorningShiroApplication {
public static void main(String[] args) { SpringApplication.run(MorningShiroApplication.class, args); }}Copy the code
- Zk View the task node
Iv. Console construction
Using the elastic job-console project to perform monitoring tasks, github.com/elasticjob/…
Download the project and import Idea to start the elastic job-lite-console project. Before starting the project, change the Spring framework version in the parent POM file to 5.1.9.RELEASE, and then start the project. In the browser, enter localhost:8899 to access the console.
4.1 Configuring the ZK service address on the Console Page
4.2 Viewing the Task List
4.3 Viewing the Service List
4.4 Modifying a Task
For more information about zK cluster setup: juejin.cn/post/684490…
Source address: github.com/alwyngo/mor…