让我们假设我们有可能长期运行的任务:
public class LongRunningTask {
public ReturnType doSomething() {
...
}
}
我们希望同时运行其中的许多任务。
所以,我可以调用它:
public class LongRunningCallable implements Callable<LongRunningTask> {
private final LongRunningTask task;
...
public ReturnType call() {
return task.doSomething();
}
...
}
现在,由于这可能真的持续很长时间,我想要限制它只运行一定数量。所以,我可能会这样做:
public class InterruptibleCallable<T> implements Callable<T> {
protected final long timeout;
protected final TimeUnit timeUnit;
protected final Callable<T> callable;
public InterruptibleCallable(long timeout, TimeUnit timeUnit, Callable<T> callable) {
this.timeout = timeout;
this.timeUnit = timeUnit;
this.callable = callable;
}
@Override
public T call() throws Exception {
ExecutorService executorService = Executors.newSingleThreadExecutor();
T result = null;
try {
result = executorService.submit(callable).get(timeout, timeUnit);
} catch (InterruptedException e) {
LOGGER.error("Callable: " + callable.toString() + " was interrupted");
throw e;
} catch (ExecutionException e) {
throw e;
} catch (TimeoutException e) {
LOGGER.warn("Callable: " + callable.toString() + " timed out after " + timeout + " " + timeUnit);
throw e;
} finally {
executorService.shutdown();
}
return result;
}
}
这没关系,但现在我还想把它包起来,如果它遇到异常,它可以自行重试(有延迟):
public class RetryableCallable<T> implements Callable<T> {
private final long delay;
private final TimeUnit timeUnit;
private final Callable<T> callable;
@Override
public T call() throws Exception {
T result = null;
try {
ExecutorService executorService = Executors.newSingleThreadExecutor();
result = executorService.submit(this.callable).get();
} catch (Exception e) {
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
result = executorService.schedule(this.callable, delay, timeUnit).get();
}
return result;
}
}
现在,我的问题:
是否已经有一个提供此功能(或超集)的库?
这是一个很好的设计,尤其是创建另一个执行器并在每个包装器中提交可调用对象,为什么?
从设计模式视图和性能视图中解决此问题的最佳方法是什么?
谢谢:D
答案 0 :(得分:1)
围绕RetryableCallable
的{{1}}换行InterruptibleCallable
并在每次LongRunningTask
执行时创建两个额外的执行程序是不好的。
当RetryableCallable
捕获RetryableCallable
时,它通常不会再次运行相同的任务。这有点令人困惑,因为如果任务被超时“杀死”,你为什么要再次运行它?
另外为什么你需要在这里创建另一个执行者?保持简单
TimeoutException
它不应该使用超时和延迟,它是public class RetryableCallable<T> implements Callable<T> {
private int retries = 3;
@Override
public T call() throws Exception {
while (retries-- > 0) {
try {
return callable.call();
} catch (InterruptedException e) {
LOGGER.error("Callable: " + callable.toString() + " was interrupted");
throw e;
} catch (TimeoutException e) {
LOGGER.warn("Callable: " + callable.toString() + " timed out after " + timeout + " " + timeUnit);
throw e;
} catch (Exception e) {
LOGGER.warn("Callable: " + callable.toString() + " failed");
}
}
throw new IllegalStateException();//or return null
}
}
而不是RetryableCallable
如果你想限制任务的执行时间,我至少看到4种好方法:
RetryableCallableWithDelayAndTimeoutAndSomethingElse
get(time, timeunit)
函数内的执行时间,例如通过“定期”检查,我们还有更多时间。call()
并具有TimeLimitedCallable
功能的Callable
。审计员将控制实施int getTimeLimit()
的所有正在运行的任务的时间范围。