0
votes

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();
}
1
Put the scheduled methods in a separate @Configuration class.Amit Phaltankar
Sorry. That doesn't work. Do I put each in their separate class or all together in one class? I have them all in one class and only the first one is called.Peter

1 Answers

1
votes

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 !