我想通过外部配置文件禁用 spring 调度。我已经设置了配置文件,并且下面示例中的任务会输出以下内容。
INFO MainTaskScheduler:36 - 计划任务:更新已转换的预订为 false。
所以我大部分时间都在那里。
我希望实现的是不必在每个任务方法中放入逻辑来确定是否已启用调度属性。
我的配置文件中有这样的内容(这不是有效的代码)
@EnableScheduling(${enable.scheduling})
我的工作片段
应用配置
@Configuration
@EnableTransactionManagement
@EnableScheduling
@ComponentScan( /*etc*/})
public class AppConfiguration {
}
MainTaskScheduler
@Component
public class MainTaskScheduler {
private Logger log = LoggerFactory.getLogger(getClass());
@Value("${enable.scheduling}")
private Boolean enableScheduling;
@Scheduled(fixedRate=300) // every 5 minutes -- check if any existing quotes have been converted to bookings
public void updateConvertedBookings() {
log.info("scheduled task: Update converted bookings "+enableScheduling);
// logic for class here
}
}
application.properties
enable.scheduling=false
最佳答案
如果您使用的是 Spring Boot,可以使用 @ConditionalOnExpression
注解来启用或禁用调度:
@ConditionalOnExpression("'${enable.scheduling}'=='true'")
关于java - 使用属性文件禁用调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29254932/