Compare commits
2 Commits
feat/sched
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9dd10862b2 | ||
|
|
a3241d8469 |
2
pom.xml
2
pom.xml
@@ -31,7 +31,7 @@
|
||||
<dependency>
|
||||
<groupId>com.xiang.starter</groupId>
|
||||
<artifactId>xmc-common</artifactId>
|
||||
<version>1.0</version>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.xiang.starter</groupId>
|
||||
|
||||
@@ -18,6 +18,13 @@
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.xiang.starter</groupId>
|
||||
<artifactId>xservice-quartz-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.xiang.app;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class HealthController {
|
||||
|
||||
@Value("${spring.profiles.active}")
|
||||
private String env;
|
||||
|
||||
@GetMapping("/actuator/health")
|
||||
public Map<String, String> checkHealth() {
|
||||
Map<String, String> map = Maps.newHashMap();
|
||||
map.put("env", env);
|
||||
map.put("status", "UP");
|
||||
log.info("cornucopia application health check success! listening in env:{}, now:{}", env, System.currentTimeMillis());
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.xiang.app.quartz.admin.domain.convert;
|
||||
|
||||
import com.xiang.app.quartz.admin.domain.entity.DynamicTaskDO;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskQueryRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskSaveRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskUpdateRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.response.DynamicTaskInfoResponse;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 11:14
|
||||
*/
|
||||
public class DynamicTaskConvert {
|
||||
public static DynamicTaskDO convert(DynamicTaskSaveRequest request) {
|
||||
DynamicTaskDO dynamicTaskDO = new DynamicTaskDO();
|
||||
dynamicTaskDO.setId(request.getId());
|
||||
dynamicTaskDO.setTaskName(request.getTaskName());
|
||||
dynamicTaskDO.setTaskGroup(request.getTaskGroup());
|
||||
dynamicTaskDO.setRunTime(request.getRunTime());
|
||||
dynamicTaskDO.setStatus(request.getStatus());
|
||||
dynamicTaskDO.setParameters(request.getParameters());
|
||||
dynamicTaskDO.setCreateTime(LocalDateTime.now());
|
||||
dynamicTaskDO.setUpdateTime(LocalDateTime.now());
|
||||
return dynamicTaskDO;
|
||||
}
|
||||
|
||||
public static DynamicTaskDO convert(DynamicTaskUpdateRequest request) {
|
||||
DynamicTaskDO dynamicTaskDO = new DynamicTaskDO();
|
||||
dynamicTaskDO.setId(request.getId());
|
||||
dynamicTaskDO.setTaskName(request.getTaskName());
|
||||
dynamicTaskDO.setTaskGroup(request.getTaskGroup());
|
||||
dynamicTaskDO.setRunTime(request.getRunTime());
|
||||
dynamicTaskDO.setStatus(request.getStatus());
|
||||
dynamicTaskDO.setParameters(request.getParameters());
|
||||
dynamicTaskDO.setUpdateTime(LocalDateTime.now());
|
||||
return dynamicTaskDO;
|
||||
}
|
||||
|
||||
public static DynamicTaskDO convert(DynamicTaskQueryRequest request) {
|
||||
DynamicTaskDO dynamicTaskDO = new DynamicTaskDO();
|
||||
dynamicTaskDO.setTaskName(request.getTaskName());
|
||||
dynamicTaskDO.setTaskGroup(request.getTaskGroup());
|
||||
dynamicTaskDO.setStatus(request.getStatus());
|
||||
return dynamicTaskDO;
|
||||
}
|
||||
|
||||
public static DynamicTaskInfoResponse convert(DynamicTaskDO entity) {
|
||||
DynamicTaskInfoResponse dynamicTaskInfoResponse = new DynamicTaskInfoResponse();
|
||||
dynamicTaskInfoResponse.setId(entity.getId());
|
||||
dynamicTaskInfoResponse.setTaskName(entity.getTaskName());
|
||||
dynamicTaskInfoResponse.setTaskGroup(entity.getTaskGroup());
|
||||
dynamicTaskInfoResponse.setRunTime(entity.getRunTime());
|
||||
dynamicTaskInfoResponse.setStatus(entity.getStatus());
|
||||
dynamicTaskInfoResponse.setParameters(entity.getParameters());
|
||||
dynamicTaskInfoResponse.setCreatedTime(entity.getCreateTime());
|
||||
dynamicTaskInfoResponse.setUpdatedTime(entity.getUpdateTime());
|
||||
return dynamicTaskInfoResponse;
|
||||
}
|
||||
|
||||
public static List<DynamicTaskInfoResponse> convertList(List<DynamicTaskDO> list) {
|
||||
return list.stream().map(DynamicTaskConvert::convert).toList();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.xiang.app.quartz.admin.domain.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@TableName("xmc_dynamic_task")
|
||||
public class DynamicTaskDO {
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 任务名称
|
||||
*/
|
||||
private String taskName;
|
||||
/**
|
||||
* 任务分组
|
||||
*/
|
||||
private String taskGroup;
|
||||
/**
|
||||
* 运行时间
|
||||
*/
|
||||
private LocalDateTime runTime;
|
||||
/**
|
||||
* 任务状态
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
private String parameters;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private LocalDateTime createTime;
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
private LocalDateTime updateTime;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.xiang.app.quartz.admin.manage;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.xiang.app.quartz.admin.domain.entity.DynamicTaskDO;
|
||||
import com.xiang.app.quartz.admin.mapper.IScheduledTaskMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 09:57
|
||||
*/
|
||||
@Service
|
||||
public class DynamicTaskManageImpl extends ServiceImpl<IScheduledTaskMapper, DynamicTaskDO> implements IDynamicTaskManage {
|
||||
@Override
|
||||
public List<DynamicTaskDO> getByList(DynamicTaskDO entity) {
|
||||
return baseMapper.getTaskList(entity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.xiang.app.quartz.admin.manage;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.xiang.app.quartz.admin.domain.entity.DynamicTaskDO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 09:57
|
||||
*/
|
||||
public interface IDynamicTaskManage extends IService<DynamicTaskDO> {
|
||||
|
||||
List<DynamicTaskDO> getByList(DynamicTaskDO entity);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.xiang.app.quartz.admin.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.xiang.app.quartz.admin.domain.entity.DynamicTaskDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
@Repository
|
||||
public interface IScheduledTaskMapper extends BaseMapper<DynamicTaskDO> {
|
||||
|
||||
int save(DynamicTaskDO entity);
|
||||
|
||||
int update(DynamicTaskDO entity);
|
||||
|
||||
DynamicTaskDO getTask(@Param("id") Long taskId);
|
||||
|
||||
List<DynamicTaskDO> getTaskList(DynamicTaskDO entity);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.xiang.app.quartz.admin.server;
|
||||
|
||||
import com.xiang.app.quartz.admin.service.IDynamicTaskService;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskQueryRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskSaveRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskUpdateRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.response.DynamicTaskInfoResponse;
|
||||
import com.xiang.xservice.basic.common.resp.Result;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 10:22
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/open/dynamic/task")
|
||||
public class DynamicTaskController {
|
||||
|
||||
private final IDynamicTaskService dynamicTaskService;
|
||||
|
||||
@PostMapping("/save")
|
||||
public Result<Void> saveTask(@RequestBody DynamicTaskSaveRequest request) {
|
||||
dynamicTaskService.saveTask(request);
|
||||
return Result.success();
|
||||
}
|
||||
@PostMapping("/update")
|
||||
public Result<Void> updateTask(@RequestBody DynamicTaskUpdateRequest request) {
|
||||
dynamicTaskService.updateTask(request);
|
||||
return Result.success();
|
||||
}
|
||||
@GetMapping("/info/{id}")
|
||||
public Result<DynamicTaskInfoResponse> info(@PathVariable("id") Long id) {
|
||||
return Result.data(dynamicTaskService.getTask(id));
|
||||
}
|
||||
@PostMapping("/getList")
|
||||
public Result<List<DynamicTaskInfoResponse>> list(@RequestBody DynamicTaskQueryRequest request) {
|
||||
return Result.data(dynamicTaskService.getTaskList(request));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,15 +22,15 @@ public class XxzJobFetchController {
|
||||
|
||||
private final ITaskConfigService taskConfigService;
|
||||
@GetMapping("/list")
|
||||
public Result<JobConfigDO> list(@RequestParam("appName") String appName, @RequestParam("env") Long env) {
|
||||
public Result<List<JobConfigDO>> list(@RequestParam("appName") String appName, @RequestParam("env") Long env) {
|
||||
List<JobConfigDO> jobsByAppName = taskConfigService.getJobsByAppName(appName, env);
|
||||
return Result.success(jobsByAppName);
|
||||
return Result.data(jobsByAppName);
|
||||
}
|
||||
|
||||
@GetMapping("/listByVersion")
|
||||
public Result<JobConfigDO> listByVersion(@RequestParam("appName") String appName,
|
||||
public Result<List<JobConfigDO>> listByVersion(@RequestParam("appName") String appName,
|
||||
@RequestParam("currVersion") Integer currVersion,
|
||||
@RequestParam("env") Long env) {
|
||||
return Result.success(taskConfigService.getJobsByAppNameAndVersion(appName, currVersion, env));
|
||||
return Result.data(taskConfigService.getJobsByAppNameAndVersion(appName, currVersion, env));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.xiang.app.quartz.admin.service;
|
||||
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskQueryRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskSaveRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskUpdateRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.response.DynamicTaskInfoResponse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 09:56
|
||||
*/
|
||||
public interface IDynamicTaskService {
|
||||
Boolean saveTask(DynamicTaskSaveRequest entity);
|
||||
Boolean updateTask(DynamicTaskUpdateRequest entity);
|
||||
DynamicTaskInfoResponse getTask(Long taskId);
|
||||
List<DynamicTaskInfoResponse> getTaskList(DynamicTaskQueryRequest entity);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.xiang.app.quartz.admin.service.impl;
|
||||
|
||||
import com.xiang.app.quartz.admin.domain.convert.DynamicTaskConvert;
|
||||
import com.xiang.app.quartz.admin.manage.IDynamicTaskManage;
|
||||
import com.xiang.app.quartz.admin.service.IDynamicTaskService;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskQueryRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskSaveRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskUpdateRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.response.DynamicTaskInfoResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class DynamicTaskSchedulerServiceImpl implements IDynamicTaskService {
|
||||
|
||||
private final IDynamicTaskManage dynamicTaskManage;
|
||||
|
||||
@Override
|
||||
public Boolean saveTask(DynamicTaskSaveRequest entity) {
|
||||
return dynamicTaskManage.save(DynamicTaskConvert.convert(entity));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updateTask(DynamicTaskUpdateRequest entity) {
|
||||
return dynamicTaskManage.updateById(DynamicTaskConvert.convert(entity));
|
||||
}
|
||||
|
||||
@Override
|
||||
public DynamicTaskInfoResponse getTask(Long taskId) {
|
||||
return DynamicTaskConvert.convert(dynamicTaskManage.getById(taskId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DynamicTaskInfoResponse> getTaskList(DynamicTaskQueryRequest entity) {
|
||||
return DynamicTaskConvert.convertList(dynamicTaskManage.getByList(DynamicTaskConvert.convert(entity)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
spring:
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
group: DEFAULT_GROUP
|
||||
namespace: 6f603892-e9f7-4ca4-acbc-538fa09ebec0
|
||||
server-addr: http://192.168.1.10:8848
|
||||
# 是否启用健康检查
|
||||
register-enabled: true
|
||||
username: nacos
|
||||
password: nacos
|
||||
datasource:
|
||||
dynamic:
|
||||
primary: master
|
||||
datasource:
|
||||
master:
|
||||
url: jdbc:mysql://120.27.153.87:3306/xservice_quartz?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
|
||||
username: quartz
|
||||
password: quartz@123
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
sshConnect: false
|
||||
|
||||
redis:
|
||||
host: r-bp1wt59a6nfyt4e3ltpd.redis.rds.aliyuncs.com
|
||||
port: 6379
|
||||
password: Xiang0000 # 如果无密码可以省略
|
||||
database: 10
|
||||
timeout: 5000
|
||||
lettuce:
|
||||
pool:
|
||||
max-active: 8
|
||||
max-idle: 8
|
||||
min-idle: 0
|
||||
max-wait: 1000
|
||||
xxz-job:
|
||||
appName: xservice-quartz-admin
|
||||
@@ -4,7 +4,9 @@ spring:
|
||||
discovery:
|
||||
group: DEFAULT_GROUP
|
||||
namespace: 00131110-3ecb-4a35-8bbb-624edde1d937
|
||||
server-addr: general.xiangtech.xyz:8848
|
||||
server-addr: http://general.xiangtech.xyz:8848
|
||||
# 是否启用健康检查
|
||||
register-enabled: false
|
||||
username: nacos
|
||||
password: nacos
|
||||
datasource:
|
||||
@@ -4,7 +4,9 @@ spring:
|
||||
discovery:
|
||||
group: DEFAULT_GROUP
|
||||
namespace: 6f603892-e9f7-4ca4-acbc-538fa09ebec0
|
||||
server-addr: general.xiangtech.xyz:8848
|
||||
server-addr: http://192.168.32.3:8848
|
||||
# 是否启用健康检查
|
||||
register-enabled: true
|
||||
username: nacos
|
||||
password: nacos
|
||||
datasource:
|
||||
|
||||
@@ -2,7 +2,7 @@ server:
|
||||
port: 30030
|
||||
spring:
|
||||
profiles:
|
||||
active: test
|
||||
active: local
|
||||
application:
|
||||
name: xservice-quartz-admin
|
||||
main:
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
<?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.xiang.app.quartz.admin.mapper.IScheduledTaskMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="com.xiang.app.quartz.admin.domain.entity.DynamicTaskDO" >
|
||||
<result column="id" property="id"/>
|
||||
<result column="task_name" property="taskName"/>
|
||||
<result column="task_group" property="taskGroup"/>
|
||||
<result column="run_time" property="runTime"/>
|
||||
<result column="status" property="status"/>
|
||||
<result column="parameters" property="parameters"/>
|
||||
<result column="create_time" property="createdTime"/>
|
||||
<result column="update_time" property="updatedTime"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id, task_name, task_group, run_time, status, parameters, create_time, update_time
|
||||
</sql>
|
||||
|
||||
<!-- 插入一条任务 -->
|
||||
<insert id="save" useGeneratedKeys="true" keyProperty="id" parameterType="com.xiang.app.quartz.admin.domain.entity.DynamicTaskDO">
|
||||
INSERT INTO script_schedule_task (id, task_name, task_group, run_time, status, parameters, create_time, update_time)
|
||||
VALUES (#{id}, #{taskName}, #{taskGroup}, #{runTime}, #{status}, #{parameters}, #{createdTime}, #{updatedTime})
|
||||
</insert>
|
||||
|
||||
<!-- 更新任务 -->
|
||||
<update id="update" parameterType="com.xiang.app.quartz.admin.domain.entity.DynamicTaskDO">
|
||||
UPDATE script_schedule_task
|
||||
SET task_name = #{taskName},
|
||||
task_group = #{taskGroup},
|
||||
run_time = #{runTime},
|
||||
status = #{status},
|
||||
parameters = #{parameters},
|
||||
create_time = #{createdTime},
|
||||
update_time = #{updatedTime}
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
|
||||
<select id="getTask" resultMap="BaseResultMap" parameterType="java.lang.Long">
|
||||
select <include refid="Base_Column_List"/>
|
||||
from script_schedule_task where id = #{id}
|
||||
</select>
|
||||
<select id="getTaskList" resultMap="BaseResultMap">
|
||||
select
|
||||
<include refid="Base_Column_List"/>
|
||||
from script_schedule_task
|
||||
<where>
|
||||
1=1
|
||||
<if test="taskName != null and taskName != ''">
|
||||
and task_name = #{taskName}
|
||||
</if>
|
||||
<if test="taskGroup != null and taskGroup != ''">
|
||||
and task_group = #{taskGroup}
|
||||
</if>
|
||||
<if test="status != null">
|
||||
and status = #{status}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.xiang.quartz.common.api;
|
||||
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskQueryRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskSaveRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskUpdateRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.response.DynamicTaskInfoResponse;
|
||||
import com.xiang.xservice.basic.common.resp.Result;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 10:59
|
||||
*/
|
||||
public interface DynamicTaskApi {
|
||||
|
||||
/**
|
||||
* 保存任务
|
||||
* @param request 新增请求
|
||||
* @return
|
||||
*/
|
||||
Result<Void> saveTask(DynamicTaskSaveRequest request);
|
||||
|
||||
/**
|
||||
* 修改任务
|
||||
* @param request 修改请求
|
||||
* @return
|
||||
*/
|
||||
Result<Void> updateTask(DynamicTaskUpdateRequest request);
|
||||
|
||||
/**
|
||||
* 查询任务详情
|
||||
* @param id 任务id
|
||||
* @return 任务详情
|
||||
*/
|
||||
Result<DynamicTaskInfoResponse> info(Long id);
|
||||
|
||||
/**
|
||||
* 查询任务列表
|
||||
* @param request 查询参数
|
||||
* @return 任务列表
|
||||
*/
|
||||
Result<List<DynamicTaskInfoResponse>> list(DynamicTaskQueryRequest request);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.xiang.quartz.common.entity.dynamic.request;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 11:10
|
||||
*/
|
||||
@Data
|
||||
public class DynamicTaskQueryRequest {
|
||||
/**
|
||||
* 任务名称
|
||||
*/
|
||||
private String taskName;
|
||||
/**
|
||||
* 任务分组
|
||||
*/
|
||||
private String taskGroup;
|
||||
/**
|
||||
* 运行时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime runTime;
|
||||
/**
|
||||
* 任务状态
|
||||
*/
|
||||
private Integer status;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.xiang.quartz.common.entity.dynamic.request;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 10:55
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class DynamicTaskSaveRequest {
|
||||
/**
|
||||
* 任务id
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 任务名称
|
||||
*/
|
||||
private String taskName;
|
||||
/**
|
||||
* 任务分组
|
||||
*/
|
||||
private String taskGroup;
|
||||
/**
|
||||
* 运行时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime runTime;
|
||||
/**
|
||||
* 任务状态
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
private String parameters;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.xiang.quartz.common.entity.dynamic.request;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 10:57
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class DynamicTaskUpdateRequest {
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 任务名称
|
||||
*/
|
||||
private String taskName;
|
||||
/**
|
||||
* 任务分组
|
||||
*/
|
||||
private String taskGroup;
|
||||
/**
|
||||
* 运行时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime runTime;
|
||||
/**
|
||||
* 任务状态
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
private String parameters;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.xiang.quartz.common.entity.dynamic.response;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 10:58
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class DynamicTaskInfoResponse {
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 任务名称
|
||||
*/
|
||||
private String taskName;
|
||||
/**
|
||||
* 任务分组
|
||||
*/
|
||||
private String taskGroup;
|
||||
/**
|
||||
* 运行时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime runTime;
|
||||
/**
|
||||
* 任务状态
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
private String parameters;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime createdTime;
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime updatedTime;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.xiang.quartz.common.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum TaskStatusEnum {
|
||||
UN_START(1, "未开始"),
|
||||
PROCEED(2, "进行中"),
|
||||
FINISHED(3, "已完成"),
|
||||
CANCELED(4, "取消"),
|
||||
ERROR(5, "错误"),
|
||||
;
|
||||
|
||||
private final Integer code;
|
||||
private final String desc;
|
||||
|
||||
TaskStatusEnum(Integer code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.xiang.quartz.common.enums.url;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 11:24
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum DynamicTaskUrlEnum {
|
||||
|
||||
|
||||
|
||||
SAVE_TASK("/open/dynamic/task/save", "保存任务", HttpMethod.POST),
|
||||
UPDATE_TASK("/open/dynamic/task/update", "保存任务",HttpMethod.POST),
|
||||
QUERY_TASK_INFO("/open/dynamic/task/info/", "查询任务详情", HttpMethod.GET),
|
||||
QUERY_TASK_LIST("/open/dynamic/task/list", "查询任务列表", HttpMethod.POST),
|
||||
|
||||
;
|
||||
private final String url;
|
||||
private final String desc;
|
||||
private final HttpMethod method;
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
<groupId>com.xiang.starter</groupId>
|
||||
<artifactId>xservice-quartz-core</artifactId>
|
||||
<version>1.1</version>
|
||||
<version>1.2</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
@@ -20,10 +20,16 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.xiang.starter</groupId>
|
||||
<artifactId>xservice-quartz-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.xiang.starter</groupId>
|
||||
<artifactId>xmc-common</artifactId>
|
||||
<version>1.0</version>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.xiang.starter</groupId>
|
||||
|
||||
@@ -13,6 +13,5 @@ public interface AdminJobClient {
|
||||
|
||||
List<JobConfigDO> fetchJobByAppName(String appName, Long namespace);
|
||||
List<JobConfigDO> fetchJobByAppNameAndVersion(String appName, Integer version, Long namespace);
|
||||
|
||||
void registerJob(JobDefinition jobDefinition, String className);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.xiang.core.schedule.api;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.TypeReference;
|
||||
import com.xiang.core.quartz.model.XxzJobProperties;
|
||||
import com.xiang.quartz.common.api.DynamicTaskApi;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskQueryRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskSaveRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskUpdateRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.response.DynamicTaskInfoResponse;
|
||||
import com.xiang.quartz.common.enums.url.DynamicTaskUrlEnum;
|
||||
import com.xiang.xmc.service.http.helper.BaseHttpHelp;
|
||||
import com.xiang.xmc.service.http.helper.HttpHelperFactory;
|
||||
import com.xiang.xservice.basic.common.resp.Result;
|
||||
import com.xiang.xservice.basic.common.resp.ResultUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 10:04
|
||||
*/
|
||||
@Service()
|
||||
@RequiredArgsConstructor
|
||||
public class HttpDynamicTaskClientImpl implements DynamicTaskApi {
|
||||
private final XxzJobProperties xxzJobProperties;
|
||||
private static final BaseHttpHelp httpHelp = HttpHelperFactory.createQuartzHttp();
|
||||
|
||||
@Override
|
||||
public Result<Void> saveTask(DynamicTaskSaveRequest request) {
|
||||
String baseUrl = xxzJobProperties.getAdminAddress();
|
||||
String resp = httpHelp.doPost(baseUrl + DynamicTaskUrlEnum.SAVE_TASK.getUrl(), null, JSON.toJSONString(request));
|
||||
return ResultUtils.getData(resp, new TypeReference<Result<Void>>() {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Void> updateTask(DynamicTaskUpdateRequest request) {
|
||||
String baseUrl = xxzJobProperties.getAdminAddress();
|
||||
String resp = httpHelp.doPost(baseUrl + DynamicTaskUrlEnum.UPDATE_TASK.getUrl(), null, JSON.toJSONString(request));
|
||||
return ResultUtils.getData(resp, new TypeReference<Result<Void>>() {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<DynamicTaskInfoResponse> info(Long id) {
|
||||
String baseUrl = xxzJobProperties.getAdminAddress();
|
||||
String resp = httpHelp.doGet(baseUrl + DynamicTaskUrlEnum.QUERY_TASK_INFO.getUrl() + id, null);
|
||||
return ResultUtils.getData(resp, new TypeReference<Result<DynamicTaskInfoResponse>>() {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<DynamicTaskInfoResponse>> list(DynamicTaskQueryRequest request) {
|
||||
String baseUrl = xxzJobProperties.getAdminAddress();
|
||||
String resp = httpHelp.doPost(baseUrl + DynamicTaskUrlEnum.QUERY_TASK_LIST.getUrl(), null, JSON.toJSONString(request));
|
||||
return ResultUtils.getData(resp, new TypeReference<Result<List<DynamicTaskInfoResponse>>>() {
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.xiang.core.schedule.config;
|
||||
|
||||
import com.xiang.core.schedule.core.DynamicTaskScheduler;
|
||||
import com.xiang.core.schedule.core.TaskRegistry;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@AutoConfiguration
|
||||
@Import({DynamicTaskSchedulerConfig.class, DynamicTaskScheduler.class, TaskRegistry.class})
|
||||
public class DynamicSchedulerAutoConfiguration {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.xiang.core.schedule.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
|
||||
@Configuration
|
||||
public class DynamicTaskSchedulerConfig {
|
||||
@Bean
|
||||
public ThreadPoolTaskScheduler globalTaskScheduler() {
|
||||
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
|
||||
scheduler.setPoolSize(10);
|
||||
scheduler.setThreadNamePrefix("dynamic-task-");
|
||||
scheduler.setWaitForTasksToCompleteOnShutdown(true);
|
||||
scheduler.setAwaitTerminationSeconds(30);
|
||||
scheduler.initialize();
|
||||
return scheduler;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.xiang.core.schedule.core;
|
||||
|
||||
import com.xiang.core.schedule.entity.TaskConfig;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class DynamicTaskScheduler {
|
||||
private final ThreadPoolTaskScheduler taskScheduler;
|
||||
private final Map<Long, ScheduledFuture<?>> taskMap = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
public void schedule(TaskConfig config, Runnable task) {
|
||||
schedule(config, task, LocalDateTime.now());
|
||||
}
|
||||
|
||||
public void schedule(TaskConfig config, Runnable task, LocalDateTime serverTime) {
|
||||
LocalDateTime time = config.getExecutionTime();
|
||||
if (time.isBefore(serverTime)) return;
|
||||
|
||||
ScheduledFuture<?> future = taskScheduler.schedule(() -> {
|
||||
try {
|
||||
task.run();
|
||||
} finally {
|
||||
taskMap.remove(config.getTaskId());
|
||||
}
|
||||
}, Date.from(time.atZone(ZoneId.systemDefault()).toInstant()));
|
||||
taskMap.put(config.getTaskId(), future);
|
||||
}
|
||||
|
||||
public void cancel(Long taskId) {
|
||||
ScheduledFuture<?> future = taskMap.get(taskId);
|
||||
if (future != null) {
|
||||
future.cancel(true);
|
||||
taskMap.remove(taskId);
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean contains(Long taskId) {
|
||||
return taskMap.containsKey(taskId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.xiang.core.schedule.core;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class TaskRegistry {
|
||||
private final ConcurrentHashMap<Long, Consumer<Long>> handlers = new ConcurrentHashMap<>();
|
||||
|
||||
public void register(Long taskId, Consumer<Long> consumer) {
|
||||
handlers.put(taskId, consumer);
|
||||
}
|
||||
|
||||
public void run(Long taskId) {
|
||||
Consumer<Long> consumer = handlers.get(taskId);
|
||||
if (consumer != null) consumer.accept(taskId);
|
||||
}
|
||||
|
||||
public void unregister(Long taskId) {
|
||||
handlers.remove(taskId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.xiang.core.schedule.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class TaskConfig {
|
||||
private Long taskId;
|
||||
private String taskName;
|
||||
private String taskGroup;
|
||||
private LocalDateTime executionTime;
|
||||
private Map<String, Object> parameters;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.xiang.core.schedule.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum TaskGroupEnum {
|
||||
SERVICE_SAMPLE_SCHEDULE("sample", "测试动态定时任务"),
|
||||
SERVICE_FWD_SCHEDULE("xs-fwd", "芬玩岛定时任务");
|
||||
|
||||
private final String code;
|
||||
private final String desc;
|
||||
|
||||
TaskGroupEnum(String code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.xiang.core.schedule.helper;
|
||||
|
||||
|
||||
import com.xiang.core.schedule.entity.TaskConfig;
|
||||
|
||||
public interface TaskExecutor {
|
||||
void execute(TaskConfig taskInfo);
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package com.xiang.core.schedule.service;
|
||||
|
||||
import com.xiang.quartz.common.api.DynamicTaskApi;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskQueryRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskSaveRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskUpdateRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.response.DynamicTaskInfoResponse;
|
||||
import com.xiang.quartz.common.enums.TaskStatusEnum;
|
||||
import com.xiang.xservice.basic.common.resp.Result;
|
||||
import com.xiang.xservice.basic.common.resp.ResultUtils;
|
||||
import com.xiang.xservice.basic.exception.BusinessException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class DynamicTaskSchedulerServiceImpl implements IDynamicTaskSchedulerService{
|
||||
|
||||
private final DynamicTaskApi dynamicTaskApi;
|
||||
|
||||
@Override
|
||||
public Boolean saveTask(DynamicTaskSaveRequest entity) {
|
||||
Result<Void> result = dynamicTaskApi.saveTask(entity);
|
||||
if (StringUtils.equals(Result.SUCCESS_CODE, result.getCode())) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updateTask(DynamicTaskUpdateRequest entity) {
|
||||
Result<Void> result = dynamicTaskApi.updateTask(entity);
|
||||
if (StringUtils.equals(Result.SUCCESS_CODE, result.getCode())) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DynamicTaskInfoResponse getTask(Long taskId) {
|
||||
return ResultUtils.getData(dynamicTaskApi.info(taskId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updateProcess(Long taskId) {
|
||||
DynamicTaskInfoResponse task = getTask(taskId);
|
||||
if (Objects.isNull(task)) {
|
||||
throw new BusinessException("task不存在!");
|
||||
}
|
||||
DynamicTaskUpdateRequest dynamicTaskUpdateRequest = buildUpdateTaskRequest(task);
|
||||
dynamicTaskUpdateRequest.setStatus(TaskStatusEnum.PROCEED.getCode());
|
||||
return updateTask(dynamicTaskUpdateRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean cancelTask(Long taskId) {
|
||||
DynamicTaskInfoResponse task = getTask(taskId);
|
||||
if (Objects.isNull(task)) {
|
||||
throw new BusinessException("task不存在!");
|
||||
}
|
||||
DynamicTaskUpdateRequest dynamicTaskUpdateRequest = buildUpdateTaskRequest(task);
|
||||
dynamicTaskUpdateRequest.setStatus(TaskStatusEnum.CANCELED.getCode());
|
||||
return updateTask(dynamicTaskUpdateRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean finishTask(Long taskId) {
|
||||
DynamicTaskInfoResponse task = getTask(taskId);
|
||||
if (Objects.isNull(task)) {
|
||||
throw new BusinessException("task不存在!");
|
||||
}
|
||||
DynamicTaskUpdateRequest dynamicTaskUpdateRequest = buildUpdateTaskRequest(task);
|
||||
dynamicTaskUpdateRequest.setStatus(TaskStatusEnum.FINISHED.getCode());
|
||||
return updateTask(dynamicTaskUpdateRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean errTask(Long taskId) {
|
||||
DynamicTaskInfoResponse task = getTask(taskId);
|
||||
if (Objects.isNull(task)) {
|
||||
throw new BusinessException("task不存在!");
|
||||
}
|
||||
DynamicTaskUpdateRequest dynamicTaskUpdateRequest = buildUpdateTaskRequest(task);
|
||||
dynamicTaskUpdateRequest.setStatus(TaskStatusEnum.ERROR.getCode());
|
||||
return updateTask(dynamicTaskUpdateRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DynamicTaskInfoResponse> getTaskList(DynamicTaskQueryRequest entity) {
|
||||
return ResultUtils.getData(dynamicTaskApi.list(entity));
|
||||
}
|
||||
|
||||
private DynamicTaskUpdateRequest buildUpdateTaskRequest(DynamicTaskInfoResponse response) {
|
||||
DynamicTaskUpdateRequest dynamicTaskUpdateRequest = new DynamicTaskUpdateRequest();
|
||||
dynamicTaskUpdateRequest.setId(response.getId());
|
||||
dynamicTaskUpdateRequest.setTaskName(response.getTaskName());
|
||||
dynamicTaskUpdateRequest.setTaskGroup(response.getTaskGroup());
|
||||
dynamicTaskUpdateRequest.setRunTime(response.getRunTime());
|
||||
dynamicTaskUpdateRequest.setStatus(response.getStatus());
|
||||
dynamicTaskUpdateRequest.setParameters(response.getParameters());
|
||||
return dynamicTaskUpdateRequest;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.xiang.core.schedule.service;
|
||||
|
||||
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskQueryRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskSaveRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskUpdateRequest;
|
||||
import com.xiang.quartz.common.entity.dynamic.response.DynamicTaskInfoResponse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public interface IDynamicTaskSchedulerService {
|
||||
Boolean saveTask(DynamicTaskSaveRequest entity);
|
||||
Boolean updateTask(DynamicTaskUpdateRequest entity);
|
||||
DynamicTaskInfoResponse getTask(Long taskId);
|
||||
Boolean updateProcess(Long taskId);
|
||||
Boolean cancelTask(Long taskId);
|
||||
Boolean finishTask(Long taskId);
|
||||
Boolean errTask(Long taskId);
|
||||
List<DynamicTaskInfoResponse> getTaskList(DynamicTaskQueryRequest entity);
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.xiang.core.quartz.config.XxzJobAutoConfiguration
|
||||
com.xiang.core.quartz.config.XxzJobAutoConfiguration, \
|
||||
com.xiang.core.schedule.config.DynamicSchedulerAutoConfiguration
|
||||
@@ -22,7 +22,7 @@
|
||||
<dependency>
|
||||
<groupId>com.xiang.starter</groupId>
|
||||
<artifactId>xservice-quartz-core</artifactId>
|
||||
<version>1.1</version>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.xiang.app.dynamic.task.sample.springboot.server;
|
||||
|
||||
import com.xiang.app.dynamic.task.sample.springboot.task.TestTaskService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 14:12
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/sample/dynamic/task")
|
||||
public class TestController {
|
||||
private final TestTaskService testTaskService;
|
||||
|
||||
@GetMapping("/test")
|
||||
public void test() {
|
||||
testTaskService.test();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.xiang.app.dynamic.task.sample.springboot.task;
|
||||
|
||||
import com.xiang.core.schedule.service.IDynamicTaskSchedulerService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 14:11
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class TestTask implements Runnable {
|
||||
|
||||
private final IDynamicTaskSchedulerService dynamicTaskSchedulerService;
|
||||
private final Long taskId;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
dynamicTaskSchedulerService.updateProcess(taskId);
|
||||
log.info("动态任务开始运行!!!");
|
||||
try {
|
||||
log.info("模拟任务处理 10秒钟");
|
||||
Thread.sleep(10000);
|
||||
} catch (InterruptedException e) {
|
||||
log.error("处理失败");
|
||||
}
|
||||
log.info("任务处理完成!");
|
||||
dynamicTaskSchedulerService.finishTask(taskId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.xiang.app.dynamic.task.sample.springboot.task;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.xiang.core.schedule.core.DynamicTaskScheduler;
|
||||
import com.xiang.core.schedule.entity.TaskConfig;
|
||||
import com.xiang.core.schedule.enums.TaskGroupEnum;
|
||||
import com.xiang.core.schedule.service.IDynamicTaskSchedulerService;
|
||||
import com.xiang.quartz.common.entity.dynamic.request.DynamicTaskSaveRequest;
|
||||
import com.xiang.quartz.common.enums.TaskStatusEnum;
|
||||
import com.xiang.xservice.basic.utils.PrimaryKeyUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-11 14:06
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class TestTaskService {
|
||||
private final DynamicTaskScheduler dynamicTaskScheduler;
|
||||
private final IDynamicTaskSchedulerService dynamicTaskSchedulerService;
|
||||
|
||||
public void test() {
|
||||
long taskId = PrimaryKeyUtils.snowflakeId();
|
||||
Map<String, Object> params = Maps.newHashMap();
|
||||
LocalDateTime runTime = LocalDateTime.of(2026, 2, 11, 15, 23, 00);
|
||||
dynamicTaskSchedulerService.saveTask(new DynamicTaskSaveRequest(taskId, "测试动态任务运行", TaskGroupEnum.SERVICE_SAMPLE_SCHEDULE.getDesc(), runTime, TaskStatusEnum.UN_START.getCode(), JSON.toJSONString(params)));
|
||||
dynamicTaskScheduler.schedule(
|
||||
new TaskConfig(taskId, "测试动态任务运行", TaskGroupEnum.SERVICE_SAMPLE_SCHEDULE.getCode(), runTime, params),
|
||||
new TestTask(dynamicTaskSchedulerService, taskId)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user