Set<ScheduledTask> scheduledTasks = scheduledTaskRegistrar.getScheduledTasks(); 停止单个任务

时间: 2023-05-27 21:02:08 浏览: 59
如果要停止单个任务,需要先获取所有已经安排的任务,然后找到要停止的任务,并调用其取消方法即可。 下面是一个示例代码: ``` Set<ScheduledTask> scheduledTasks = scheduledTaskRegistrar.getScheduledTasks(); for (ScheduledTask task : scheduledTasks) { if (task.getRunnable() instanceof MyScheduledTask) { // 假设要停止的任务类型是 MyScheduledTask MyScheduledTask myTask = (MyScheduledTask) task.getRunnable(); if (myTask.getId().equals("task001")) { // 假设要停止的任务 ID 是 task001 boolean result = task.cancel(); // 停止任务并返回是否成功 if (result) { System.out.println("任务已停止。"); } else { System.out.println("停止任务失败。"); } break; // 找到并停止了任务,退出循环 } } } ``` 这段代码首先获取所有已经安排的任务,遍历每个任务并检查类型和 ID 是否符合要求。如果找到了要停止的任务,就调用其取消方法,停止任务并输出结果。需要注意的是,必须在找到并停止了任务后,退出循环,否则还可能停止其他符合条件的任务。

相关推荐

@Datapublic class PropertiesMessage implements Cloneable, Serializable { public Map<String, DeviceProperties> propertiesMap; @Override public PropertiesMessage clone() throws CloneNotSupportedException { return (PropertiesMessage) super.clone(); }}@Datapublic class DeviceProperties implements Cloneable, Serializable { public String value; @Override public DeviceProperties clone() throws CloneNotSupportedException { return (DeviceProperties) super.clone(); }}@RequiredArgsConstructor(onConstructor = @(@Autowired))public class StorageListen { private static PropertiesMessage filteredRealtimeData = new PropertiesMessage(); private PeriodStorage periodStorage; @Scheduled(cron="*/1 * * * * ?") @Async("PeriodStorage") public void periodStorage() throws CloneNotSupportedException { PropertiesMessage clone = filteredRealtimeData.clone(); Map<String,List<StorageConfig>> map = new HashMap(); List<StorageConfig> list = new ArrayList(); StorageConfig s1 = new StorageConfig(); s1.setValue("1"); StorageConfig s2 = new StorageConfig(); s2.setValue("1"); list.add(s1); list.add(s2); map.put("1",list); periodStorage.storageData(clone,map); }}@RequiredArgsConstructor(onConstructor = @(@Autowired))public class PeriodStorage { private static DeviceProperties filteredRealtimeData = new DeviceProperties(); public void storageData(PropertiesMessage message, Map<String,List<StorageConfig>> map) throws CloneNotSupportedException { map.entrySet().stream().forEach(k -> { k.getValue().parallelStream().forEach(y -> { DeviceProperties clone = filteredRealtimeData.clone(); System.out.println(y.toString()); System.out.println(clone.toString()); }) }); }}

//KidsGPT定时任务 @Scheduled(cron = "00 20 0 * * *") // 秒、分、时、日期、月份、星期、年份(可省略) //@Scheduled(fixedRate = 5000) //五秒执行一次 public void processQuestionScores() { // 获取当前日期 LocalDate currentDate = LocalDate.now(); int year = currentDate.getYear(); int month = currentDate.getMonthValue(); int day = currentDate.getDayOfMonth(); // 获取题库总条数 int totalCount = regularMapper.getTotalCount(); // 遍历题目进行处理 for (int i = 1; i <= totalCount; i++) { // 查询题目 String question = regularMapper.getRegularById(i); if (!(question == null || question.isEmpty())) { try { // 计算时间 long startTime = System.currentTimeMillis(); String answer = regularImpl.getAnswerUseKnowledgeBase(question); long endTime = System.currentTimeMillis(); // 查询正确答案 String ranswer = regularMapper.getRegularModelById(String.valueOf(i)); // 查询到正确答案,继续下一步处理 if (!(ranswer == null || ranswer.isEmpty())) { // 计算分数 double score = MakeScoreUtil.calculateMeteorScore(ranswer, answer); // 得到正确分数,继续下一步处理 if (!Double.isNaN(score)) { // 保存记录 String source = "KidsGPT"; String version = year + "." + month + "." + day + "_version"; String spendTime = (endTime - startTime) + "ms"; // 计算处理时间 regularMapper.autoinsertRegular(question, answer, score, source, version, spendTime); logger.info("问题:" + i + ",保存成功"); } else { logger.info("未得到正确分数"); } } else { logger.info("未查询到正确答案"); } } catch (Exception e) { logger.error("查询题目答案时发生异常:" + e.getMessage()); } } else { logger.info("未查询到任何问题"); } } }帮我优化一下

// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package com.liruilong.hros.controller.statistics; import com.liruilong.hros.model.datas.DataModel; import com.liruilong.hros.service.EmployeeService; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping({"/statistics/personnel"}) public class PersonnelController { @Autowired EmployeeService employeeService; public PersonnelController() { } @Scheduled( cron = "0 0 3 * * ?" ) @GetMapping({"/Position"}) public List<DataModel> DataViewPosition() { return this.employeeService.dataViewPosition(); } @Scheduled( cron = "0 0 3 * * ?" ) @GetMapping({"/JobLevel"}) public List<DataModel> DataViewJobLevel() { return this.employeeService.dataViewJobLevel(); } @Scheduled( cron = "0 0 3 * * ?" ) @GetMapping({"/Department"}) public List<DataModel> DataViewDepartment() { return this.employeeService.dataViewDepartment(); } @Scheduled( cron = "0 0 3 * * ?" ) @GetMapping({"/tiptopdegree"}) public List<DataModel> DataViewtiptopdegree() { return this.employeeService.dataViewtiptopdegree(); } @Scheduled( cron = "0 0 3 * * ?" ) @GetMapping({"/school"}) public List<DataModel> DataViewschool() { return this.employeeService.dataViewschool(); } @Scheduled( cron = "0 0 3 * * ?" ) @GetMapping({"/nation"}) public List<DataModel> DataViewNation() { return this.employeeService.dataViewNation(); } @Scheduled( cron = "0 0 3 * * ?" ) @GetMapping({"/politicsstatus"}) public List<DataModel> DataViewPoliticsstatus() { return this.employeeService.dataViewPoliticsstatus(); } }

最新推荐

recommend-type

详解Spring Boot中使用@Scheduled创建定时任务

本篇文章中主要介绍了Spring Boot中使用@Scheduled创建定时任务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
recommend-type

SpringBoot中使用@Scheduled注解创建定时任务的实现

主要介绍了SpringBoot中使用@Scheduled注解创建定时任务的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Spring Boot @Scheduled定时任务代码实例解析

主要介绍了Spring Boot @Scheduled定时任务代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

Spring boot如何通过@Scheduled实现定时任务及多线程配置

主要介绍了Spring boot如何通过@Scheduled实现定时任务及多线程配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

ScheduledExecutorService任务定时代码示例

主要介绍了ScheduledExecutorService任务定时代码示例,具有一定借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用

![MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用](https://img-blog.csdnimg.cn/2020050917173284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2thbmdqaWVsZWFybmluZw==,size_16,color_FFFFFF,t_70) # 1. MATLAB取整函数概述** MATLAB取整函数是一组强大的工具,用于对数值进行
recommend-type

我想做python的算法工程师,我应该学什么?学习的顺序是什么?网上有什么推荐的免费课程吗?回答具体精确一点不要太笼统

对于想要成为 Python 算法工程师的人来说,他们应该先从掌握 Python 的基础语法开始,然后学习数据结构和算法,并且要深入了解 Python 中的一些科学计算和数据处理库,比如 NumPy、Pandas 等。 学习的顺序可以是先学习基础语法和编程技巧,然后再学习数据结构和算法相关的知识,最后深入了解数据处理和科学计算相关的库。 对于免费课程的推荐,我建议你可以先去 Coursera、edX、Udacity 等网站上寻找相关课程,这些网站上有很多优质的 Python 编程和算法课程,你可以根据自己的需求和学习进度进行选择。此外,还可以考虑一些在线编程网站,如 HackerRank、L
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。