简体   繁体   中英

Why is java.util.concurrent.FutureTask not serializable

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

FutureTask不可序列化,因为它依赖于其他资源,如Executor ,可能是Thread实例, Queue和OS资源,无法轻松序列化。

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM