Spring Boot如何使用@Scheduled运行多种方法

问题描述 投票:0回答:1

我有一个Spring Boot应用程序,我想在一天中的不同时间运行多种方法。第一个运行,但没有后续方法运行。我需要怎么做才能解决此问题?这是我的代码:

@EnableScheduling
@Configuration
@EnableAsync
//@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-boot quartz-scheduler
1个回答
0
投票

您可以在方法上使用@Scheduled(cron =“您的cron工作时间”来尝试在给定的计划的日期/时间运行的匿名方法。

例如

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

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.