@postConstruct和@schedule with cron
在运行该应用程序后,我使用以下代码 - 刺管很长一段时间来触发导出。
@PostConstruct
@Scheduled(cron = "${" + EXPORT_SCHEDULE + "}")
public void exportJob()
{
exportService().exportTask();
}
在更新了Spring-boot 2.6.x之后,根据“循环bean定义”的策略变得更加严格,我再也无法对此方法使用两种注释。一些答案建议将@postConstruct合并到@scheduled中,为 initiaLdelay = 0
,但是crontab和@scheduled的其他属性不兼容。导致以下例外:
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'exportJob': 'initialDelay' not supported for cron triggers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一个解决方案可能是实现A bean。该界面包含
run()
方法,该方法是在应用程序启动之后执行的Another solution may be to implement a CommandLineRunner bean. This interface contains a
run()
method that is executed after the application startup我发现的一个工作解决方案是仅使用@scheduled注释,但也有另一种方法是用@scheduled和
initiaLdelay = 0
注释的方法,它只是代理另一种方法。从理论上讲,cronjob被更频繁地触发,因此我选择了进行服务呼叫。一个人也可以朝相反的方向构建呼叫链。
A working solution I found was to use just the @Scheduled annotation but also have another method be annotated with @Scheduled and
initialDelay=0
which just proxies the other method.In theory, the cronJob is triggered more often, hence I chose it to do the service call. One could structure the call chain in the opposite direction as well.