控制层:XxlJobController.java 

package com.cwp.data.job.controller;import com.cwp.data.intelligence.common.exception.RRException;
import com.cwp.data.intelligence.common.utils.R;
import com.cwp.data.job.api.XxlJobApi;
import com.cwp.data.job.dto.SaveXxlJobDto;
import com.cwp.data.job.dto.UpdateXxlJobDto;
import com.cwp.data.job.util.XxlJobUtil;
import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;import java.io.IOException;/*** @ClassName XxlJobController* @Date 2021/4/6 11:40*/
@Slf4j
@RestController
public class XxlJobController implements XxlJobApi {@Value("${xxl.job.admin.addresses}")private String adminAddresses;@Value("${xxl.job.executor.appname}")private String executorAppname;@Value("${xxl.job.admin.username:admin}")private String username;@Value("${xxl.job.admin.password:admin}")private String password;@Value("${xxl.job.admin.jobGroup:1")private Integer jobGroup;@ApiOperation(value = "添加jobInfo并启动", httpMethod = "POST")@Overridepublic R addXxlJob(@RequestBody SaveXxlJobDto saveXxlJobDto) {/* Map<String, Object> paramMap = new HashMap<>();paramMap.put("jobGroup", 2);paramMap.put("jobDesc", "这是测试任务");paramMap.put("executorRouteStrategy", "FIRST");paramMap.put("cronGen_display", "0/6 * * * * ?");paramMap.put("jobCron", "0/6 * * * * ?");paramMap.put("glueType", "BEAN");paramMap.put("executorHandler", "messageJob"); // 此处hander需提前在项目中定义paramMap.put("executorBlockStrategy", "SERIAL_EXECUTION");paramMap.put("executorTimeout", 0);paramMap.put("executorFailRetryCount", 1);paramMap.put("author", "admin");paramMap.put("glueRemark", "GLUE代码初始化");paramMap.put("triggerStatus", 1);*///设置执行器组saveXxlJobDto.setJobGroup(jobGroup);String jsonStr=JSONObject.toJSONString(saveXxlJobDto);JSONObject jsonObjectParam= JSONObject.parseObject(jsonStr);JSONObject response = null;try {log.info("新增任务入参={}",jsonStr);String cookie= getLoginCookie();log.info("登录cookie={}",cookie);response = XxlJobUtil.addJob(adminAddresses,jsonObjectParam);} catch (IOException e) {throw new RRException("调用xxl-job-admin-add接口失败!"+e.getMessage());}if (response.containsKey("code") && HttpStatus.HTTP_OK == (Integer) response.get("code")) {String  jobIdStr=(String)response.get("content");Integer jobId=Integer.valueOf(jobIdStr);return R.okWithData(jobId);} else {return  R.error(response.get("msg")!=null?response.get("msg").toString():"");}}@ApiOperation(value = "更新jobInfo", httpMethod = "POST")@Overridepublic R  updateXxlJob(@RequestBody UpdateXxlJobDto updateXxlJobDto) {//设置执行器组updateXxlJobDto.setJobGroup(jobGroup);String jsonStr=JSONObject.toJSONString(updateXxlJobDto);JSONObject jsonObjectParam= JSONObject.parseObject(jsonStr);JSONObject response = null;try {log.info("新增任务入参={}",jsonStr);String cookie= getLoginCookie();log.info("登录cookie={}",cookie);response = XxlJobUtil.updateJob(adminAddresses,jsonObjectParam);} catch (IOException e) {throw new RRException("调用xxl-job-admin-update接口失败!"+e.getMessage());}if (response.containsKey("code") && HttpStatus.HTTP_OK == (Integer) response.get("code")) {return R.ok();} else {return  R.error(response.get("msg")!=null?response.get("msg").toString():"");}}/*** 删除任务** @param id* @return* @throws IOException*/@Overridepublic R delete(@RequestParam("id") Integer id) {try {String cookie= getLoginCookie();log.info("登录cookie={}",cookie);JSONObject response = XxlJobUtil.deleteJob(adminAddresses, id);if (response.containsKey("code") &&HttpStatus.HTTP_OK == (Integer) response.get("code")) {return R.ok();} else {return  R.error(response.get("msg")!=null?response.get("msg").toString():"");}} catch (Exception e) {return R.error(e.getMessage());}}/*** 开始任务** @param id* @return* @throws IOException*/@Overridepublic R start(@RequestParam("id") Integer id) {try {String cookie= getLoginCookie();log.info("登录cookie={}",cookie);JSONObject response = XxlJobUtil.startJob(adminAddresses, id);if (response.containsKey("code") &&HttpStatus.HTTP_OK == (Integer) response.get("code")) {return R.ok();} else {return  R.error(response.get("msg")!=null?response.get("msg").toString():"");}} catch (Exception e) {return R.error(e.getMessage());}}/*** 挂起任务** @param id* @return* @throws IOException*/@Overridepublic R stop(@RequestParam("id") Integer id) {JSONObject response =null;try {String cookie= getLoginCookie();log.info("登录cookie={}",cookie);response=XxlJobUtil.stopJob(adminAddresses, id);} catch (IOException e) {throw new RRException("调用xxl-job-admin-stop接口失败!"+e.getMessage());}if (response.containsKey("code") && HttpStatus.HTTP_OK == (Integer) response.get("code")) {return R.ok();} else {return  R.error(response.get("msg")!=null?response.get("msg").toString():"");}}/*** 登陆** @param userName* @param password* @return* @throws IOException*/@RequestMapping(value = "/login", method = RequestMethod.GET)public R login(@RequestParam("userName") String userName,@RequestParam("password") String password) {try {String cookie = XxlJobUtil.login(adminAddresses, userName, password);if (StringUtils.isNotBlank(cookie)) {return R.ok();} else {throw new Exception("调用xxl-job-admin-login接口失败!");}} catch (Exception e) {return R.error(e.getMessage());}}/*** 根据xxl-appname获取对应id** @return* @throws IOException*/@Overridepublic R getAppNameIdByAppname() {JSONObject response =null;try {String cookie= getLoginCookie();log.info("登录cookie={}",cookie);response = XxlJobUtil.getAppNameIdByAppname(adminAddresses,executorAppname);} catch (IOException e) {throw new RRException("调用xxl-job-admin-getAppNameIdByAppname接口失败!"+e.getMessage());}if (response.containsKey("code") && HttpStatus.HTTP_OK == (Integer) response.get("code")) {return R.ok();} else {return  R.error(response.get("msg")!=null?response.get("msg").toString():"");}}/*** @Description  获取登录cookie* @Author  chengweiping* @Date   2021/4/13 16:48*/public  String getLoginCookie() {String cookie= XxlJobUtil.getCookie();try {if (StringUtils.isNotBlank(cookie)) {return cookie;}cookie=XxlJobUtil.login(adminAddresses, username, password);XxlJobUtil.setCookie(cookie);} catch (IOException e) {e.printStackTrace();throw new RRException(e.getMessage());}return cookie;}}

控制层api接口:XxlJobApi.java

package com.cwp.data.job.api;import com.cwp.data.intelligence.common.utils.R;
import com.cwp.data.job.dto.SaveXxlJobDto;
import com.cwp.data.job.dto.UpdateXxlJobDto;
import org.springframework.web.bind.annotation.*;/*** @ClassName XxlJobApi* @Description TODO* @Date 2020/12/23 10:46*/public interface XxlJobApi {/*** @Description 新增定时任务* @Author  chengweiping* @Date   2021/4/14 9:46*/@PostMapping("/xxljob/save")R addXxlJob(@RequestBody SaveXxlJobDto saveXxlJobDto);/***  更新定时任务*/@PostMapping("/xxljob/update")R  updateXxlJob(@RequestBody UpdateXxlJobDto updateXxlJobDto);/*** @Description  删除定时任务* @Author  chengweiping* @Date   2021/4/14 9:46*/@RequestMapping(value = "/xxljob/delete", method = RequestMethod.GET)R delete(@RequestParam("id") Integer id);/***  启动定时任务*/@RequestMapping(value = "/xxljob/start", method = RequestMethod.GET)R start(@RequestParam("id") Integer id);/*** 停止定时任务*/@RequestMapping(value = "/xxljob/stop", method = RequestMethod.GET)R stop(@RequestParam("id") Integer id);/***  根据启动器名字获取启动器ID*/@RequestMapping(value = "/xxljob/getAppNameIdByAppname", method = RequestMethod.GET)R getAppNameIdByAppname();
}

新增任务传输DTO: SaveXxlJobDto.java 

package com.cwp.data.job.dto;import lombok.Data;import java.io.Serializable;/*** @ClassName SaveXxlJobDto* @Date 2021/4/13 17:40*/
@Data
public class SaveXxlJobDto implements Serializable {/*** 执行器ID*/private  Integer jobGroup;/***  任务描述*/private  String jobDesc;/***  执行策略*/private  String executorRouteStrategy;/***  表达式显示*/private   String cronGen_display;/***  任务表达式*/private   String jobCron;/***  运行模式*/private  String glueType;/***  此处hander需提前在项目中定义*/private  String executorHandler;/***  失败重试次数*/private Integer executorFailRetryCount;/***  负责人*/private  String author;private  String glueRemark;/***  触发状态*/private  Integer triggerStatus;/***  执行参数*/private  String executorParam;/***  SERIAL_EXECUTION 阻塞处理策略*/private  String executorBlockStrategy;}

更新任务DTO:  UpdateXxlJobDto.java

package com.cwp.data.job.dto;import lombok.Data;import java.io.Serializable;/*** @ClassName SaveXxlJobDto* @Description TODO* @Date 2021/4/13 17:40*/
@Data
public class UpdateXxlJobDto extends SaveXxlJobDto implements Serializable {/*** @Description 任务ID* @Author  chengweiping* @Date   2021/4/13 18:50*/private  Integer id;}

xxl-job工具类:XxlJobUtil.java

package com.cwp.data.job.util;import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;@Slf4j
public class XxlJobUtil {private static String cookie="";/*** 新增/编辑任务* @param url* @param requestInfo* @return* @throws HttpException* @throws IOException*/public static JSONObject addJob(String url, JSONObject requestInfo) throws HttpException, IOException {String path = "/jobinfo/add";String targetPath=url+path;cookie=getCookie();HttpResponse response = HttpRequest.post(targetPath).form(requestInfo).cookie(cookie).execute();JSONObject jsonObject = JSON.parseObject(response.body());return jsonObject;}public static JSONObject updateJob(String url, JSONObject requestInfo) throws HttpException, IOException {String path = "/jobinfo/update";String targetPath=url+path;cookie=getCookie();HttpResponse response = HttpRequest.post(targetPath).form(requestInfo).cookie(cookie).execute();JSONObject jsonObject = JSON.parseObject(response.body());return jsonObject;}/*** 删除任务* @param url* @param id* @return* @throws HttpException* @throws IOException*/public static JSONObject deleteJob(String url,int id) throws HttpException, IOException {String path = "/jobinfo/remove?id=" + id;String targetPath=url+path;cookie=getCookie();HttpResponse response = HttpRequest.post(targetPath).cookie(cookie).execute();JSONObject jsonObject = JSON.parseObject(response.body());return jsonObject;}/*** 开始任务* @param url* @param id* @return* @throws HttpException* @throws IOException*/public static JSONObject startJob(String url,int id) throws HttpException, IOException {String path = "/jobinfo/start?id="+id;String targetPath=url+path;cookie=getCookie();HttpResponse response = HttpRequest.post(targetPath).cookie(cookie).execute();JSONObject jsonObject = JSON.parseObject(response.body());return jsonObject;}/*** 停止任务* @param url* @param id* @return* @throws HttpException* @throws IOException*/public static JSONObject stopJob(String url,int id) throws HttpException, IOException {String path = "/jobinfo/stop?id="+id;String targetPath=url+path;cookie=getCookie();HttpResponse response = HttpRequest.post(targetPath).cookie(cookie).execute();JSONObject jsonObject = JSON.parseObject(response.body());return jsonObject;}/*** 根据xxl-appname获取对应id* @param url* @param appnameParam* @return* @throws HttpException* @throws IOException*/public static JSONObject getAppNameIdByAppname(String url,String appnameParam) throws HttpException, IOException {String path = "/jobgroup/getAppNameIdByAppname?appnameParam="+appnameParam;String targetPath=url+path;cookie=getCookie();HttpResponse response = HttpRequest.post(targetPath).cookie(cookie).execute();JSONObject jsonObject = JSON.parseObject(response.body());return jsonObject;}public static JSONObject doGet(String url,String path) throws HttpException, IOException {String targetUrl = url + path;HttpClient httpClient = new HttpClient();HttpMethod get = new GetMethod(targetUrl);get.setRequestHeader("cookie", cookie);httpClient.executeMethod(get);JSONObject result = new JSONObject();result = getJsonObject(get, result);return result;}private static JSONObject getJsonObject(HttpMethod get, JSONObject result) throws IOException {InputStream inputStream = get.getResponseBodyAsStream();BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));StringBuffer stringBuffer = new StringBuffer();String str = "";while ((str = br.readLine()) != null) {stringBuffer.append(str);}if (get.getStatusCode() == 200) {/***  使用此方式会出现*  Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.*  异常*  String responseBodyAsString = get.getResponseBodyAsString();*  result = JSONObject.parseObject(responseBodyAsString);*/result = JSONObject.parseObject(stringBuffer.toString());} else {try {
//                result = JSONObject.parseObject(get.getResponseBodyAsString());result = JSONObject.parseObject(stringBuffer.toString());} catch (Exception e) {result.put("error", stringBuffer.toString());}}return result;}public static String login(String url, String userName, String password) throws HttpException, IOException {String path = "/login?userName="+userName+"&password="+password;String targetUrl = url + path;HttpClient httpClient = new HttpClient();PostMethod get = new PostMethod(targetUrl);httpClient.executeMethod(get);if (get.getStatusCode() == 200) {Cookie[] cookies = httpClient.getState().getCookies();StringBuffer tmpcookies = new StringBuffer();for (Cookie c : cookies) {tmpcookies.append(c.toString() + ";");}cookie = tmpcookies.toString();} else {try {cookie = "";} catch (Exception e) {cookie="";}}return cookie;}/*** @Description 获取登录cookie* @Author  chengweiping* @Date   2021/4/13 16:39*/public static String getCookie() {return cookie;}public static void setCookie(String cookie) {XxlJobUtil.cookie = cookie;}
}

远程Feign接口:XxlJobFeignApi.java

package com.cwp.data.manager.feign;import com.cwp.data.development.api.SendEmailApi;
import com.cwp.data.intelligence.common.config.FeignConfiguration;
import com.cwp.data.intelligence.common.constants.MicroServiceConstant;
import com.cwp.data.job.api.XxlJobApi;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;/*** @ClassName SendEmailFeignApi* @Date 2020/12/24 15:33*/
@Component
@FeignClient(value = MicroServiceConstant.DATA_JOB_SERVICE,path = MicroServiceConstant.DATA_JOB_PATH,configuration = FeignConfiguration.class)
public interface XxlJobFeignApi extends XxlJobApi {
}

常量类:MicroServiceConstant.java

package com.cwp.data.intelligence.common.constants;public class MicroServiceConstant {public  final  static String DATA_JOB_SERVICE="data-job-web";public  final  static String DATA_JOB_PATH="data-job";}

Feign配置类转发请求头:FeignConfiguration.java 

package com.cwp.data.intelligence.common.config;import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;/*** @ClassName FeignConfiguration* @Description 设置Feign转发请求头* @Date 2020/12/24 14:16*/
@Configuration
@ConditionalOnBean(value =RequestInterceptor.class)
public class FeignConfiguration implements RequestInterceptor {@Overridepublic void apply(RequestTemplate requestTemplate) {ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (null != attributes) {HttpServletRequest request = attributes.getRequest();if (null != request) {Enumeration<String> headerNames = request.getHeaderNames();if (headerNames != null) {while (headerNames.hasMoreElements()) {String name = headerNames.nextElement();String values = request.getHeader(name);requestTemplate.header(name, values);}}}}}
}

返回类:R.java

package com.cwp.data.intelligence.common.utils;import java.util.HashMap;
import java.util.Map;public class R extends HashMap<String, Object> {private static final long serialVersionUID = 1L;public R() {put("code", 0);put("msg", "success");}public static R error() {return error(500, "未知异常,请联系管理员");}public static R error(String msg) {return error(500, msg);}public static R error(int code, String msg) {R r = new R();r.put("code", code);r.put("msg", msg);return r;}public static R ok(String msg) {R r = new R();r.put("msg", msg);return r;}public static R ok(Map<String, Object> map) {R r = new R();r.putAll(map);return r;}public static R okWithData(Object data) {R r = new R();r.put("data", data);return r;}public static R okWithPage(Object page) {R r = new R();r.put("page", page);return r;}public static R ok() {return new R();}public R put(String key, Object value) {super.put(key, value);return this;}public boolean isOk() {if (get("code") != null) {if (Integer.parseInt(get("code").toString()) == 0) {return true;}}return false;}
}

需要的依赖

            <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.4</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.4.7</version></dependency>

整合xxl-job-admin动态添加xxl-job定时任务相关推荐

  1. 如何动态添加修改删除定时任务

    如何动态添加修改删除定时任务 (1)思路说明: (a)首先这里我们需要重新认识一个类ThreadPoolTaskScheduler:线程池任务调度类,能够开启线程池进行任务调度. (b)ThreadP ...

  2. SpringBoot定时任务升级篇(动态添加修改删除定时任务)

    (1)思路说明: (a)首先这里我们需要重新认识一个类ThreadPoolTaskScheduler:线程池任务调度类,能够开启线程池进行任务调度. (b)ThreadPoolTaskSchedule ...

  3. SpringBoot整合多数据源,动态添加新数据源并切换(保姆级教程)

    前言 前段时间在项目的开发过程中,遇到了需要从数据库中动态查询新的数据源信息并切换到该数据源做相应的查询操作,翻阅了网上很多资料都是简单的对多数据源的整合,并没有涉及到动态添加新数据源并切换的案例,本 ...

  4. Android动态添加Device Admin权限

    /*********************************************************************** Android动态添加Device Admin权限* ...

  5. springboot整合quartz实现动态添加、修改、删除、停止job,以及优化quartz工具类,支持自动停止逻辑

    原文链接:http://pengfeiguo.com/article/16 什么是Quartz? 一个定时任务调度框架,简单易用,功能强大可以使实现定时任务的. 优点: 支持集群下定时任务处理 支持任 ...

  6. Quartz动态添加,修改,删除任务(暂停,任务状态,恢复,最近触发时间)

    首页 博客 学院 下载 图文课 论坛 APP 问答 商城 VIP会员 活动 招聘 ITeye GitChat 写博客 小程序 消息 登录注册 关闭 quartz_Cron表达式一分钟教程 09-05 ...

  7. 后盾网lavarel视频项目---vue实现动态添加和删除板块

    后盾网lavarel视频项目---vue实现动态添加和删除板块 一.总结 一句话总结: 原理就是:列表时根据vue中的videos变量中的元素来遍历的,初始时videos:[{title:'',pat ...

  8. php动态删除输入框,jQuery实现动态添加和删除input框实例代码

    本文实例为大家分享了jQuery实现动态添加和删除input框的具体代码,供大家参考,具体内容如下 选项 $(function(){ // 添加选项 $("#opbtn").cli ...

  9. activemenu怎么拼 vue_vue-element-admin登录逻辑,以及动态添加路由,显示侧边栏

    这段时间在研究element-admin,感觉这个库有许多值得学习的地方,我学习这个库的方法是,先看它的路由,顺着路由,摸清它的逻辑,有点像顺藤摸瓜. 这个库分的模块非常清晰,适合多人合作开发项目,但 ...

  10. django 集成个推_Django动态添加定时任务之djangocelery的使用

    定时任务和周期任务在我们日常工作中应用广泛,例如定时发布.周期巡检等,通常我们会借助Linux下的Crontab来实现,但如何将这一功能搬进我们自研的运维系统呢?借助django-celery即可轻松 ...

最新文章

  1. Winsock编程原理——面向连接
  2. java线程学习之notify方法和notifyAll方法
  3. 使用福禄克CFP单模光纤测试仪像专家一样设置参数!
  4. 百度地图移动端开发和ArcGIS for Android 开发入门
  5. mysql安装教程_mysql8.0.20安装教程,mysql下载安装教程8.0.20
  6. OSI七层网络与TCP/IP五层网络架构及二层/三层网络
  7. 划分vlan实验心得体会_vlan划分实验报告.doc
  8. 16种设计思想 - Design for failure
  9. vue指令模式 添加埋点
  10. 计算机专业常见面试题目汇总
  11. Winform使用第三方库控件出现“NoLicenseInformation”或“变量未声明或从未赋值”问题总结
  12. JavaScript运动详解:匀速运动、变速运动和曲线运动
  13. 跨境电商ERP中的自动化 3.平台订单自动发货
  14. 静态HTML网页设计作品 我的家乡-云南(9页) HTML+CSS+JavaScript 关于我的家乡的HTML网页设计-----云南
  15. 解决eclipse中打开xml文件时不显示namespace标签的问题
  16. layui 数据表格下拉框_layui学习——数据表格嵌套下拉列表,并实现动态更新
  17. 云媒易:抖音短视频推广小技巧汇总
  18. Android studio3.1 汉化
  19. 基于 React 和 Redux 的现代内容编辑器 ORY Editor
  20. 种草Cypress和TestCafe,QA同学一定想了解的Web UI自动化测试工具

热门文章

  1. ISE UCF 写法
  2. 关于lodop的学习小计
  3. 涉案资金超10亿,又一洗钱团伙被端,“二清”警钟不能忘
  4. everedit 保存机器学习路径注意事项
  5. hu沪江计算机词汇,拼音带hu的字大全150个拼音含hu的字组词 - 小孩子点读
  6. macbook黑屏_Mac Book电脑黑屏开不了机三种解决方法
  7. 如何使用Java以编程方式在 Excel 中创建图表
  8. At least one JAR was scanned for TLDs yet contained no TLDs.
  9. 私有云服务器同步盘的定义及优势详解!
  10. tensorflow各个版本的CUDA以及Cudnn版本对应关系(重点)