本文介绍了如何检查@Schedule批注(Spring 3.1)cron作业是否正在运行.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个cron作业,该作业连接到FTP服务器并传输文件,这是一个非常简单的功能,它使用spring @Schedule批注(带有cron表达式作为参数)进行配置.它运行了好几个月,然后突然停止,出现了connectException.可能是FTP服务器已关闭或发生了某些事情,导致cron线程停止了.我查看(google)的原因是什么,但没有得到任何帮助(日志中也没什么-只是异常名称).这可能只是一回事了:)

In my application I have one cron job which connects to a FTP server and transfer files, a very simple functionality and it is configured using spring @Schedule annotation with cron expression as a parameter.It was running fine for few months and then suddenly it stopped, got the connectException.May be the FTP server was down or something happened which causes the cron thread to stop.I looked (google) for the reasons but didnt get any ( Nothing much in the logs also - Just the exception name ).It may be a one time thing :)

我的问题是,我可以对@Schedule cron作业进行一些检查或监视以了解其是否正在运行吗?

my question is that can I put some check or watcher on the @Schedule cron job to know whether it is running or not ?

对不起我的不好解释/英语

Sorry for my bad explanation/english

谢谢

推荐答案

基本上,您不能.

当您使用 @Scheduled 时,Spring使用 ScheduledAnnotationBeanPostProcessor 来注册您指定的任务(带注释的方法).它使用 ScheduledTaskRegistrar 注册它们. ScheduledAnnotationBeanPostProcessor ApplicationListener< ContextRefreshEvent> .当它从 ApplicationContext 接收到 ContextRefreshEvent 时,它将调度在 ScheduledTaskRegistrar 中注册的任务.

When you use @Scheduled, Spring uses a ScheduledAnnotationBeanPostProcessor to register the tasks you specify (annotated methods). It registers them with a ScheduledTaskRegistrar. The ScheduledAnnotationBeanPostProcessor is an ApplicationListener<ContextRefreshEvent>. When it receives the ContextRefreshEvent from the ApplicationContext, it schedules the tasks registered in the ScheduledTaskRegistrar.

在此步骤中,这些任务是通过 TaskScheduler 安排的,该任务通常包装 ScheduledExecutorService .如果发生异常未被提交的任务捕获,然后将该任务从 ScheduledExecutorService 队列中删除.

During this step, these tasks are scheduled with a TaskScheduler which typically wraps a ScheduledExecutorService. If an exception is uncaught in a submitted task, then the task is removed from the ScheduledExecutorService queue.

TaskScheduler 类不提供公共API来检索计划的任务,即. ScheduledFuture 对象.因此,您无法使用它来确定您的任务是否正在运行.

The TaskScheduler class does not provide a public API to retrieve the scheduled tasks, ie. the ScheduledFuture objects. So you can't use it to find out if your tasks are running or not.

您可能不应该这样做.开发您的任务和您的 @Scheduled 方法,以能够承受所引发的异常.有时候,这显然是不可能的.例如,由于网络错误,您可能必须重新启动应用程序.在不了解您的应用程序的情况下,我想说更多日志记录是您的最佳选择.

And you probably shouldn't. Develop your tasks, your @Scheduled methods, to be able to withstand an exception being thrown. Some times, obviously, that's not possible. With a network error, for example, you would probably have to restart your application. Without knowing anything else about your application, I would say more logging is your best bet.

这篇关于如何检查@Schedule批注(Spring 3.1)cron作业是否正在运行.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:01