[英]Java 5: java.util.concurrent.FutureTask - Semantics of cancel() and done()
[英]Why is java.util.concurrent.FutureTask not serializable
我目前正在使用Apache Wicket。 我有一些REST调用,每个调用几秒钟。 Wicket只允许同步调用ajax,因此我尝试使用Future和Callable。
这是我班级的一部分:
public abstract class GetPrices extends AbstractAjaxTimerBehavior {
private static final long serialVersionUID = 1L;
private List<Future<List<Result>>> list;
public GetPrices(Duration updateInterval, List<Callable> priceCalls) {
super(updateInterval);
list = new ArrayList<Future<List<Result>>>();
ExecutorService executor = Executors.newFixedThreadPool(priceCalls.size());
for(Callable callable : priceCalls) {
list.add(executor.submit(callable));
}
executor.shutdown();
}
@Override
protected void onTimer(AjaxRequestTarget target) {
while(list.hasNext()) {
Future<List<Result>> future = listIterator.next();
if(future.isDone()) {
List<Result> data = future.get();
//Process data
}
}
}
//Error handling etc
}
List<Callable> priceCalls
包含对相应价格调用的方法调用
我收到的对象类型不是Serializable! 针对java.util.concurrent.FutureTask列表字段
我假设我应该以不同的方式设计。 可以提出任何关于如何做到这一点的想法吗?
如果有帮助我正在使用Spring
FutureTask
不可序列化,因为它依赖于其他资源,如Executor
,可能是Thread
实例, Queue
和OS资源,无法轻松序列化。
另一种选择是创建自己的Future实现,它也实现了Serializable。
在下面的页面中,作者显示了这一点。 在这种情况下,他在Future实现后面使用了JMS。
http://www.javacodegeeks.com/2013/02/implementing-custom-future.html
我最终使用了Spring的Async和AjaxLazyLoadPanel
点击此处查看更多信息: http : //apache-wicket.1842946.n4.nabble.com/AjaxLazyLoadPanel-loading-asynchronously-td4664035.html#a4665188
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.