feat: 增加根据beanName及方法调用接口
This commit is contained in:
@@ -4,11 +4,16 @@ import com.sqx.common.annotation.SysLog;
|
||||
import com.sqx.common.utils.PageUtils;
|
||||
import com.sqx.common.utils.Result;
|
||||
import com.sqx.common.validator.ValidatorUtils;
|
||||
import com.sqx.modules.job.dto.RunJobDTO;
|
||||
import com.sqx.modules.job.entity.ScheduleJobEntity;
|
||||
import com.sqx.modules.job.service.ScheduleJobService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -20,8 +25,15 @@ import java.util.Map;
|
||||
public class ScheduleJobController {
|
||||
@Autowired
|
||||
private ScheduleJobService scheduleJobService;
|
||||
|
||||
/**
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public ScheduleJobController(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 定时任务列表
|
||||
*/
|
||||
@RequestMapping("/list")
|
||||
@@ -30,17 +42,17 @@ public class ScheduleJobController {
|
||||
|
||||
return Result.success().put("page", page);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 定时任务信息
|
||||
*/
|
||||
@RequestMapping("/info/{jobId}")
|
||||
public Result info(@PathVariable("jobId") Long jobId){
|
||||
ScheduleJobEntity schedule = scheduleJobService.getById(jobId);
|
||||
|
||||
|
||||
return Result.success().put("schedule", schedule);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 保存定时任务
|
||||
*/
|
||||
@@ -48,12 +60,12 @@ public class ScheduleJobController {
|
||||
@RequestMapping("/save")
|
||||
public Result save(@RequestBody ScheduleJobEntity scheduleJob){
|
||||
ValidatorUtils.validateEntity(scheduleJob);
|
||||
|
||||
|
||||
scheduleJobService.saveJob(scheduleJob);
|
||||
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 修改定时任务
|
||||
*/
|
||||
@@ -61,12 +73,12 @@ public class ScheduleJobController {
|
||||
@RequestMapping("/update")
|
||||
public Result update(@RequestBody ScheduleJobEntity scheduleJob){
|
||||
ValidatorUtils.validateEntity(scheduleJob);
|
||||
|
||||
|
||||
scheduleJobService.update(scheduleJob);
|
||||
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除定时任务
|
||||
*/
|
||||
@@ -74,13 +86,40 @@ public class ScheduleJobController {
|
||||
@RequestMapping("/delete")
|
||||
public Result delete(@RequestBody Long[] jobIds){
|
||||
scheduleJobService.deleteBatch(jobIds);
|
||||
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 立即执行任务
|
||||
*/
|
||||
|
||||
@SysLog("立即执行任务")
|
||||
@RequestMapping("/run")
|
||||
public Result run(@RequestBody @Validated RunJobDTO jobDTO) throws Exception {
|
||||
// 从Spring容器获取类的实例
|
||||
Object classInstance = applicationContext.getBean(jobDTO.getClassName());
|
||||
|
||||
if (jobDTO.getArgs() != null && !jobDTO.getArgs().isEmpty()) {
|
||||
Class<?>[] argTypes = new Class<?>[jobDTO.getArgs().size()];
|
||||
for (int i = 0; i < jobDTO.getArgs().size(); i++) {
|
||||
argTypes[i] = jobDTO.getArgs().get(i).getClass();
|
||||
}
|
||||
// 获取指定的Method对象
|
||||
Method method = classInstance.getClass().getDeclaredMethod(jobDTO.getMethodName(), argTypes);
|
||||
|
||||
// 调用方法并返回结果
|
||||
Object invoke = method.invoke(classInstance, jobDTO.getArgs().toArray());
|
||||
return Result.success().put("data", invoke == null ? "ok" : invoke);
|
||||
}
|
||||
// 获取指定的Method对象
|
||||
Method method = classInstance.getClass().getDeclaredMethod(jobDTO.getMethodName());
|
||||
|
||||
// 调用方法并返回结果
|
||||
Object invoke = method.invoke(classInstance);
|
||||
return Result.success().put("data", invoke == null ? "ok" : invoke);
|
||||
}
|
||||
|
||||
// @SysLog("立即执行任务")
|
||||
// @RequestMapping("/run")
|
||||
// public Result run(@RequestBody Long[] jobIds){
|
||||
@@ -88,7 +127,7 @@ public class ScheduleJobController {
|
||||
//
|
||||
// return Result.success();
|
||||
// }
|
||||
//
|
||||
|
||||
// /**
|
||||
// * 暂停定时任务
|
||||
// */
|
||||
|
||||
Reference in New Issue
Block a user