gpt4 book ai didi

java - Guava ListenableFuture 找不到源对象失败

转载 作者:太空宇宙 更新时间:2023-11-04 14:43:21 26 4
gpt4 key购买 nike

当使用 Guava 的 ListenableFuture 时,如何知道失败的任务。示例

FutureCallback<AsyncHTTPResponse> future = new FutureCallback<AsyncHTTPResponse>() {
@Override
public void onFailure(Throwable arg0)
{
// In this block I do not know who succeeded. We could create custom
// exceptions and put the object in there, but what if we are not
// directly causing the exception. Seems like there should be an easy
// way to get the callable task that failed to produce a success result
}

@Override
public void onSuccess(AsyncHTTPResponse arg0)
{
// In this block I know the original object that succeded
}
};

因此,在这个示例中,我找不到给定 URL 来尝试通过 http 获取一些数据的可调用函数。因此失败者不知道哪个 URL 失败了。当然有一些解决方法,例如在表中记录 futures 和 url,但是有没有更简洁的方法来使用框架来为您做到这一点?

最佳答案

我通常看到的解决方案是为每个请求创建一个新的回调实例:

final URL url = ...;
ListenableFuture<AsyncHTTPResponse> future = makeRequest(url);
addCallback(future, new FutureCallback<AsyncHTTPResponse>() {
@Override
public void onFailure(Throwable e)
{
// do something with url
}

...
});

关于java - Guava ListenableFuture 找不到源对象失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24717846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com