Elastic-Job
Elastic-Job what is the use
Elastic-job to solve the problem, we developed regular tasks usually use quartz or spring-task, using either quartz or spring-task, we will encounter at least two pain points:
- Application services can not easily follow a multi-node deployment, may be repeated multiple times triggered system logic errors.
- quartz cluster just to HA, does not increase the number of nodes each can bring to enhance our efficiency, that can not achieve the level of expansion.
In simple terms is to resolve multiple machines to repeat the scheduled tasks or modify the configuration requirements specify a fixed single machine to perform regular tasks troubles at the moment distributed cluster prevailing background, while avoiding a specific station if the timing of the task execution machine dang machine cause embarrassment regular tasks can not be performed.
Ado I think we can see these presentations in other places, we direct the dry!
A, Mavne dependence
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-core</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>2.1.3</version>
</dependency>
Second, the timing of the job creation business class
package com.zhibo.elastic.job;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author zhibo.lv
* @title: TestJob
* @description: Test定时任务
* @date 2019/12/2 14:15
*/
@Slf4j
@Component
public class TestJob implements SimpleJob {
@Override
public void execute(ShardingContext shardingContext) {
log.info("[elastic job TestJob] begin----------------");
//如有设置分片获取当前任务的分片编号
int shardingItem = shardingContext.getShardingItem();
System.out.println("Job 分片处理:" + shardingItem);
//自行添加业务代码 ----
log.info("[elastic job TestJob] end----------------");
}
}
Third, the configuration elasticJob.xml
digest=" {} Zookeeper.password "This configuration will be described below generally do not have access
<?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">
<reg:zookeeper id="regCenter" server-lists="${job.registry.address}" namespace="zhibo-elastic-job"
base-sleep-time-milliseconds="10000" max-retries="3" max-sleep-time-milliseconds="30000"
digest="${zookeeper.username}:${zookeeper.password}"/>
<job:simple id="activeAdminInfoJob" class="com.zhibo.elastic.job.TestJob"
registry-center-ref="regCenter"
sharding-total-count="1"
cron="0 * * * * ? "
sharding-item-parameters="0=0"
monitor-execution="false"
monitor-port="9888"
failover="true"
description="Test Job"
disabled="false"
overwrite="true"/>
</beans>
Do not forget to introduce elasticJob.xml in which applicationContext.xml
reg: zookeeper namespace property details
Property name | Types of | Required | The default value | description |
---|---|---|---|---|
id | String | Yes | Id registry in the Spring container | |
server-lists | String | Yes | Zookeeper server address and port multiple addresses separated by commas |
|
namespace | String | Yes | Zookeeper namespace is recommended that each project defines a unique name used to distinguish |
|
base-sleep-time-milliseconds | int | no | 1000 | The initial value of the waiting time of the retry interval (ms) |
max-sleep-time-milliseconds | int | no | 3000 | Retry latency time of the maximum value (in milliseconds) |
max-retries | int | no | 3 | The maximum number of retries |
session-timeout-milliseconds | int | no | 60000 | Session Timeout (ms) |
connection-timeout-milliseconds | int | no | 15000 | Connection time (ms) |
digest | String | no | Authentication is not directly connected | Zookeeper connection permission token |
job: simple namespace property details
Property name | Types of | Required | The default value | description |
---|---|---|---|---|
id | String | Yes | Job name be unique under the same namespace |
|
registry-center-ref | String | Yes | Bean's reference registry with reg: consistent value to the id attribute of the zookeeper |
|
cron | String | Yes | cron expression that is used to configure the trigger time job just to find the expression of learning address |
|
sharding-total-count | int | Yes | Total number of jobs fragment need not be fragmented wrote 1 |
|
class | String | no | Job implementation class, to be achieved by ElasticJob interface requires no configuration script type jobs | |
sharding-item-parameters | String | no | Sequence number and fragment parameters are separated by an equal sign, a plurality of key-value pairs separated by a comma fragment sequence number from zero, not equal to, or greater than the total number of jobs, such as slice: 0 = A, B =. 1, C 2 = |
|
job-parameter | String | no | Job custom parameter code by shardingContext.getJobParameter () Gets |
|
monitor-execution | boolean | no | true | Status monitoring job runs every case execution time job and are very short interval of time, it is recommended not to monitor the status of the job is running to improve efficiency. Because it is a transient state, so no need to monitor. Please users to add their own data accumulation monitoring. Repeating selected data and can not be guaranteed, to be achieved in idempotent operations. Job execution time and each time interval are longer, it is recommended to monitor the job running state, may be selected to ensure that data is not repeated. |
monitor-port | int | no | -1 | Job Monitor port |
max-time-diff-seconds | int | no | -1 | The maximum allowable time error and the number of seconds the machine registry will throw if the time error exceeds the set number of seconds the job start-up abnormality configured to -1 does not verify time error |
failover | boolean | no | false | Whether to open the failover only monitorExecution open, failover is valid |
misfire | boolean | no | true | Whether to open the missed task re-run |
description | String | no | Job Description Information | |
disabled | boolean | no | false | The job is Never start time can be used for deployment, the first ban started after the end of the deployment of the unified start |
overwrite | boolean | no | false | Whether local configuration can cover registry configuration does not take effect covering the recommended configuration modified to avoid |