springboot集成xxl-job

1.下载xxl-job-admin源码

1
2
3
ruby复制代码下载地址: https://github.com/xuxueli/xxl-job/releases
文档地址: https://www.xuxueli.com/xxl-job/
本人下载版本为2.3.0

2.执行数据库脚本

1
复制代码脚本存放路径: xxl-job-2.3.0\doc\db\tables_xxl_job.sql

3.修改配置文件

1
2
css复制代码配置文件路径: xxl-job-2.3.0\xxl-job-admin\src\main\resources\application.properties
修改数据库配置和邮箱配置,如下:

image.png

4.启动xxl-job-admin项目

1
css复制代码启动类路径: xxl-job-2.3.0\xxl-job-admin\src\main\java\com\xxl\job\admin\XxlJobAdminApplication.java

5.浏览器查看

1
2
bash复制代码地址: http://localhost:8080/xxl-job-admin
用户名 admin 密码 123456

image.png

6.引入maven配置

1
复制代码在另一个springboot应用中添加maven配置
1
2
3
4
5
xml复制代码<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>

7.配置文件修改

1
复制代码application.yml

image.png

8.编写配置类XxlJobConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
kotlin复制代码
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* xxl-job config
*
* @author xuxueli 2017-04-28
*/
@Configuration
public class XxlJobConfig {
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

@Value("${xxl.job.admin.addresses}")
private String adminAddresses;

@Value("${xxl.job.executor.appname}")
private String appName;

@Value("${xxl.job.executor.ip}")
private String ip;

@Value("${xxl.job.executor.port}")
private int port;

@Value("${xxl.job.accessToken}")
private String accessToken;

@Value("${xxl.job.executor.logpath}")
private String logPath;

@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;

@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobSpringExecutor xxlJobExecutor() {
logger.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;
}

}

9.编写测试任务类

1
2
3
4
5
6
7
8
9
10
11
12
13
scala复制代码import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

@Component
public class SampleXxlJobHandler extends IJobHandler {

@Override
@XxlJob(value = "sampleXxlJobHandler")
public void execute() throws Exception {
System.out.println("自动任务" + this.getClass().getSimpleName() + "执行");
}
}

10.新增执行器

1
复制代码在执行器管理页面,点击新增按钮,弹出新增框。输入AppName (与第7点中配置的appname保持一致),名称,注册方式默认自动注册,点击保存。

image.png

11.新增自动任务

1
复制代码在任务管理页面,选择上一步新建的执行器,点击新增按钮,弹出新增框。输入任务描述,负责人,cron表达式(输入框右侧有按钮,点开可以辅助自动生成cron表达式),jobHandler(需要与第9步中的XxlJob注解中的值一致),点击保存。

image.png

12.执行自动任务

1
2
3
复制代码启动配置自动任务的springboot服务,在任务管理页面选中要执行的自动任务操作按钮右侧下拉三角,选择执行一次,查看执行结果。
正常开发中,点击启动保持任务一直执行。
在调度日志页面,可以搜索查看日志。

image.png

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%