Scheduling and monitoring -spring batch (7) binding xxl-job batch
tags: springbatch
Article Directory
- Scheduling and monitoring -spring batch (7) binding xxl-job batch
- 1 Introduction
- 2.xxl-job introduction
- 3. Write Actuator Spring Batch
- 3.1 Add rely maven
- 3.2 Adding actuator configuration file `executor.properties`
- 3.3 actuator assembly configuration
- 3.4 write Actuator
- 4. Use xxl-job for scheduling Spring Batch
- 5. Summary
1 Introduction
After previous articles on the Spring Batch
introduction of combined sample, from the most simple helloworld
data string output, to read the file to the database synchronization and database to database, and then combined BeetlSql
further simplified to read and write the database, then tied by dynamic parameters given achieve incremental synchronization, from shallow to deep, already basically meet the data extraction, data synchronization work, here is a list of previous article:
- Batch data artifact -Spring Batch (1) Introduction and usage scenarios
- A quick overview of the components -spring batch (2) of the helloworld
- Quick assembly -spring batch (3) reads file data into the database
- Final database -spring batch (4) database to database
- Easy data read -spring batch (5) to read and write data in conjunction beetlSql
- Incremental synchronization -spring batch (6) binding and dynamic parameters incremental synchronization
Batch jobs, generally running as a background service regular service operation and maintenance work is an important task than the batch services, including how to monitor services running, run the task statistics, view the task execution log and so on. Previously mentioned, Spring Batch
it is a batch framework, not a scheduling framework. Scheduling framework, you can use simple point quartz
, crontab
while the market has a relatively versatile perfect scheduling framework, by comparison, personal feeling xxl-job
is relatively easy to use, and relatively complete function, therefore, to introduce here, the use xxl-job
of Spring Batch
batch tasks scheduling, monitoring functions to achieve.
2.xxl-job introduction
xxl-job
Is a lightweight distributed task scheduling platform, its core design goal is to develop rapid, simple to learn, lightweight, easy to expand. System architecture design is reasonable, the scheduling system with separate actuator, scheduling system is responsible for scheduling logic associated with the actuator is a logic to achieve specific tasks, developers can use the scheduling system directly, and then logic to achieve their mission to do that is for the executor can, so out of the box. Specific use and detailed instructions, see its official documents
According to xxl-job
the document, to install xxl-job
the database, and then xxl-job-admin
start up, start up page as follows:
3. Write Actuator Spring Batch
This example is based on an article that used the sample database incremental synchronization , only need to add a device to perform on the basis of the original, this is an example spring-batch-xxl-executor
. According to xxl-job
official documents, there are instructions on how to configure the deployment "actuator project" . Specific to the present example, arranged to achieve the following:
3.1 Add rely maven
Add xxl-job
core dependencies, as follows:
<!-- xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.0.2</version>
</dependency>
3.2 Adding actuator profileexecutor.properties
The actuator includes an actuator arranged to fill the name, address and other scheduling system, as follows:
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8089/xxl-job-admin
### xxl-job executor address
xxl.job.executor.appname=${spring.application.name}
xxl.job.executor.ip=
xxl.job.executor.port=9999
### xxl-job, access token
xxl.job.accessToken=
### xxl-job log path
xxl.job.executor.logpath=logs
### xxl-job log retention days
xxl.job.executor.logretentiondays=-1
3.3 actuator assembly configuration
Add java
profiles for above- executor.properties
configuration. as follows:
@Configuration
@ConfigurationProperties(prefix = "xxl.job")
@PropertySource("classpath:/config/executor.properties")
@Slf4j
public class JobExecutorConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
......略
@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobSpringExecutor xxlJobExecutor() {
log.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppName(appName);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
3.4 write Actuator
Actuators, in fact, inherited the IJobHandler
implementation class, in the execute
realization of the task to implement the logical method. In this example, the logic of the task before simply press the start of the test can be achieved. as follows:
@JobHandler(value="incrementUserJobHandler")
@Component
public class JobIncrementUserHandler extends IJobHandler {
@Autowired
private JobLauncherService jobLauncherService;
@Autowired
private IncrementService incrementService;
@Autowired
private Job incrementJob;
@Override
public ReturnT<String> execute(String s) throws Exception {
return JobUtil.runJob4Executor("incrementUser",incrementService,jobLauncherService,incrementJob);
}
}
4. Use xxl-job for scheduling Spring Batch
To xxl-job-admin
and spring-batch-xxl-executor
after deployment (note configured correctly), you can xxl-job-admin
task scheduling interface. Specific process, you can add a reference to xxl-job
the document can be. It simply is adding actuators spring-batch-xxl-executor
, then add tasks incrementUserJobHandler
, specify the execution of cron
an expression, you can start. After the mission the following output:
5. Summary
This article is based on the sample database incremental synchronization, use Spring Batch
in combination xxl-job
to achieve the task scheduler that can be monitored, so that batch jobs more maintainable, hope requires Spring Batch
personnel to help develop a batch job.