瀏覽代碼

旧数据迁移功能开发

zhangqiankun 3 年之前
父節點
當前提交
4170bf7574

File diff suppressed because it is too large
+ 10 - 2
doc/sql/tables_xxl_job.sql


+ 18 - 14
job-admin/src/main/java/com/xxl/job/admin/controller/JobInfoController.java

@@ -1,6 +1,22 @@
 package com.xxl.job.admin.controller;
 
-import com.xxl.job.admin.core.cron.CronExpression;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
+
 import com.xxl.job.admin.core.exception.XxlJobException;
 import com.xxl.job.admin.core.model.XxlJobGroup;
 import com.xxl.job.admin.core.model.XxlJobInfo;
@@ -19,18 +35,6 @@ import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.enums.ExecutorBlockStrategyEnum;
 import com.xxl.job.core.glue.GlueTypeEnum;
 import com.xxl.job.core.util.DateUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.stereotype.Controller;
-import org.springframework.ui.Model;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.ResponseBody;
-
-import javax.annotation.Resource;
-import javax.servlet.http.HttpServletRequest;
-import java.text.ParseException;
-import java.util.*;
 
 /**
  * index controller
@@ -150,8 +154,8 @@ public class JobInfoController {
 		return ReturnT.SUCCESS;
 	}
 
-	@RequestMapping("/nextTriggerTime")
 	@ResponseBody
+	@RequestMapping("/nextTriggerTime")
 	public ReturnT<List<String>> nextTriggerTime(String scheduleType, String scheduleConf) {
 
 		XxlJobInfo paramXxlJobInfo = new XxlJobInfo();

+ 91 - 0
job-admin/src/main/java/com/xxl/job/admin/controller/custom/CustomInfoController.java

@@ -0,0 +1,91 @@
+package com.xxl.job.admin.controller.custom;
+
+import javax.annotation.Resource;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+import com.persagy.common.constant.CommonConstant;
+import com.persagy.common.utils.StringUtil;
+import com.xxl.job.admin.core.model.XxlJobInfo;
+import com.xxl.job.admin.core.thread.JobTriggerPoolHelper;
+import com.xxl.job.admin.core.trigger.TriggerTypeEnum;
+import com.xxl.job.admin.service.XxlJobService;
+import com.xxl.job.core.biz.model.ReturnT;
+
+import io.swagger.annotations.Api;
+import io.swagger.v3.oas.annotations.parameters.RequestBody;
+
+/**
+ * 暴露接口
+ * @version 1.0.0
+ * @company persagy 
+ * @author zhangqiankun
+ * @date 2021年9月24日 下午2:37:55
+ */
+@RestController
+@Api(tags = "任务触发点设置")
+@RequestMapping(value = "/customInfo", method = {RequestMethod.POST})
+public class CustomInfoController {
+
+	@Resource
+	private XxlJobService xxlJobService;
+	
+	@Autowired
+	private CustomInfoHandler customInfoHandler;
+	
+	@RequestMapping(value = "/transfer")
+	public ReturnT<String> transfer() {
+		return customInfoHandler.transfer();
+	}
+	
+	@RequestMapping(value = "/addOrUpdateAndStart")
+	public ReturnT<String> addOrUpdateAndStart(@RequestBody XxlJobInfo jobInfo) {
+		if (jobInfo.getId() == null) {
+			xxlJobService.add(jobInfo);
+		} else {
+			xxlJobService.update(jobInfo);
+		}
+		// 启动任务
+		return xxlJobService.start(jobInfo.getId());
+	}
+	
+	@RequestMapping(value = "/add")
+	public ReturnT<String> add(@RequestBody XxlJobInfo jobInfo) {
+		return xxlJobService.add(jobInfo);
+	}
+	
+	@RequestMapping(value = "/update")
+	public ReturnT<String> update(@RequestBody XxlJobInfo jobInfo) {
+		return xxlJobService.update(jobInfo);
+	}
+	
+	@RequestMapping(value = "/remove")
+	public ReturnT<String> remove(@RequestBody XxlJobInfo jobInfo) {
+		return xxlJobService.remove(jobInfo.getId());
+	}
+	
+	@RequestMapping(value = "/stop")
+	public ReturnT<String> pause(@RequestBody XxlJobInfo jobInfo) {
+		return xxlJobService.stop(jobInfo.getId());
+	}
+	
+	@RequestMapping(value = "/start")
+	public ReturnT<String> start(@RequestBody XxlJobInfo jobInfo) {
+		return xxlJobService.start(jobInfo.getId());
+	}
+	
+	@RequestMapping(value = "/trigger")
+	public ReturnT<String> triggerJob(@RequestBody XxlJobInfo jobInfo) {
+		String executorParam = jobInfo.getExecutorParam();
+		if (StringUtil.isBlank(executorParam)) {
+			executorParam = CommonConstant.EMPTY;
+		}
+
+		JobTriggerPoolHelper.trigger(jobInfo.getId(), TriggerTypeEnum.MANUAL, -1, null, executorParam, jobInfo.getAddressList());
+		return ReturnT.SUCCESS;
+	}
+	
+}

+ 96 - 0
job-admin/src/main/java/com/xxl/job/admin/controller/custom/CustomInfoHandler.java

@@ -0,0 +1,96 @@
+package com.xxl.job.admin.controller.custom;
+
+import java.util.List;
+
+import javax.annotation.Resource;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import com.xxl.job.admin.core.model.OldXxlJobInfo;
+import com.xxl.job.admin.core.model.XxlJobInfo;
+import com.xxl.job.admin.dao.OldXxlJobInfoDao;
+import com.xxl.job.admin.service.XxlJobService;
+import com.xxl.job.core.biz.model.ReturnT;
+
+import cn.hutool.core.collection.CollectionUtil;
+
+/**
+ * 
+ * @version 1.0.0
+ * @company persagy 
+ * @author zhangqiankun
+ * @date 2021年9月24日 下午3:28:23
+ */
+@Component
+public class CustomInfoHandler {
+
+	@Resource
+	private XxlJobService xxlJobService;
+	
+	@Autowired
+	private OldXxlJobInfoDao oldXxlJobInfoDao;
+	
+	/**
+	 * 原数据迁移;group 的迁移走sql
+	 * <p>
+	 *  delete from xxl_job_group;
+	 *	insert into xxl_job_group (id, app_name, title, address_type, address_list, update_time) 
+	 *	SELECT id, app_name, title, 1, CONCAT("http://", address_list) AS address_list, now() FROM `xxl_job_qrtz_trigger_info` ;
+	 * </p>
+	 * 
+	 * @param jobInfo
+	 */
+	public ReturnT<String> transfer() {
+		// 获取原数据
+		List<OldXxlJobInfo> infoList = this.oldXxlJobInfoDao.infoList();
+		if (CollectionUtil.isEmpty(infoList)) {
+			return ReturnT.SUCCESS;
+		}
+		
+		for (OldXxlJobInfo oldXxlJobInfo : infoList) {
+			// 添加并启动
+			ReturnT<String> returnT = xxlJobService.add(this.transferDataInfo(oldXxlJobInfo));
+			if (ReturnT.SUCCESS_CODE == returnT.getCode()) {
+				xxlJobService.start(Integer.valueOf(returnT.getContent()));
+			}
+		}
+		
+		return ReturnT.SUCCESS;
+	}
+	
+	/**
+	 * 数据信息转换
+	 * 
+	 * @param oldXxlJobInfo
+	 * @return
+	 */
+	private XxlJobInfo transferDataInfo(OldXxlJobInfo oldXxlJobInfo) {
+		XxlJobInfo jobInfo = new XxlJobInfo();
+		jobInfo.setAuthor(oldXxlJobInfo.getAuthor());
+		jobInfo.setAddTime(oldXxlJobInfo.getAddTime());
+		jobInfo.setAlarmEmail(oldXxlJobInfo.getAlarmEmail());
+		jobInfo.setChildJobId(oldXxlJobInfo.getChildJobId());
+		jobInfo.setExecutorBlockStrategy(oldXxlJobInfo.getExecutorBlockStrategy());
+		jobInfo.setExecutorFailRetryCount(oldXxlJobInfo.getExecutorFailRetryCount());
+		jobInfo.setExecutorHandler(oldXxlJobInfo.getExecutorHandler());
+		jobInfo.setExecutorParam(oldXxlJobInfo.getExecutorParam());
+		jobInfo.setExecutorRouteStrategy(oldXxlJobInfo.getExecutorRouteStrategy());
+		jobInfo.setExecutorTimeout(oldXxlJobInfo.getExecutorTimeout());
+		jobInfo.setGlueRemark(oldXxlJobInfo.getGlueRemark());
+		jobInfo.setGlueSource(oldXxlJobInfo.getGlueSource());
+		jobInfo.setGlueType(oldXxlJobInfo.getGlueType());
+		jobInfo.setGlueUpdatetime(oldXxlJobInfo.getGlueUpdatetime());
+		jobInfo.setId(oldXxlJobInfo.getId());
+		jobInfo.setJobDesc(oldXxlJobInfo.getJobDesc());
+		jobInfo.setJobGroup(oldXxlJobInfo.getJobGroup());
+		jobInfo.setMisfireStrategy("DO_NOTHING");
+		jobInfo.setScheduleConf(oldXxlJobInfo.getJobCron());
+		jobInfo.setScheduleType("CRON");
+		jobInfo.setTriggerLastTime(0);
+		jobInfo.setTriggerNextTime(0);
+		jobInfo.setTriggerStatus(0);
+		return jobInfo;
+	}
+	
+}

+ 1 - 1
job-admin/src/main/java/com/xxl/job/admin/controller/interceptor/WebMvcConfig.java

@@ -27,7 +27,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
     public static final List<String> excludePaths = new ArrayList<String>();
     
     static {
-    	excludePaths.add("/admin/customJobInfo/**");
+    	excludePaths.add("/customInfo/**");
     	excludePaths.add("/v2/api-docs");
     	excludePaths.add("/swagger/**");
     	excludePaths.add("/swagger-resources/**");

+ 239 - 0
job-admin/src/main/java/com/xxl/job/admin/core/model/OldXxlJobInfo.java

@@ -0,0 +1,239 @@
+package com.xxl.job.admin.core.model;
+
+import java.util.Date;
+
+/**
+ * xxl-job info
+ *
+ * @author xuxueli  2016-1-12 18:25:49
+ */
+public class OldXxlJobInfo {
+	
+	private int id;				// 主键ID	    (JobKey.name)
+	
+	private int jobGroup;		// 执行器主键ID	(JobKey.group)
+	private String jobCron;		// 任务执行CRON表达式 【base on quartz】
+	private String jobDesc;
+	
+	private Date addTime;
+	private Date updateTime;
+	
+	private String author;		// 负责人
+	private String alarmEmail;	// 报警邮件
+
+	private String executorRouteStrategy;	// 执行器路由策略
+	private String executorHandler;		    // 执行器,任务Handler名称
+	private String executorParam;		    // 执行器,任务参数
+	private String executorBlockStrategy;	// 阻塞处理策略
+	private int executorTimeout;     		// 任务执行超时时间,单位秒
+	private int executorFailRetryCount;		// 失败重试次数
+	
+	private String glueType;		// GLUE类型	#com.xxl.job.core.glue.GlueTypeEnum
+	private String glueSource;		// GLUE源代码
+	private String glueRemark;		// GLUE备注
+	private Date glueUpdatetime;	// GLUE更新时间
+
+	private String childJobId;		// 子任务ID,多个逗号分隔
+	
+	// copy from quartz
+	private String jobStatus;		// 任务状态 【base on quartz】
+	
+	// custom trigger
+	private Long triggerStartTime;	// 自定义trigger开始日期
+	private Long triggerEndTime;		// 自定义trigger结束日期
+	private Integer intervalTime;		// 间隔时间,单位-S
+	private Integer repeatCount = 0;		// 重复执行次数,默认0,不重复 Integer.MAX_VALUE
+	
+	public int getId() {
+		return id;
+	}
+
+	public void setId(int id) {
+		this.id = id;
+	}
+
+	public int getJobGroup() {
+		return jobGroup;
+	}
+
+	public void setJobGroup(int jobGroup) {
+		this.jobGroup = jobGroup;
+	}
+
+	public String getJobCron() {
+		return jobCron;
+	}
+
+	public void setJobCron(String jobCron) {
+		this.jobCron = jobCron;
+	}
+
+	public String getJobDesc() {
+		return jobDesc;
+	}
+
+	public void setJobDesc(String jobDesc) {
+		this.jobDesc = jobDesc;
+	}
+
+	public Date getAddTime() {
+		return addTime;
+	}
+
+	public void setAddTime(Date addTime) {
+		this.addTime = addTime;
+	}
+
+	public Date getUpdateTime() {
+		return updateTime;
+	}
+
+	public void setUpdateTime(Date updateTime) {
+		this.updateTime = updateTime;
+	}
+
+	public String getAuthor() {
+		return author;
+	}
+
+	public void setAuthor(String author) {
+		this.author = author;
+	}
+
+	public String getAlarmEmail() {
+		return alarmEmail;
+	}
+
+	public void setAlarmEmail(String alarmEmail) {
+		this.alarmEmail = alarmEmail;
+	}
+
+	public String getExecutorRouteStrategy() {
+		return executorRouteStrategy;
+	}
+
+	public void setExecutorRouteStrategy(String executorRouteStrategy) {
+		this.executorRouteStrategy = executorRouteStrategy;
+	}
+
+	public String getExecutorHandler() {
+		return executorHandler;
+	}
+
+	public void setExecutorHandler(String executorHandler) {
+		this.executorHandler = executorHandler;
+	}
+
+	public String getExecutorParam() {
+		return executorParam;
+	}
+
+	public void setExecutorParam(String executorParam) {
+		this.executorParam = executorParam;
+	}
+
+	public String getExecutorBlockStrategy() {
+		return executorBlockStrategy;
+	}
+
+	public void setExecutorBlockStrategy(String executorBlockStrategy) {
+		this.executorBlockStrategy = executorBlockStrategy;
+	}
+
+	public int getExecutorTimeout() {
+		return executorTimeout;
+	}
+
+	public void setExecutorTimeout(int executorTimeout) {
+		this.executorTimeout = executorTimeout;
+	}
+
+	public int getExecutorFailRetryCount() {
+		return executorFailRetryCount;
+	}
+
+	public void setExecutorFailRetryCount(int executorFailRetryCount) {
+		this.executorFailRetryCount = executorFailRetryCount;
+	}
+
+	public String getGlueType() {
+		return glueType;
+	}
+
+	public void setGlueType(String glueType) {
+		this.glueType = glueType;
+	}
+
+	public String getGlueSource() {
+		return glueSource;
+	}
+
+	public void setGlueSource(String glueSource) {
+		this.glueSource = glueSource;
+	}
+
+	public String getGlueRemark() {
+		return glueRemark;
+	}
+
+	public void setGlueRemark(String glueRemark) {
+		this.glueRemark = glueRemark;
+	}
+
+	public Date getGlueUpdatetime() {
+		return glueUpdatetime;
+	}
+
+	public void setGlueUpdatetime(Date glueUpdatetime) {
+		this.glueUpdatetime = glueUpdatetime;
+	}
+
+	public String getChildJobId() {
+		return childJobId;
+	}
+
+	public void setChildJobId(String childJobId) {
+		this.childJobId = childJobId;
+	}
+
+	public String getJobStatus() {
+		return jobStatus;
+	}
+
+	public void setJobStatus(String jobStatus) {
+		this.jobStatus = jobStatus;
+	}
+
+	public Long getTriggerStartTime() {
+		return triggerStartTime;
+	}
+
+	public void setTriggerStartTime(Long triggerStartTime) {
+		this.triggerStartTime = triggerStartTime;
+	}
+
+	public Long getTriggerEndTime() {
+		return triggerEndTime;
+	}
+
+	public void setTriggerEndTime(Long triggerEndTime) {
+		this.triggerEndTime = triggerEndTime;
+	}
+
+	public Integer getIntervalTime() {
+		return intervalTime;
+	}
+
+	public void setIntervalTime(Integer intervalTime) {
+		this.intervalTime = intervalTime;
+	}
+
+	public Integer getRepeatCount() {
+		return repeatCount;
+	}
+
+	public void setRepeatCount(Integer repeatCount) {
+		this.repeatCount = repeatCount;
+	}
+
+}

+ 14 - 3
job-admin/src/main/java/com/xxl/job/admin/core/model/XxlJobInfo.java

@@ -9,7 +9,7 @@ import java.util.Date;
  */
 public class XxlJobInfo {
 	
-	private int id;				// 主键ID
+	private Integer id;				// 主键ID
 	
 	private int jobGroup;		// 执行器主键ID
 	private String jobDesc;
@@ -41,13 +41,15 @@ public class XxlJobInfo {
 	private int triggerStatus;		// 调度状态:0-停止,1-运行
 	private long triggerLastTime;	// 上次调度时间
 	private long triggerNextTime;	// 下次调度时间
+	
+	private String addressList;		//执行器地址
 
 
-	public int getId() {
+	public Integer getId() {
 		return id;
 	}
 
-	public void setId(int id) {
+	public void setId(Integer id) {
 		this.id = id;
 	}
 
@@ -234,4 +236,13 @@ public class XxlJobInfo {
 	public void setTriggerNextTime(long triggerNextTime) {
 		this.triggerNextTime = triggerNextTime;
 	}
+
+	public String getAddressList() {
+		return addressList;
+	}
+
+	public void setAddressList(String addressList) {
+		this.addressList = addressList;
+	}
+	
 }

+ 24 - 0
job-admin/src/main/java/com/xxl/job/admin/dao/OldXxlJobInfoDao.java

@@ -0,0 +1,24 @@
+package com.xxl.job.admin.dao;
+
+import java.util.List;
+
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import com.xxl.job.admin.core.model.OldXxlJobInfo;
+
+/**
+ * 
+ * @version 1.0.0
+ * @company persagy 
+ * @author zhangqiankun
+ * @date 2021年9月24日 下午3:56:30
+ */
+@Mapper
+public interface OldXxlJobInfoDao {
+
+	OldXxlJobInfo loadById(@Param("id") int id);
+	
+	List<OldXxlJobInfo> infoList();
+	
+}

+ 76 - 0
job-admin/src/main/resources/mybatis-mapper/OldXxlJobInfoMapper.xml

@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.xxl.job.admin.dao.OldXxlJobInfoDao">
+
+	<resultMap id="XxlJobInfo" type="com.xxl.job.admin.core.model.OldXxlJobInfo" >
+		<result column="id" property="id" />
+
+		<result column="job_group" property="jobGroup" />
+	    <result column="job_cron" property="jobCron" />
+	    <result column="job_desc" property="jobDesc" />
+
+	    <result column="add_time" property="addTime" />
+	    <result column="update_time" property="updateTime" />
+
+	    <result column="author" property="author" />
+	    <result column="alarm_email" property="alarmEmail" />
+
+		<result column="executor_route_strategy" property="executorRouteStrategy" />
+		<result column="executor_handler" property="executorHandler" />
+	    <result column="executor_param" property="executorParam" />
+		<result column="executor_block_strategy" property="executorBlockStrategy" />
+		<result column="executor_timeout" property="executorTimeout" />
+		<result column="executor_fail_retry_count" property="executorFailRetryCount" />
+
+	    <result column="glue_type" property="glueType" />
+	    <result column="glue_source" property="glueSource" />
+	    <result column="glue_remark" property="glueRemark" />
+		<result column="glue_updatetime" property="glueUpdatetime" />
+
+		<result column="child_jobid" property="childJobId" />
+		
+		<!-- custom trigger -->
+		<result column="trigger_start_time" property="triggerStartTime" />
+		<result column="trigger_end_time" property="triggerEndTime" />
+		<result column="interval_time" property="intervalTime" />
+		<result column="repeat_count" property="repeatCount" />
+		
+	</resultMap>
+
+	<sql id="Base_Column_List">
+		t.id,
+		t.job_group,
+		t.job_cron,
+		t.job_desc,
+		t.add_time,
+		t.update_time,
+		t.author,
+		t.alarm_email,
+		t.executor_route_strategy,
+		t.executor_handler,
+		t.executor_param,
+		t.executor_block_strategy,
+		t.executor_timeout,
+		t.executor_fail_retry_count,
+		t.glue_type,
+		t.glue_source,
+		t.glue_remark,
+		t.glue_updatetime,
+		t.child_jobid,
+		t.trigger_start_time,
+		t.trigger_end_time,
+		t.interval_time,
+		t.repeat_count
+	</sql>
+	
+	<select id="infoList" resultMap="XxlJobInfo">
+		SELECT <include refid="Base_Column_List" /> FROM xxl_job_qrtz_trigger_info AS t 
+	</select>
+	
+	<select id="loadById" resultMap="XxlJobInfo">
+		SELECT <include refid="Base_Column_List" />
+		FROM xxl_job_qrtz_trigger_info AS t WHERE t.id = #{id}
+	</select>
+	
+</mapper>