Java执行具有多次重试和超时的任务 [英] Java execute task with a number of retries and a timeout

查看:64
本文介绍了Java执行具有多次重试和超时的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一种在最长时间内执行给定任务的方法.如果在那段时间内未能完成,则应在放弃之前重试多次.它也应该在每次尝试之间等待几秒钟.这是我的想法,我想对我的方法提出一些批评.他们使用 ScheduledExecutorService 是一种更简单的方法,还是我的方法就足够了?

I'm trying to create a method that executes a given task in a maximum amount of time. If it fails to finish in that time, it should be retried a number of times before giving up. It should also wait a number of seconds between each try. Here's what I've come up with and I'd like some critiques on my approach. Is their a simpler way to do this using the ScheduledExecutorService or is my way of doing this suffice?

public static <T> T execute(Callable<T> task, int tries, int waitTimeSeconds, int timeout) 
    throws InterruptedException, TimeoutException, Exception {

    Exception lastThrown = null;
    for (int i = 0; i < tries; i++) {
        try {
            final Future<T> future = new FutureTask<T>(task);
            return future.get(timeout, TimeUnit.SECONDS);
        } catch (TimeoutException ex) {
            lastThrown = ex;
        } catch (ExecutionException ex) {
            lastThrown = (Exception) ex.getCause();
        }
        Thread.sleep(TimeUnit.SECONDS.toMillis(waitTimeSeconds));
    }
    if (lastThrown == null) {
        lastThrown = new TimeoutException("Reached max tries without being caused by some exception. " + task.getClass());
    }
    throw lastThrown;
}

推荐答案

我认为,但这是我的观点,如果您正在调度网络相关任务,您不应该重试,而是最终并行运行它们.我稍后会描述另一种方法.

I think, but it's my opinion, that if you are scheduling network related tasks, you should not retry but eventually run them in parallel. I describe this other approach later.

关于您的代码,您应该将任务传递给执行程序,或将 FutureTask 传递给线程.它不会产生线程或自行执行.如果你有一个 executor(参见 ExecutorService),你甚至不需要 FutureTask,你可以简单地安排它并获得一个可调用的.

Regarding your code, you should pass the task to an executor, or the FutureTask to a thread. It will not spawn a thread or execute by itself. If you have an executor (see ExecutorService), you don't even need a FutureTask, you can simply schedule it and obtain a callable.

所以,假设你有一个 ExecutorService,你可以调用:

So, given that you have an ExecutorService, you can call :

Future<T> future = yourExecutor.submit(task);

Future.get(timeout) 将等待该超时并最终以 TimeoutException 返回,即使任务根本没有开始,例如,如果 Executor 已经忙于做其他工作并且找不到空闲线程.因此,您最终可能会尝试 5 次并等待几秒钟,而从未给任务机会运行.这可能是也可能不是您所期望的,但通常不是.也许您应该等待它启动,然后再给它超时.

Future.get(timeout) will wait for that timeout and eventually return with TimeoutException even if the task has never started at all, for example if the Executor is already busy doing other work and cannot find a free thread. So, you could end up trying 5 times and waiting for seconds without ever giving the task a chance to run. This may or may not be what you expect, but usually it is not. Maybe you should wait for it to start before giving it a timeout.

此外,您应该明确取消 Future,即使它抛出 TimeoutException,否则它可能会继续运行,因为文档和代码都没有说明它会在带有超时的 get 失败时停止.

Also, you should explicitly cancel the Future even if it throws TimeoutException, otherwise it may keep running, since nor documentation nor code says it will stop when a get with timeout fails.

即使您取消它,除非 Callable 已正确编写",否则它可能会继续运行一段时间.在这部分代码中,您无能为力,请记住,没有一个线程可以真正停止"另一个线程在 Java 中正在执行的操作,这是有充分理由的.

Even if you cancel it, unless the Callable has been "properly written", it could keep running for some time. Nothing you can do it about it in this part of code, just keep in mind that no thread can "really stop" what another thread is doing in Java, and for good reasons.

但是我认为您的任务主要与网络相关,因此它应该对线程中断做出正确反应.

However I suppose your tasks will mostly be network related, so it should react correctly to a thread interruption.

我通常使用不同的策略是这样的情况:

I usually use a different strategy is situations like this:

  1. 我会写 public static T execute(Callable task, int maxTries, int timeout),所以任务、最大尝试次数(可能是 1)、最大总超时(我希望在最多 10 秒内得到答案,无论你尝试了多少次,10 秒或什么都不做")
  2. 我开始生成任务,将其交给执行程序,然后调用 future.get(timeout/tries)
  3. 如果我收到结果,请将其返回.如果我收到异常,会再试一次(见下文)
  4. 如果超时,我不会取消未来,而是将其保存在列表中.
  5. 我会检查是否经过了太多时间,或重试次数是否过多.在这种情况下,我取消列表中的所有期货并抛出异常,返回 null,无论如何
  6. 否则,我会循环,再次安排任务(与第一个任务并行).
  7. 见第 2 点
  8. 如果我没有收到结果,我会检查列表中的未来,也许之前生成的任务之一设法做到了.

假设您的任务可以执行多次(我认为是这样,否则无法重试),对于网络问题,我发现此解决方案效果更好.

Assuming your tasks can be executed more than once (as I suppose they are, otherwise no way to retry), for network stuff I found this solution to work better.

假设您的网络实际上非常繁忙,您请求网络连接,每次重试 2 秒,重试 20 次.由于您的网络繁忙,20 次重试都无法在 2 秒内获得连接.但是,持续 40 秒的单次执行可能会设法连接和接收数据.就像一个人在网速很慢的网页上强行按f5,没有任何好处,因为每次浏览器都要从头开始.

Suppose your network is actually very busy, you ask for a network connection, giving 20 retries 2 seconds each. Since your network is busy, none of the 20 retries manages to get the connection in 2 seconds. However, a single execution lasting 40 seconds may manage to connect and receive data. It's like a person pressing f5 compulsively on a page when the net is slow, it will not do any good, since every time the browser has to start from the beginning.

相反,我保持各种期货运行,第一个设法获取数据的期货将返回结果,其他期货将被停止.如果第一个挂了,第二个会工作,或者第三个可能.

Instead, I keep the various futures running, the first one that manages to get the data will return a result and the others will be stopped. If the first one hangs, the second one will work, or the third one maybe.

与浏览器相比,就像打开另一个选项卡并在不关闭第一个选项卡的情况下重新尝试加载页面.如果网络很慢,第二个将需要一些时间,但不会停止第一个,最终会正确加载.如果第一个标签被挂起,第二个将快速加载.无论哪个先加载,我们都可以关闭另一个标签.

Comparing with a browser, is like opening another tab and retrying to load the page there without closing the first one. If the net is slow, the second one will take some time, but not stopping the first one, which will eventually load properly. If instead the first tab was hung, the second one will load rapidly. Whichever loads first, we can close the other tab.

这篇关于Java执行具有多次重试和超时的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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