Some time ago, my boss asked me to use springboot to integrate xxl-job. Here are some of my integration steps, typed word by word. I hope it will be useful to you. Thank you for reading.
This example is based on the Ruoyi framework (Ruoyi has its own scheduled tasks). This example is just for learning.
Ruo Yi:
https://gitee.com/y_project/RuoYi-fast
XXL-JOB:https://www.xuxueli.com/xxl-job/
First, start the Ruoyi project:
Step 2: Get the xxl source code:
https://gitee.com/xuxueli0323/xxl-job
Then use idea to open xxl-job-admin and configure the database
Then start access:
http://127.0.0.1:8080/xxl-job-admin/toLogin
Default account: admin Default password: 123456
Interface after successful login:
Step 3: Add to the pom.xml of the Ruoyi project
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
Step 4: Add to application.yml of Ruoyi project
xxl:
job:
admin:
addresses: http://127.0.0.1:8080/xxl-job-admin
executor:
appname: xxl-job-executor-sample # 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
address:
ip:
port: 9999
logpath: G:/
logretentiondays: -1
accessToken: ### 执行器通讯TOKEN [选填]:非空时启用;
Then create a new class (XxlJobConfig)
@Configuration
public class XxlJobConfig {
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.appname}")
private String appname;
@Value("${xxl.job.executor.address}")
private String address;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
xxlJobSpringExecutor.setAddress(address);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
Then start the Ruoyi project and go to the dispatch center again to check the executor:
If you find that the registration is successful, it means the integration is successful.
Step 5: Add a task processing Class to the Ruoyi project, the content is as follows
@Component
public class TestJob {
@XxlJob("testHandler")
public void testHandler() throws Exception {
System.out.println("我执行了------------");
}
}
Step 6: Configure task management
Step 7: Execute and view
. It is found that it can be executed successfully. The executor in it indicates that the integration is successful.
ps: It is not easy to integrate. I hope to collect it. Maybe it will be useful to you in the future.