spring-boot - 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();
}
解决方案
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 !
推荐阅读
- flurl - Flurl 不遵守超时设置
- javascript - ^ 节点脚本参数中的字符
- python - 使用 bdist_wheel 后,Python 轮不包含项目,而 __init__.py 位于根目录中
- php - 如何从 PHP 变量动态加载和显示 TWIG 中的翻译?
- java - 获取基于时间的 uuid 最多 100 纳秒
- php - 显示事件发生的特定月份 PHP
- java - 在 Windows 上使用 startup.bat 运行 Tomcat8 时的 JVM 选项
- sql - 如何在 Oracle SQL 中将 ID 与逗号分隔的字符串进行比较?
- c++ - 如何在设计库中找出硬件中的#define 值
- java - 尝试处理 s3 文件时出现 OOM