Scheduling and monitoring -spring batch (7) binding xxl-job batch

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/masson32/article/details/91503723

Scheduling and monitoring -spring batch (7) binding xxl-job batch

tags: springbatch


1 Introduction

After previous articles on the Spring Batchintroduction of combined sample, from the most simple helloworlddata string output, to read the file to the database synchronization and database to database, and then combined BeetlSqlfurther 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 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 Batchit is a batch framework, not a scheduling framework. Scheduling framework, you can use simple point quartz, crontabwhile the market has a relatively versatile perfect scheduling framework, by comparison, personal feeling xxl-jobis relatively easy to use, and relatively complete function, therefore, to introduce here, the use xxl-jobof Spring Batchbatch tasks scheduling, monitoring functions to achieve.

2.xxl-job introduction

xxl-jobIs 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-jobthe document, to install xxl-jobthe database, and then xxl-job-adminstart up, start up page as follows:

xxl-job interface

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-jobofficial 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-jobcore 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 javaprofiles for above- executor.propertiesconfiguration. 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 IJobHandlerimplementation class, in the executerealization 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-adminand spring-batch-xxl-executorafter deployment (note configured correctly), you can xxl-job-admintask scheduling interface. Specific process, you can add a reference to xxl-jobthe document can be. It simply is adding actuators spring-batch-xxl-executor, then add tasks incrementUserJobHandler, specify the execution of cronan expression, you can start. After the mission the following output:

Results of the

5. Summary

This article is based on the sample database incremental synchronization, use Spring Batchin combination xxl-jobto achieve the task scheduler that can be monitored, so that batch jobs more maintainable, hope requires Spring Batchpersonnel to help develop a batch job.

Guess you like

Origin blog.csdn.net/masson32/article/details/91503723