当前位置: 首页 > 知识库问答 >
问题:

spring boot如何用@scheduled运行多个方法

冉锋
2023-03-14

我有一个spring boot应用程序,我想有多个方法运行在一天的不同时间。第一个方法运行,但没有后续方法运行。我需要做什么来解决这个问题?这里是我的代码:@enableScheduling@configuration//@conditionalonproperty(name=“spring.enable.scheduling”)@springbootapplication@propertysources({@propertysource(value=“prop.properties”,ignoreResourceNotFound=true)})公共类应用程序{private static final Logger Logger=LoggerFactory.getLogger(Application.class);public static MyClass类=new MyClass();public static

    SpringApplication.run(Application.class, args);
}

@Scheduled(cron = "${4am.cron.expression}", zone = "America/New_York") //0 0 6 * * ?
public void method1() {
    something;
}

@Scheduled(cron = "${10am.cron.expression}", zone = "America/New_York") //0 0 6 * * ?
public void method2() {
    something;
}

@Scheduled(cron = "${10am.cron.expression}", zone = "America/New_York") //0 0 6 * * ?
public void method3() {
    something;
}

@Scheduled(cron = "${330pm.cron.expression}", zone = "America/New_York") //0 0 6 * * ?
public void method4() {
    something;
}

@Scheduled(cron = "${430pm.cron.expression}", zone = "America/New_York") //0 0 6 * * ?
public void stopExecutor() {
    MyClass class = new MyClass();
    Executor executor = new Executor(class);
    executor.stop();
}

共有1个答案

盖嘉庆
2023-03-14

您可以使用@scheduled(cron=“your cron job time”)on方法尝试annonate方法您正在尝试在给定的排定日期/时间运行。

例如。

@Scheduled(cron = " specify cron job here ")
public void run job() { 
      // Code here
}

希望这有帮助!

 类似资料:
  • 我使用的是Spring的@计划和@异步注释。 我的目的 计划一个同步方法——它运行一个for循环,而这个循环将运行一个异步方法,因此循环中的下一个值不需要等到方法完成。 见下面我的代码: 问题: 我的异步方法不会在不同的任务上运行。只有在我将@Async注释也放在我的计划方法上时,它才起作用。但我的计划方法将异步运行到,这不是我想要的。计划的方法需要同步运行,但for循环中被调用的方法需要异步运行

  • 本文向大家介绍SpringBoot执行定时任务@Scheduled的方法,包括了SpringBoot执行定时任务@Scheduled的方法的使用技巧和注意事项,需要的朋友参考一下 在做项目时,需要一个定时任务来接收数据存入数据库,后端再写一个接口来提供该该数据的最新的那一条。 数据保持最新:设计字段sign的值(0,1)来设定是否最新 定时任务插入数据:首先进行更新,将所有为1即新数据设置过期,然

  • Spring计划的cron作业在计划时间内运行了两次。 使用spring 4.2.5版本。 REST API- web.xml如下。 任务条目的spring应用程序上下文xml文件。 春豆如下。

  • 是否允许在一个方法中使用多个@scheduled注释?

  • 我有一个Spring Boot2.1.6.Release应用程序,其中我有一个用 但它似乎没有工作,因为它仍然在等待一个执行结束,然后再开始下一个。 在@scheduled注释中使用cron进行并行执行的正确方法是什么?

  • 我试图在springboot上同时运行多个计划任务,但实际上它们运行队列(一个接一个,不是并行的) 这是我简单的服务: 输出: 但是,它应该是这样的: 我做错了什么? 这是我的配置: