Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring equivalent of CompletionService?
    text
    copied!<p>In my app I have to process multiple jobs asynchronously from the main application thread and collect the result of each job. I have a plain Java solution that does this using a ExecutorService and a ExecutorCompletionService that collects the job results.</p> <p>Now I would like to convert my code to a Spring solution. The <a href="http://static.springsource.org/spring/docs/3.0.x/reference/scheduling.html" rel="noreferrer">docs</a> show me how the use the ExecutorService and the @Async annotation, but I am not sure how and if I can collect the results of multiple jobs.</p> <p>In other words: I am looking for the Spring equivalent of the CompletionService. Is there such a thing?</p> <p>My current code:</p> <pre><code>class MyService { private static ExecutorService executorService; private static CompletionService&lt;String&gt; taskCompletionService; // static init block static { executorService = Executors.newFixedThreadPool(4); taskCompletionService = new ExecutorCompletionService&lt;String&gt;(executorService); // Create thread that keeps looking for results new Thread(new Runnable() { @Override public void run() { while (true) { try { Future&lt;String&gt; future = taskCompletionService.take(); String s = future.get(); LOG.debug(s); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } } }).start(); } // This method can and will be called multiple times, // so multiple jobs are submitted to the completion service public void solve(List&lt;Long&gt; ids) throws IOException, SolverException { String data = createSolverData(ids); taskCompletionService.submit(new SolverRunner(data, properties)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload