首页 > 解决方案 > Spring Boot How to run multiple method with @Scheduled

问题描述

I have a Spring Boot app where I want to have multiple methods to run at different times of the day. The first one runs, but no subsequent method runs. What do I need to do to fix this? Here is my code: @EnableScheduling @Configuration //@ConditionalOnProperty(name = "spring.enable.scheduling") @SpringBootApplication @PropertySources({ @PropertySource(value = "prop.properties", ignoreResourceNotFound = true) }) public class Application { private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); public static MyClass class = new MyClass(); public static void main(String[] args) { ClassLoader classLoader = ClassLoader.getSystemClassLoader(); InputStream resourceAsStream = classLoader.getResourceAsStream("log4j2.properties"); PropertyConfigurator.configure(resourceAsStream);

    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();
}

标签: spring-bootquartz-scheduler

解决方案


You can try annonate method you are trying to run at given scheduled day / time using @Scheduled ( cron = "your cron job time ") on method.

E.g.

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

Hope this helps !


推荐阅读