Skip to content

Commit 750cc33

Browse files
committed
✨ spring-boot-demo-task-xxl-job完成
1 parent 94da2cb commit 750cc33

File tree

8 files changed

+364
-1
lines changed

8 files changed

+364
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/

spring-boot-demo-task-xxl-job/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2121
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2222
<java.version>1.8</java.version>
23+
<xxl-job.version>2.1.0</xxl-job.version>
2324
</properties>
2425

2526
<dependencies>
@@ -28,11 +29,24 @@
2829
<artifactId>spring-boot-starter-web</artifactId>
2930
</dependency>
3031

32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-configuration-processor</artifactId>
35+
<optional>true</optional>
36+
</dependency>
37+
3138
<dependency>
3239
<groupId>mysql</groupId>
3340
<artifactId>mysql-connector-java</artifactId>
3441
</dependency>
3542

43+
<!-- xxl-job-core -->
44+
<dependency>
45+
<groupId>com.xuxueli</groupId>
46+
<artifactId>xxl-job-core</artifactId>
47+
<version>${xxl-job.version}</version>
48+
</dependency>
49+
3650
<dependency>
3751
<groupId>org.springframework.boot</groupId>
3852
<artifactId>spring-boot-starter-test</artifactId>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.xkcoding.task.xxl.job;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* <p>
8+
* 启动器
9+
* </p>
10+
*
11+
* @author yangkai.shen
12+
* @date Created in 2019-08-07 10:13
13+
*/
14+
@SpringBootApplication
15+
public class SpringBootDemoTaskXxlJobApplication {
16+
17+
public static void main(String[] args) {
18+
SpringApplication.run(SpringBootDemoTaskXxlJobApplication.class, args);
19+
}
20+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.xkcoding.task.xxl.job.config;
2+
3+
import com.xkcoding.task.xxl.job.config.props.XxlJobProps;
4+
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
/**
13+
* <p>
14+
* xxl-job 自动装配
15+
* </p>
16+
*
17+
* @author yangkai.shen
18+
* @date Created in 2019-08-07 10:20
19+
*/
20+
@Slf4j
21+
@Configuration
22+
@EnableConfigurationProperties(XxlJobProps.class)
23+
@RequiredArgsConstructor(onConstructor_ = @Autowired)
24+
public class XxlJobConfig {
25+
private final XxlJobProps xxlJobProps;
26+
27+
@Bean(initMethod = "start", destroyMethod = "destroy")
28+
public XxlJobSpringExecutor xxlJobExecutor() {
29+
log.info(">>>>>>>>>>> xxl-job config init.");
30+
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
31+
xxlJobSpringExecutor.setAdminAddresses(xxlJobProps.getAdmin().getAddress());
32+
xxlJobSpringExecutor.setAccessToken(xxlJobProps.getAccessToken());
33+
xxlJobSpringExecutor.setAppName(xxlJobProps.getExecutor().getAppName());
34+
xxlJobSpringExecutor.setIp(xxlJobProps.getExecutor().getIp());
35+
xxlJobSpringExecutor.setPort(xxlJobProps.getExecutor().getPort());
36+
xxlJobSpringExecutor.setLogPath(xxlJobProps.getExecutor().getLogPath());
37+
xxlJobSpringExecutor.setLogRetentionDays(xxlJobProps.getExecutor().getLogRetentionDays());
38+
39+
return xxlJobSpringExecutor;
40+
}
41+
42+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.xkcoding.task.xxl.job.config.props;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
/**
7+
* <p>
8+
* xxl-job 配置
9+
* </p>
10+
*
11+
* @author yangkai.shen
12+
* @date Created in 2019-08-07 10:25
13+
*/
14+
@Data
15+
@ConfigurationProperties(prefix = "xxl.job")
16+
public class XxlJobProps {
17+
/**
18+
* 调度中心配置
19+
*/
20+
private XxlJobAdminProps admin;
21+
22+
/**
23+
* 执行器配置
24+
*/
25+
private XxlJobExecutorProps executor;
26+
27+
/**
28+
* 与调度中心交互的accessToken
29+
*/
30+
private String accessToken;
31+
32+
@Data
33+
public static class XxlJobAdminProps {
34+
/**
35+
* 调度中心地址
36+
*/
37+
private String address;
38+
}
39+
40+
@Data
41+
public static class XxlJobExecutorProps {
42+
/**
43+
* 执行器名称
44+
*/
45+
private String appName;
46+
47+
/**
48+
* 执行器 IP
49+
*/
50+
private String ip;
51+
52+
/**
53+
* 执行器端口
54+
*/
55+
private int port;
56+
57+
/**
58+
* 执行器日志
59+
*/
60+
private String logPath;
61+
62+
/**
63+
* 执行器日志保留天数,-1
64+
*/
65+
private int logRetentionDays;
66+
}
67+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.xkcoding.task.xxl.job.controller;
2+
3+
import cn.hutool.http.HttpResponse;
4+
import cn.hutool.http.HttpUtil;
5+
import cn.hutool.json.JSONUtil;
6+
import com.google.common.collect.Maps;
7+
import com.xxl.job.core.enums.ExecutorBlockStrategyEnum;
8+
import com.xxl.job.core.glue.GlueTypeEnum;
9+
import lombok.RequiredArgsConstructor;
10+
import lombok.extern.slf4j.Slf4j;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
import java.util.Map;
17+
18+
/**
19+
* <p>
20+
* 手动操作 xxl-job
21+
* </p>
22+
*
23+
* @author yangkai.shen
24+
* @date Created in 2019-08-07 14:58
25+
*/
26+
@Slf4j
27+
@RestController
28+
@RequestMapping("/xxl-job")
29+
@RequiredArgsConstructor(onConstructor_ = @Autowired)
30+
public class ManualOperateController {
31+
private final static String baseUri = "http://127.0.0.1:18080/xxl-job-admin";
32+
private final static String JOB_INFO_URI = "/jobinfo";
33+
private final static String JOB_GROUP_URI = "/jobgroup";
34+
35+
/**
36+
* 任务组列表,xxl-job叫做触发器列表
37+
*/
38+
@GetMapping("/group")
39+
public String xxlJobGroup() {
40+
HttpResponse execute = HttpUtil.createGet(baseUri + JOB_GROUP_URI + "/list").execute();
41+
log.info("【execute】= {}", execute);
42+
return execute.body();
43+
}
44+
45+
/**
46+
* 分页任务列表
47+
* @param page 当前页,第一页 -> 0
48+
* @param size 每页条数,默认10
49+
* @return 分页任务列表
50+
*/
51+
@GetMapping("/list")
52+
public String xxlJobList(Integer page, Integer size) {
53+
Map<String, Object> jobInfo = Maps.newHashMap();
54+
jobInfo.put("start", page != null ? page : 0);
55+
jobInfo.put("length", size != null ? size : 10);
56+
jobInfo.put("jobGroup", 2);
57+
jobInfo.put("triggerStatus", -1);
58+
59+
HttpResponse execute = HttpUtil.createGet(baseUri + JOB_INFO_URI + "/pageList").form(jobInfo).execute();
60+
log.info("【execute】= {}", execute);
61+
return execute.body();
62+
}
63+
64+
/**
65+
* 测试手动保存任务
66+
*/
67+
@GetMapping("/add")
68+
public String xxlJobAdd() {
69+
Map<String, Object> jobInfo = Maps.newHashMap();
70+
jobInfo.put("jobGroup", 2);
71+
jobInfo.put("jobCron", "0 0/1 * * * ? *");
72+
jobInfo.put("jobDesc", "手动添加的任务");
73+
jobInfo.put("author", "admin");
74+
jobInfo.put("executorRouteStrategy", "ROUND");
75+
jobInfo.put("executorHandler", "demoTask");
76+
jobInfo.put("executorParam", "手动添加的任务的参数");
77+
jobInfo.put("executorBlockStrategy", ExecutorBlockStrategyEnum.SERIAL_EXECUTION);
78+
jobInfo.put("glueType", GlueTypeEnum.BEAN);
79+
80+
HttpResponse execute = HttpUtil.createGet(baseUri + JOB_INFO_URI + "/add").form(jobInfo).execute();
81+
log.info("【execute】= {}", execute);
82+
return execute.body();
83+
}
84+
85+
/**
86+
* 测试手动触发一次任务
87+
*/
88+
@GetMapping("/trigger")
89+
public String xxlJobTrigger() {
90+
Map<String, Object> jobInfo = Maps.newHashMap();
91+
jobInfo.put("id", 4);
92+
jobInfo.put("executorParam", JSONUtil.toJsonStr(jobInfo));
93+
94+
HttpResponse execute = HttpUtil.createGet(baseUri + JOB_INFO_URI + "/trigger").form(jobInfo).execute();
95+
log.info("【execute】= {}", execute);
96+
return execute.body();
97+
}
98+
99+
/**
100+
* 测试手动删除任务
101+
*/
102+
@GetMapping("/remove")
103+
public String xxlJobRemove() {
104+
Map<String, Object> jobInfo = Maps.newHashMap();
105+
jobInfo.put("id", 4);
106+
107+
HttpResponse execute = HttpUtil.createGet(baseUri + JOB_INFO_URI + "/remove").form(jobInfo).execute();
108+
log.info("【execute】= {}", execute);
109+
return execute.body();
110+
}
111+
112+
/**
113+
* 测试手动停止任务
114+
*/
115+
@GetMapping("/stop")
116+
public String xxlJobStop() {
117+
Map<String, Object> jobInfo = Maps.newHashMap();
118+
jobInfo.put("id", 4);
119+
120+
HttpResponse execute = HttpUtil.createGet(baseUri + JOB_INFO_URI + "/stop").form(jobInfo).execute();
121+
log.info("【execute】= {}", execute);
122+
return execute.body();
123+
}
124+
125+
/**
126+
* 测试手动停止任务
127+
*/
128+
@GetMapping("/start")
129+
public String xxlJobStart() {
130+
Map<String, Object> jobInfo = Maps.newHashMap();
131+
jobInfo.put("id", 4);
132+
133+
HttpResponse execute = HttpUtil.createGet(baseUri + JOB_INFO_URI + "/start").form(jobInfo).execute();
134+
log.info("【execute】= {}", execute);
135+
return execute.body();
136+
}
137+
138+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.xkcoding.task.xxl.job.task;
2+
3+
import cn.hutool.core.date.DateUtil;
4+
import cn.hutool.core.util.RandomUtil;
5+
import com.xxl.job.core.biz.model.ReturnT;
6+
import com.xxl.job.core.handler.IJobHandler;
7+
import com.xxl.job.core.handler.annotation.JobHandler;
8+
import com.xxl.job.core.log.XxlJobLogger;
9+
import lombok.extern.slf4j.Slf4j;
10+
import org.springframework.stereotype.Component;
11+
12+
/**
13+
* <p>
14+
* 测试定时任务
15+
* </p>
16+
*
17+
* @author yangkai.shen
18+
* @date Created in 2019-08-07 10:15
19+
*/
20+
@Slf4j
21+
@Component
22+
@JobHandler("demoTask")
23+
public class DemoTask extends IJobHandler {
24+
25+
/**
26+
* execute handler, invoked when executor receives a scheduling request
27+
*
28+
* @param param 定时任务参数
29+
* @return 执行状态
30+
* @throws Exception 任务异常
31+
*/
32+
@Override
33+
public ReturnT<String> execute(String param) throws Exception {
34+
// 可以动态获取传递过来的参数,根据参数不同,当前调度的任务不同
35+
log.info("【param】= {}", param);
36+
XxlJobLogger.log("demo task run at : {}", DateUtil.now());
37+
return RandomUtil.randomInt(1, 11) % 2 == 0 ? SUCCESS : FAIL;
38+
}
39+
}
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
server:
22
port: 8080
33
servlet:
4-
context-path: /demo
4+
context-path: /demo
5+
xxl:
6+
job:
7+
# 执行器通讯TOKEN [选填]:非空时启用;
8+
access-token:
9+
admin:
10+
# 调度中心部署跟地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
11+
address: http://localhost:18080/xxl-job-admin
12+
executor:
13+
# 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
14+
app-name: spring-boot-demo-task-xxl-job-executor
15+
# 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
16+
ip:
17+
# 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
18+
port: 9999
19+
# 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
20+
log-path: logs/spring-boot-demo-task-xxl-job/task-log
21+
# 执行器日志保存天数 [选填] :值大于3时生效,启用执行器Log文件定期清理功能,否则不生效;
22+
log-retention-days: -1

0 commit comments

Comments
 (0)