fix: 新增手动启动定时任务接口

This commit is contained in:
zhangyanduan
2022-10-21 09:47:06 +08:00
parent 04749fd69d
commit 9542200d13
2 changed files with 41 additions and 1 deletions
@@ -9,6 +9,7 @@ package com.adc.da.search.conf;
import com.adc.da.slrs.tsTask.dao.TbTaskDao;
import com.adc.da.slrs.tsTask.entity.TbTask;
import com.adc.da.slrs.tsTask.service.ITbTaskService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -95,7 +96,7 @@ public class SchedulerConf {
log.info("{},任务启动!", taskId);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
log.error("{},任务启动失败...", taskId);
e.printStackTrace();
log.error(e.getMessage(),e);
}
}
@@ -113,6 +114,28 @@ public class SchedulerConf {
log.info("{},任务停止...", taskId);
}
public TbTask manualRunTask(String taskId){
TbTask task = iTbTaskService.getById(taskId);
if(task!=null){
try {
//获取并实例化Runnable任务类
Class<?> clazz = Class.forName(task.getTaskClass());
Runnable runnable = (Runnable) clazz.newInstance();
Thread runTaskThread = new Thread(runnable);
runTaskThread.start();
log.info("{},任务开始启动执行!", task.getTaskName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
log.error("{},任务启动失败...", task.getTaskName());
log.error(e.getMessage(),e);
}
return task;
}else{
return null;
}
}
/**
* 根据ip开启定时任务
*/
@@ -1,8 +1,11 @@
package com.adc.da.slrs.tsTask.controller;
import com.adc.da.http.ResponseMessage;
import com.adc.da.http.Result;
import com.adc.da.search.conf.SchedulerConf;
import com.adc.da.slrs.tsTask.service.ITbTaskService;
import io.swagger.annotations.ApiOperation;
import oracle.ucp.proxy.annotation.Post;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -55,6 +58,20 @@ public class TbTaskController extends BaseController<TbTask> {
return "操作成功";
}
@ApiOperation(value = "手动启动定时任务")
@GetMapping("/manualRunTask/{taskId}")
public ResponseMessage manualRunTask(@PathVariable("taskId") String taskId) {
TbTask tbTask = schedulerConf.manualRunTask(taskId);
if(tbTask!=null){
return Result.success("开始启动定时任务:《"+tbTask.getTaskName()+"");
}else{
return Result.error("定时任务未找到:"+taskId);
}
}
/**
* 根据ip开启定时任务
* http://localhost:10085/tbTask