package com.bochao.devmgr.job;
import com.bochao.devmgr.job.domain.XxlJobInfo;
import com.xxl.job.core.biz.model.ReturnT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.util.MultiValueMap;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;
/**
* Date:2022/5/20
*
* @author:zrlie
*/
//@Lazy
@Component
public class JobUtil {
@Qualifier("restTemplate")
@Autowired
private RestTemplate restTemplate;
private static String cookie = "";
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
/**
* xxljob登录
*
* @return
* @throws IOException
*/
@PostConstruct
public String loginXXLJob() {
String path = adminAddresses + "/login";
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("userName", "admin");
map.add("password", "123456");
map.add("ifRemember", "on");
//构造实体对象
HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, null);
ResponseEntity<ReturnT> entity = restTemplate.postForEntity(path, param, ReturnT.class);
List<String> cookies = entity.getHeaders().get("Set-Cookie");
StringBuilder tmpcookies = new StringBuilder();
for (String c : cookies) {
tmpcookies.append(c + ";");
}
cookie = tmpcookies.toString();
return cookie;
}
/**
* 新增任务
*
* @param xxlJobInfo
* @return
*/
public ReturnT addXXLJob(XxlJobInfo xxlJobInfo) {
String path = adminAddresses +"/jobinfo/add";
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("jobGroup", xxlJobInfo.getJobGroup());
map.add("jobDesc", xxlJobInfo.getJobDesc());
map.add("author", xxlJobInfo.getAuthor());
map.add("alarmEmail", xxlJobInfo.getAlarmEmail());
map.add("scheduleType", xxlJobInfo.getScheduleType());
map.add("scheduleConf", xxlJobInfo.getScheduleConf());
map.add("cronGen_display", xxlJobInfo.getScheduleConf());
map.add("schedule_conf_CRON", xxlJobInfo.getScheduleConf());
map.add("schedule_conf_FIX_RATE", "");
map.add("schedule_conf_FIX_DELAY", "");
map.add("glueType", xxlJobInfo.getGlueType());
map.add("executorHandler", xxlJobInfo.getExecutorHandler());
map.add("executorParam", xxlJobInfo.getExecutorParam());
map.add("executorRouteStrategy", xxlJobInfo.getExecutorRouteStrategy());
map.add("childJobId", xxlJobInfo.getChildJobId());
map.add("misfireStrategy", xxlJobInfo.getMisfireStrategy());
map.add("executorBlockStrategy", xxlJobInfo.getExecutorBlockStrategy());
map.add("executorTimeout", xxlJobInfo.getExecutorTimeout());
map.add("executorFailRetryCount", xxlJobInfo.getExecutorFailRetryCount());
map.add("glueRemark", xxlJobInfo.getGlueRemark());
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("cookie", cookie);
//构造实体对象
HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
return restTemplate.postForObject(path, param, ReturnT.class);
}
/**
* 更新任务
*
* @param xxlJobInfo
* @return
*/
public ReturnT updateXXLJob(XxlJobInfo xxlJobInfo) {
String path = adminAddresses +"/jobinfo/update";
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("id", xxlJobInfo.getId());
map.add("jobGroup", xxlJobInfo.getJobGroup());
map.add("jobDesc", xxlJobInfo.getJobDesc());
map.add("author", xxlJobInfo.getAuthor());
map.add("alarmEmail", xxlJobInfo.getAlarmEmail());
map.add("scheduleType", xxlJobInfo.getScheduleType());
map.add("scheduleConf", xxlJobInfo.getScheduleConf());
map.add("cronGen_display", xxlJobInfo.getScheduleConf());
map.add("schedule_conf_CRON", xxlJobInfo.getScheduleConf());
map.add("schedule_conf_FIX_RATE", "");
map.add("schedule_conf_FIX_DELAY", "");
map.add("glueType", xxlJobInfo.getGlueType());
map.add("executorHandler", xxlJobInfo.getExecutorHandler());
map.add("executorParam", xxlJobInfo.getExecutorParam());
map.add("executorRouteStrategy", xxlJobInfo.getExecutorRouteStrategy());
map.add("childJobId", xxlJobInfo.getChildJobId());
map.add("misfireStrategy", xxlJobInfo.getMisfireStrategy());
map.add("executorBlockStrategy", xxlJobInfo.getExecutorBlockStrategy());
map.add("executorTimeout", xxlJobInfo.getExecutorTimeout());
map.add("executorFailRetryCount", xxlJobInfo.getExecutorFailRetryCount());
map.add("glueRemark", xxlJobInfo.getGlueRemark());
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("cookie", cookie);
//构造实体对象
HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
return restTemplate.postForObject(path, param, ReturnT.class);
}
/**
* 开启任务
*
* @param id
* @return
*/
public ReturnT startXXLJob(int id) {
String path = adminAddresses +"/jobinfo/start";
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("id", id);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("cookie", cookie);
//构造实体对象
HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
return restTemplate.postForObject(path, param, ReturnT.class);
}
/**
* 停止任务
*
* @param id
* @return
*/
public ReturnT stopXXLJob(int id) {
String path = adminAddresses +"/jobinfo/stop";
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("id", id);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("cookie", cookie);
//构造实体对象
HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
return restTemplate.postForObject(path, param, ReturnT.class);
}
/**
* 删除任务
*
* @return
*/
public ReturnT removeXXLJob(int id) {
String path = adminAddresses +"/jobinfo/remove";
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("id", id);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("cookie", cookie);
//构造实体对象
HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
return restTemplate.postForObject(path, param, ReturnT.class);
}
}
外部应用调用xxljob接口
于 2022-05-23 14:10:45 首次发布