谁若与集体脱离,谁的命运就要悲哀。——奥斯特洛夫斯基

昨天对接了xxl-jobrestful api,发现其没有提供查询job信息的api,于是自己拓展

在原先com.xxl.job.admin.controller.JobApiController#api添加

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
/**
* api
*
* @param uri
* @param data
* @return
*/
@RequestMapping("/{uri}")
@ResponseBody
@PermissionLimit(limit=false)
public ReturnT<?> api(HttpServletRequest request, @PathVariable("uri") String uri, @RequestBody(required = false) String data) {

// valid
if (!"POST".equalsIgnoreCase(request.getMethod())) {
return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, HttpMethod not support.");
}
if (uri==null || uri.trim().length()==0) {
return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, uri-mapping empty.");
}
if (XxlJobAdminConfig.getAdminConfig().getAccessToken()!=null
&& XxlJobAdminConfig.getAdminConfig().getAccessToken().trim().length()>0
&& !XxlJobAdminConfig.getAdminConfig().getAccessToken().equals(request.getHeader(XxlJobRemotingUtil.XXL_JOB_ACCESS_TOKEN))) {
return new ReturnT<String>(ReturnT.FAIL_CODE, "The access token is wrong.");
}

// services mapping
if ("callback".equals(uri)) {
List<HandleCallbackParam> callbackParamList = GsonTool.fromJson(data, List.class, HandleCallbackParam.class);
return adminBiz.callback(callbackParamList);
} else if ("registry".equals(uri)) {
RegistryParam registryParam = GsonTool.fromJson(data, RegistryParam.class);
return adminBiz.registry(registryParam);
} else if ("registryRemove".equals(uri)) {
RegistryParam registryParam = GsonTool.fromJson(data, RegistryParam.class);
return adminBiz.registryRemove(registryParam);
} else if ("pageList".equals(uri)) {
ReturnT<Map<String, Object>> success = new ReturnT<>();
JobQuery jobQuery = GsonTool.fromJson(data, JobQuery.class);
Map<String, Object> pageList = xxlJobService.pageList(
jobQuery.getStart(),
jobQuery.getLength(),
jobQuery.getJobGroup(),
jobQuery.getTriggerStatus(),
jobQuery.getJobDesc(),
jobQuery.getExecutorHandler(),
jobQuery.getAuthor());
success.setContent(pageList);
return success;
} else {
return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, uri-mapping("+ uri +") not found.");
}

}

这里参数

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
55
56
57
58
59
60
61
62
63
64
65
public class JobQuery {
private Integer start;
private Integer length;
private Integer jobGroup;
private Integer triggerStatus;
private String jobDesc;
private String executorHandler;
private String author;

public Integer getStart() {
return start;
}

public void setStart(Integer start) {
this.start = start;
}

public Integer getLength() {
return length;
}

public void setLength(Integer length) {
this.length = length;
}

public Integer getJobGroup() {
return jobGroup;
}

public void setJobGroup(Integer jobGroup) {
this.jobGroup = jobGroup;
}

public Integer getTriggerStatus() {
return triggerStatus;
}

public void setTriggerStatus(Integer triggerStatus) {
this.triggerStatus = triggerStatus;
}

public String getJobDesc() {
return jobDesc;
}

public void setJobDesc(String jobDesc) {
this.jobDesc = jobDesc;
}

public String getExecutorHandler() {
return executorHandler;
}

public void setExecutorHandler(String executorHandler) {
this.executorHandler = executorHandler;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}
}

然后是修改对应的serviceimpl的签名

com.xxl.job.admin.service.XxlJobService#pageList

1
public Map<String, Object> pageList(Integer start, Integer length, Integer jobGroup, Integer triggerStatus, String jobDesc, String executorHandler, String author);

com.xxl.job.admin.service.impl.XxlJobServiceImpl#pageList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Override
public Map<String, Object> pageList(Integer start, Integer length, Integer jobGroup, Integer triggerStatus, String jobDesc, String executorHandler, String author) {

start = Optional.ofNullable(start).orElse(0);
length = Optional.ofNullable(length).orElse(10);
// page list
List<XxlJobInfo> list = xxlJobInfoDao.pageList(start, length, jobGroup, triggerStatus, jobDesc, executorHandler, author);
int list_count = xxlJobInfoDao.pageListCount(start, length, jobGroup, triggerStatus, jobDesc, executorHandler, author);

// package result
Map<String, Object> maps = new HashMap<String, Object>();
maps.put("recordsTotal", list_count); // 总记录数
maps.put("recordsFiltered", list_count); // 过滤后的总记录数
maps.put("data", list); // 分页列表
return maps;
}

然后是mapper

com.xxl.job.admin.dao.XxlJobInfoDao

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public List<XxlJobInfo> pageList(@Param("offset") Integer offset,
@Param("pagesize") Integer pagesize,
@Param("jobGroup") Integer jobGroup,
@Param("triggerStatus") Integer triggerStatus,
@Param("jobDesc") String jobDesc,
@Param("executorHandler") String executorHandler,
@Param("author") String author);

public int pageListCount(@Param("offset") Integer offset,
@Param("pagesize") Integer pagesize,
@Param("jobGroup") Integer jobGroup,
@Param("triggerStatus") Integer triggerStatus,
@Param("jobDesc") String jobDesc,
@Param("executorHandler") String executorHandler,
@Param("author") String author);

以及XxlJobInfoMapper.xml

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
<select id="pageList" parameterType="java.util.HashMap" resultMap="XxlJobInfo">
SELECT <include refid="Base_Column_List" />
FROM xxl_job_info AS t
<trim prefix="WHERE" prefixOverrides="AND | OR" >
<if test="jobGroup != null and jobGroup gt 0">
AND t.job_group = #{jobGroup}
</if>
<if test="triggerStatus != null and triggerStatus gte 0">
AND t.trigger_status = #{triggerStatus}
</if>
<if test="jobDesc != null and jobDesc != ''">
AND t.job_desc like CONCAT(CONCAT('%', #{jobDesc}), '%')
</if>
<if test="executorHandler != null and executorHandler != ''">
AND t.executor_handler like CONCAT(CONCAT('%', #{executorHandler}), '%')
</if>
<if test="author != null and author != ''">
AND t.author like CONCAT(CONCAT('%', #{author}), '%')
</if>
</trim>
ORDER BY id DESC
LIMIT #{offset}, #{pagesize}
</select>

<select id="pageListCount" parameterType="java.util.HashMap" resultType="int">
SELECT count(1)
FROM xxl_job_info AS t
<trim prefix="WHERE" prefixOverrides="AND | OR" >
<if test="jobGroup != null and jobGroup gt 0">
AND t.job_group = #{jobGroup}
</if>
<if test="triggerStatus != null and triggerStatus gte 0">
AND t.trigger_status = #{triggerStatus}
</if>
<if test="jobDesc != null and jobDesc != ''">
AND t.job_desc like CONCAT(CONCAT('%', #{jobDesc}), '%')
</if>
<if test="executorHandler != null and executorHandler != ''">
AND t.executor_handler like CONCAT(CONCAT('%', #{executorHandler}), '%')
</if>
<if test="author != null and author != ''">
AND t.author like CONCAT(CONCAT('%', #{author}), '%')
</if>
</trim>
</select>

至此,终于可以通过请求

1
http://localhost:8080/xxl-job-admin/api/pageList

查询到job列表了,别忘了在header里携带token

1
XXL-JOB-ACCESS-TOKEN:{xxl.job.accessToken}