之前做过一次在业务代码中调用xxljob的接口添加任务启动任务,xxljob的接口添加免登录验证注解后直接调用,博文地址:xxl-job 在业务代码中添加任务,后面用到的groupId获取方法也在这里面。
这次再这样操作的时候,加了免登录验证注解还是会验证token,第一次拦截器里面确实过了,但不知道为什么还会走第二遍拦截器,这次又会验证token。又试了一下在配置拦截器拦截路径的时候把自己复制出来的接口配置不拦截,但是也没有成功。
所以只能登录之后再携带token调接口了
?http请求工具还是使用的forest,官网地址:forest官网
代码如下
package com.gt.gxjhpt.controller;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dtflys.forest.http.ForestCookie;
import com.gt.gxjhpt.entity.bo.XxlJobInfo;
import com.gt.gxjhpt.enumeration.RedisKeyEnum;
import com.gt.gxjhpt.http.XxlJobHttp;
import com.gt.gxjhpt.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author vhukze
* @date 2022/3/28 16:24
*/
@RestController
@RequestMapping("demo")
public class DemoController {
@Resource
private XxlJobHttp xxlJobHttp;
@Resource
private RedisUtil redisUtil;
@Value("${xxl.job.groupId}")
private Integer groupId;
@Value("${xxl.job.userName}")
private String userName;
@Value("${xxl.job.password}")
private String password;
@GetMapping(value = "addJob", produces = MediaType.APPLICATION_JSON_VALUE)
public String addJob() {
XxlJobInfo jobInfo = new XxlJobInfo();
jobInfo.setJobCron("0 0 0 1 * ? *");
jobInfo.setGlueType("BEAN");
jobInfo.setJobGroup(groupId);
jobInfo.setExecutorHandler("testJob");
jobInfo.setAuthor("vhukze");
jobInfo.setJobDesc("描述");
// 路由策略 第一个
jobInfo.setExecutorRouteStrategy("FIRST");
// 阻塞处理策略 单机串行
jobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
// 执行参数
JSONObject jsonParam = new JSONObject();
jsonParam.set("redisKey", "vhukze");
jobInfo.setExecutorParam(jsonParam.toString());
// 任务超时时间
jobInfo.setExecutorTimeout(10);
// 失败重试次数
jobInfo.setExecutorFailRetryCount(5);
String result = xxlJobHttp.addJob(jobInfo, (request, cookies) -> {
// 将之前调用登入接口获得的Cookie传入请求发送到服务端
cookies.addCookie(this.getXxlToken());
});
return JSONUtil.parseObj(result).get("content").toString();
// xxlJobHttp.startJob(xxlJobAddresses, JSONUtil.parseObj(result).get("content").toString());
}
@GetMapping(value = "startJob", produces = MediaType.APPLICATION_JSON_VALUE)
public String startJob(String id) {
Map<String, Object> map = new HashMap<>(2, 1);
map.put("id", id);
// 执行任务参数
String runId = IdUtil.objectId();
JSONObject jsonObject = new JSONObject();
jsonObject.set("redisKey", runId);
map.put("executorParam", jsonObject.toString());
String result = xxlJobHttp.triggerJob(map, (request, cookies) -> {
// 将之前调用登入接口获得的Cookie传入请求发送到服务端
cookies.addCookie(this.getXxlToken());
});
if (!"200".equals(JSONUtil.parseObj(result).get("code").toString())) {
return null;
}
return runId;
}
private ForestCookie getXxlToken() {
Object token = redisUtil.get(RedisKeyEnum.XXL_TOKEN.getKey());
if (token != null) {
return (ForestCookie) token;
}
AtomicReference<ForestCookie> cookieAtomic = new AtomicReference<>(null);
xxlJobHttp.login(new HashMap<String, Object>() {{
this.put("userName", userName);
this.put("password", password);
}}, (request, cookies) -> {
// 将服务端传来的Cookie放入cookieAtomic
cookieAtomic.set(cookies.allCookies().get(0));
});
redisUtil.set(RedisKeyEnum.XXL_TOKEN.getKey(), cookieAtomic.get());
return cookieAtomic.get();
}
}
http请求接口,#{}参数是在配置文件配置的,forest支持读取配置文件 ?
package com.gt.gxjhpt.http;
import com.dtflys.forest.annotation.*;
import com.dtflys.forest.callback.OnLoadCookie;
import com.dtflys.forest.callback.OnSaveCookie;
import com.dtflys.forest.http.ForestCookie;
import com.gt.gxjhpt.entity.bo.XxlJobInfo;
import com.gt.gxjhpt.enumeration.RedisKeyEnum;
import com.gt.gxjhpt.utils.RedisUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author vhukze
* @date 2022/3/28 16:25
*/
public interface XxlJobHttp {
@Post("#{xxl.job.admin.addresses}/jobinfo/add")
String addJob(@Body XxlJobInfo jobInfo, OnLoadCookie onLoadCookie);
@Post("#{xxl.job.admin.addresses}/jobinfo/start")
String startJob(@Query("id") int id, OnLoadCookie onLoadCookie);
@Post("#{xxl.job.admin.addresses}/jobinfo/trigger")
String triggerJob(@Body Map<String, Object> map, OnLoadCookie onLoadCookie);
@Post("#{xxl.job.admin.addresses}/login")
void login(@Body Map<String, Object> map, OnSaveCookie onSaveCookie);
}
|