|
|
@@ -10,6 +10,7 @@ import com.zksy.data.service.XhTimeTaskService;
|
|
|
import com.zksy.data.mapper.XhTimeTaskMapper;
|
|
|
import com.zksy.data.utils.TaskManager;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.Date;
|
|
|
@@ -25,8 +26,19 @@ public class XhTimeTaskServiceImpl extends ServiceImpl<XhTimeTaskMapper, XhTimeT
|
|
|
implements XhTimeTaskService{
|
|
|
@Autowired
|
|
|
private TaskManager taskManager;
|
|
|
+ @Autowired
|
|
|
+ private ApplicationContext applicationContext;
|
|
|
@Override
|
|
|
public Result<String> saveTimeTask(TimeTaskDto timeTaskDto) {
|
|
|
+ boolean b = applicationContext.containsBean(timeTaskDto.getServiceName());
|
|
|
+ if(!b){
|
|
|
+ return Result.error("你想添加的服务不存在");
|
|
|
+ }
|
|
|
+ LambdaQueryWrapper<XhTimeTask> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(XhTimeTask::getServiceName, timeTaskDto.getServiceName());
|
|
|
+ if(this.count(wrapper)>0){
|
|
|
+ return Result.error("该服务已存在");
|
|
|
+ }
|
|
|
XhTimeTask task = new XhTimeTask();
|
|
|
task.setCreatedAt(new Date());
|
|
|
task.setUpdatedAt(new Date());
|
|
|
@@ -36,14 +48,6 @@ public class XhTimeTaskServiceImpl extends ServiceImpl<XhTimeTaskMapper, XhTimeT
|
|
|
task.setScheduleType(timeTaskDto.getScheduleType());
|
|
|
task.setEnabled(1);
|
|
|
this.save(task);
|
|
|
- taskManager.addTask(task.getTaskName(), () -> {
|
|
|
- try {
|
|
|
- Thread.sleep(1000);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- System.out.println("执行任务");
|
|
|
- }, timeTaskDto.getCronExpression());
|
|
|
return Result.ok("添加定时任务成功");
|
|
|
}
|
|
|
|