private final ExecutorService executorParsers = Executors.newFixedThreadPool(10);
public void parse(List<MyObjInt> objs) {
//... bunch of elided stuff ....
CompletionService<AsupParseObj> parserService = new ExecutorCompletionService<AsupParseObj>(executorParsers);
for (final AsupStoreObj obj : objs) {
parserService.submit(new ParseThread(obj));
}
}
I would like to DI the “ParseThread” but surely there has to be a better way to do this than burying a call to getBean on a prototype scoped bean and as I am new to Spring I figured I would ask…
Here is the full configuration using
lookup-method(see 3.4.6.1 Lookup method injection):And the Java code:
Unfortunately you cannot pass any parameters to
lookup-method(see SPR-7431 and my article Creating prototype Spring beans on demand using lookup-method), hence the need for artificialsetObject().If you don’t like
abstractmethods/classes, lookup method can be non-abstract no-op method or (better) the default implementation can throw an exception. Spring will override the implementation at runtime, effectively callinggetBean()for you.Bonus: I translated
Executor/CompletionServiceto Spring managed beans as well. Note that Spring supports these out-of-the-box: Task Execution and Scheduling.