如果任务抛出RuntimeException/Error,ScheduledExecutorService.scheduleAt *方法是否应该重新计划任务? [英] Should ScheduledExecutorService.scheduleAt* methods re-schedule tasks if the task throws RuntimeException/Error?

查看:122
本文介绍了如果任务抛出RuntimeException/Error,ScheduledExecutorService.scheduleAt *方法是否应该重新计划任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天,我在我的应用程序中实现了一项重要服务,无论如何,该服务都应继续运行.因此,我使用了以下构造:

The other day I was implementing an important service in my application, that should continue to run no matter what. So I used the following construct:

ScheduledExecutorService ses =
Executors.newSingleThreadScheduledExecutor();

//If the thread dies, another will take over
ses.scheduleAtFixedRate(importantPeriodicTask, 1, 1, TimeUnit.NANOSECONDS);

...仅发现当重要的PeriodicTask意外抛出RuntimeException或Error时,ScheduledExecutorService将停止执行此任务(它们将被调度).

...only to find out that when importantPeriodicTask acutually throws a RuntimeException or Error, the ScheduledExecutorService will stop executing this task (they will cease to be scheduled).

这当然是javadoc所说的:

This is of course exactly what the javadoc says:

任务是否执行 遇到异常,随后 处决被禁止.

If any execution of the task encounters an exception, subsequent executions are suppressed.

太可惜了,但是我不明白为什么作者如此实现ScheduledExecutorService.

So shame on me, but I couldn't understand why the authors implemented ScheduledExecutorService like this.

当然,不应捕获RuntimeException或Error,尤其是Error.但是实际上,特别是在RuntimeException的情况下,事实是它们在生产部署中很常见,我觉得几乎总是希望虽然该特定操作应该失败,但应用程序本身不应该因为该孤立的错误而失败.

Granted, a RuntimeException or Error should generally not be caught, especially Error. But in reality, especially in case of RuntimeException, truth is that they are thrown quite common in production deployment, and I feel it is almost always desirable that while that particular operation should fail, the app itself should not fail because of that isolated error.

确实,对一项定期任务的压制不会影响其他种类的定期任务.但是,鉴于大多数定期任务的性质,难道不应该将这些任务视为服务",而不是孤立的任务吗?

It is true that a suppression of one periodic task does not affect other kinds of periodic tasks. But given the nature of most periodic tasks, shouldn't these tasks perceived as a "service", rather than isolated tasks?

换句话说,不仅不是importantPeriodicTask的一个实例失败,而且任务本身会继续被重新安排吗?

In other words, shouldn't only that one instance of importantPeriodicTask fail, and the task itself be continued to be rescheduled?

推荐答案

在我看来,当前行为是合理的. RuntimeExceptions通常是指错误.它们实际上可以出现在任务代码中的任何位置.例如,如果任务是有状态的,则其状态可能会不一致,并且随后的执行将具有意外行为.总的来说,我不喜欢试图从自己的错误中恢复的代码,但这是我的看法.

In my opinion the current behavior is reasonable. RuntimeExceptions usually refer to bugs. They can practically occur anywhere in the task's code. If the task is stateful for example, it may leave its state inconsistent, and subsequent executions will have an unexpected behavior. In general, I don't like code that tries to recover from its own bugs, but that's my opinion.

如果您想更改ScheduledExecutorService的行为,请查看以下通用解决方案:

If you wish to change the behavior of ScheduledExecutorService, take a look at the following generic solution:

http://www.javaspecialists.eu/archive/Issue154.html

这篇关于如果任务抛出RuntimeException/Error,ScheduledExecutorService.scheduleAt *方法是否应该重新计划任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆