feat:动态定时任务开发

This commit is contained in:
Xiang
2026-02-11 15:23:32 +08:00
parent 90433695e4
commit a3241d8469
36 changed files with 1065 additions and 10 deletions

View File

@@ -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>

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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)
);
}
}