问题
I am currently using Apache Wicket. I have some REST calls which take a few seconds each. Wicket allows ajax calls synchronously only, so I was trying to use Future and 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
I receive The object type is not Serializable! against java.util.concurrent.FutureTask list field
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
回答1:
FutureTask
isn't serializable because it depends on other resources like an Executor
, probably a Thread
instance, a Queue
and OS resources which can't be easily serialized.
回答2:
Another option would be create your own implementation of Future, which also implements Serializable.
In the page below, the author shows that. In this case, he used JMS behind the Future implementation.
http://www.javacodegeeks.com/2013/02/implementing-custom-future.html
回答3:
I ended up using Spring's Async and AjaxLazyLoadPanel
See more info here: http://apache-wicket.1842946.n4.nabble.com/AjaxLazyLoadPanel-loading-asynchronously-td4664035.html#a4665188
来源:https://stackoverflow.com/questions/22375764/why-is-java-util-concurrent-futuretask-not-serializable