[英]Why is java.util.concurrent.FutureTask not serializable
I am currently using Apache Wicket. 我目前正在使用Apache Wicket。 I have some REST calls which take a few seconds each. 我有一些REST调用,每个调用几秒钟。 Wicket allows ajax calls synchronously only, so I was trying to use Future and Callable. Wicket只允许同步调用ajax,因此我尝试使用Future和Callable。
This is part of my class: 这是我班级的一部分:
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
}
The List<Callable> priceCalls
contains the method calls to the appropriate price calls List<Callable> priceCalls
包含对相应价格调用的方法调用
I receive The object type is not Serializable! 我收到的对象类型不是Serializable! against java.util.concurrent.FutureTask list field 针对java.util.concurrent.FutureTask列表字段
I am assuming I should design this differently. 我假设我应该以不同的方式设计。 Could any provide any thoughts on how this should be done? 可以提出任何关于如何做到这一点的想法吗?
I am using Spring if that helps 如果有帮助我正在使用Spring
FutureTask
不可序列化,因为它依赖于其他资源,如Executor
,可能是Thread
实例, Queue
和OS资源,无法轻松序列化。
Another option would be create your own implementation of Future, which also implements Serializable. 另一种选择是创建自己的Future实现,它也实现了Serializable。
In the page below, the author shows that. 在下面的页面中,作者显示了这一点。 In this case, he used JMS behind the Future implementation. 在这种情况下,他在Future实现后面使用了JMS。
http://www.javacodegeeks.com/2013/02/implementing-custom-future.html http://www.javacodegeeks.com/2013/02/implementing-custom-future.html
I ended up using Spring's Async and AjaxLazyLoadPanel 我最终使用了Spring的Async和AjaxLazyLoadPanel
See more info here: http://apache-wicket.1842946.n4.nabble.com/AjaxLazyLoadPanel-loading-asynchronously-td4664035.html#a4665188 点击此处查看更多信息: http : //apache-wicket.1842946.n4.nabble.com/AjaxLazyLoadPanel-loading-asynchronously-td4664035.html#a4665188
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.