图片定时任务
This commit is contained in:
@@ -0,0 +1,89 @@
|
|||||||
|
package top.wms.admin.controller.tcp.service;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.scheduling.TaskScheduler;
|
||||||
|
import org.springframework.scheduling.support.CronTrigger;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import top.wms.admin.controller.tcp.manager.ChannelManager;
|
||||||
|
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态定时任务服务
|
||||||
|
* 支持通过接口触发和取消定时任务
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class SaveBmpTaskService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TaskScheduler taskScheduler;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ChannelManager channelManager;
|
||||||
|
|
||||||
|
// 存储定时任务的Future对象,用于取消任务
|
||||||
|
private ScheduledFuture<?> scheduledTask;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动定时任务
|
||||||
|
* 当用户进入称重管理的扫码核验页面时调用
|
||||||
|
*/
|
||||||
|
public boolean startTask() {
|
||||||
|
// 先取消可能存在的任务
|
||||||
|
stopTask();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 创建并启动定时任务,每1秒执行一次
|
||||||
|
scheduledTask = taskScheduler.schedule(() -> {
|
||||||
|
try {
|
||||||
|
// 执行任务逻辑
|
||||||
|
executeTask();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("定时任务执行失败: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}, new CronTrigger("*/1 * * * * ?"));
|
||||||
|
|
||||||
|
log.info("定时任务已启动");
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("启动定时任务失败: {}", e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止定时任务
|
||||||
|
* 当用户离开称重管理的扫码核验页面时调用
|
||||||
|
*/
|
||||||
|
public boolean stopTask() {
|
||||||
|
if (scheduledTask != null && !scheduledTask.isCancelled()) {
|
||||||
|
scheduledTask.cancel(false);
|
||||||
|
scheduledTask = null;
|
||||||
|
log.info("定时任务已停止");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行定时任务的具体逻辑
|
||||||
|
*/
|
||||||
|
private void executeTask() {
|
||||||
|
try {
|
||||||
|
// 001流程是执行保存图片到本地
|
||||||
|
log.debug("执行定时任务,发送命令: 001");
|
||||||
|
channelManager.getFirstChannel().writeAndFlush("001");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("执行定时任务失败: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查定时任务是否正在运行
|
||||||
|
*/
|
||||||
|
public boolean isTaskRunning() {
|
||||||
|
return scheduledTask != null && !scheduledTask.isCancelled();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
import top.continew.starter.core.validation.CheckUtils;
|
import top.continew.starter.core.validation.CheckUtils;
|
||||||
import top.wms.admin.controller.tcp.config.SimpleRequestMatcher;
|
import top.wms.admin.controller.tcp.config.SimpleRequestMatcher;
|
||||||
import top.wms.admin.controller.tcp.manager.ChannelManager;
|
import top.wms.admin.controller.tcp.manager.ChannelManager;
|
||||||
import top.wms.admin.materialProcess.mapper.MaterialProcessMapper;
|
import top.wms.admin.controller.tcp.service.SaveBmpTaskService;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
@@ -22,6 +22,9 @@ public class VmCommandController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private SimpleRequestMatcher requestMatcher;
|
private SimpleRequestMatcher requestMatcher;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SaveBmpTaskService saveBmpTaskService;
|
||||||
|
|
||||||
@PostMapping("/send")
|
@PostMapping("/send")
|
||||||
public String sendAndWait(@RequestBody JSONObject js) {
|
public String sendAndWait(@RequestBody JSONObject js) {
|
||||||
String materialProcess = js.getString("materialProcess");
|
String materialProcess = js.getString("materialProcess");
|
||||||
@@ -44,4 +47,14 @@ public class VmCommandController {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/start")
|
||||||
|
public boolean startTask() {
|
||||||
|
return saveBmpTaskService.startTask();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/stop")
|
||||||
|
public boolean stopTask() {
|
||||||
|
return saveBmpTaskService.stopTask();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user