web.controller.sys.SysTaskJobController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of task Show documentation
Show all versions of task Show documentation
jishi series products task on java
package web.controller.sys;
import framework.runtime.SystemContext;
import framework.utils.JsonUtil;
import framework.vo.R;
import framework.vo.ResultInfo;
import framework.vo.ResultList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import system.entity.SysTaskJob;
import system.service.sys.SysTaskJobService;
import system.task.Task;
import system.task.TaskManager;
import system.task.config.TaskConfig;
import system.task.defined.TaskStatus;
import system.task.errors.TaskException;
import system.task.utils.TaskUtil;
import system.task.vo.SysTaskRunning;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/sys/task/job")
@Api(tags = "任务计划")
public class SysTaskJobController {
@Autowired
private SysTaskJobService sysTaskJobService;
@Autowired
private TaskManager taskManager;
@Autowired
private TaskConfig taskConfig;
@ApiOperation("添加任务计划")
@PostMapping("/add")
public ResultInfo add(@ModelAttribute SysTaskJob param) throws TaskException {
java.util.Date now = new java.util.Date();
param.setCreateTime(now);
Integer added = sysTaskJobService.add(param);
// 导入新添加的任务
SysTaskJob job = sysTaskJobService.loadById(param.getId());
Task task = new Task();
BeanUtils.copyProperties(job, task);
taskManager.addTask(task);
//
ResultInfo info = new ResultInfo<>(param.getId());
return info;
}
@ApiOperation("删除任务计划")
@PostMapping("/del")
public ResultInfo del(@RequestParam("ids") List ids) throws TaskException {
int rows = 0;
List sysTaskJobs = sysTaskJobService.loadByIds(ids);
for (SysTaskJob sysTaskJob : sysTaskJobs) {
//删除任务
taskManager.delTask(sysTaskJob.getName());
//删除记录
rows += sysTaskJobService.deleteById(sysTaskJob.getId());
}
ResultInfo info = new ResultInfo<>(rows);
return info;
}
@ApiOperation("更新任务计划")
@PostMapping("/edit")
public ResultInfo edit(@ModelAttribute SysTaskJob param) throws TaskException {
SysTaskJob sysTaskJob = sysTaskJobService.loadById(param.getId());
if (sysTaskJob == null) {
return R.ok(0);
}
//
Task oldTask = taskManager.getTask(sysTaskJob.getName());
Integer updated = sysTaskJobService.update(param);
if (updated > 0) {
// 删除旧任务
if (oldTask != null) {
taskManager.delTask(oldTask.getName());
}
// 添加新任务
SysTaskJob job = sysTaskJobService.loadById(param.getId());
Task task = new Task();
BeanUtils.copyProperties(job, task);
taskManager.addTask(task);
}
//
ResultInfo info = new ResultInfo<>(updated);
return info;
}
@ApiOperation("查看任务计划")
@GetMapping("/info")
public ResultInfo info(@RequestParam("id") Long id) throws TaskException {
SysTaskRunning running = sysTaskJobService.loadInfo(id);
return new ResultInfo<>(running);
}
@ApiOperation("任务计划列表")
@GetMapping("/list")
public ResultList list(SysTaskJob param, @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws TaskException {
return sysTaskJobService.loadRunning(param, pageIndex, pageSize);
}
@ApiOperation("运行任务")
@PostMapping("/run")
public ResultInfo run(@RequestParam("name") String name, @RequestParam("param") String param) throws TaskException {
SysTaskJob query = new SysTaskJob();
query.setName(name);
SysTaskJob info = sysTaskJobService.first(query);
if (info == null) {
String msg = SystemContext.getMessageDefault("task.not.found", "Not found task " + name);
return R.failed(msg);
}
Task task = taskManager.getTask(name);
if (task == null) {
String msg = SystemContext.getMessageDefault("task.no.run.scheduler", "Task not run on this scheduler {0}", TaskUtil.hostname());
return R.failed(msg);
}
if (TaskStatus.DISABLED.equals(info.getStatus())) {
String msg = SystemContext.getMessageDefault("task.no.run.disabled", "not allowed run because task has disabled");
return R.failed(msg);
}
Map map = new HashMap<>();
//自定义参数构建
if (StringUtils.hasText(param)) {
HashMap hashMap = JsonUtil.parse(param, HashMap.class);
for (Object o : hashMap.keySet()) {
map.put(String.valueOf(o), hashMap.get(o));
}
}
//
try {
taskManager.startTask(name, map);
} catch (TaskException e) {
String msg = SystemContext.getMessageDefault("error.operate.failed", "operation failed {0}", e.getMessage());
return R.failed(msg);
}
return new ResultInfo<>(1);
}
@ApiOperation("停止任务")
@PostMapping("/stop")
public ResultInfo stop(@RequestParam("name") String name) throws TaskException {
SysTaskJob query = new SysTaskJob();
query.setName(name);
SysTaskJob info = sysTaskJobService.first(query);
if (info == null) {
String msg = SystemContext.getMessageDefault("task.not.found", "Not found task " + name);
return new ResultInfo<>().failed(msg);
}
Task task = taskManager.getTask(name);
if (task == null) {
String msg = SystemContext.getMessageDefault("task.no.run.scheduler", "Task not run on this scheduler {0}", TaskUtil.hostname());
return new ResultInfo<>().failed(msg);
}
try {
taskManager.stopTask(name);
} catch (TaskException e) {
String msg = SystemContext.getMessageDefault("error.operate.failed", "operation failed {0}", e.getMessage());
return new ResultInfo<>().failed(msg);
}
return new ResultInfo<>(1);
}
}