Spring 引导如何使用@Scheduled 运行 多个方法
Spring Boot How to run multiple method with @Scheduled
我有一个 Spring 启动应用程序,我想在一天中的不同时间使用多种方法 运行。第一个 运行s,但没有后续方法 运行s。我需要做什么来解决这个问题?这是我的代码:
@EnableScheduling
@配置
//@ConditionalOnProperty(名称="spring.enable.scheduling")
@SpringBootApplication
@PropertySources({
@PropertySource(value = "prop.properties", ignoreResourceNotFound = true)
})
public class 申请{
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();
}
您可以在给定的预定日期/时间使用方法上的@Scheduled (cron = "your cron job time ") 尝试对您尝试运行 的方法进行注解。
例如
@Scheduled(cron = " specify cron job here ")
public void run job() {
// Code here
}
希望对您有所帮助!
我有一个 Spring 启动应用程序,我想在一天中的不同时间使用多种方法 运行。第一个 运行s,但没有后续方法 运行s。我需要做什么来解决这个问题?这是我的代码: @EnableScheduling @配置 //@ConditionalOnProperty(名称="spring.enable.scheduling") @SpringBootApplication @PropertySources({ @PropertySource(value = "prop.properties", ignoreResourceNotFound = true) }) public class 申请{ 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();
}
您可以在给定的预定日期/时间使用方法上的@Scheduled (cron = "your cron job time ") 尝试对您尝试运行 的方法进行注解。
例如
@Scheduled(cron = " specify cron job here ")
public void run job() {
// Code here
}
希望对您有所帮助!