我知道spring有自己的ListenableFuture
实现,但是它有类似的Futures.transform
实现吗?我试图找到一种使用 spring 提供的类将操作链接在一起的方法,但我只看到使用 Guava 的示例。
最佳答案
Springs 实现似乎是 ListenableFutureAdapter
这是一个例子:
@Service
public class HeavyLiftingServiceImpl implements IHeavyLiftingService {
public ListenableFuture<String> heavyLifting() {
AsyncRestTemplate asycTemp = new AsyncRestTemplate();
ListenableFuture<String> future = asycTemp.execute(url, method, requestCallback, responseExtractor, urlVariable);
ListenableFutureAdapter<String, String> chainedFuture;
chainedFuture = new ListenableFutureAdapter<String, String>(future) {
@Override
protected String adapt(String adapteeResult)
throws ExecutionException {
String parsedString = parse(adapteeResult);
return adapteeResult;
}
};
return chainedFuture;
}
}
我建议使用 Guava 的可听 future 实现。它更具可读性,并且有更多关于将它们链接在一起的文档
关于java - Guavas Futures.transform 的 Spring 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35143511/