Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking Spring beans behave like ThreadLocal instances for an ExecutorService
    primarykey
    data
    text
    <p>In my web application, I have a background service. This service uses Generator class that contains an Engine class and an <code>ExecutorService</code> configured to use multiple threads and that accepts GeneratorTasks.</p> <pre><code>@Component public class Generator { @Autowired private Engine heavyEngine; private ExecutorService exec = Executors.newFixedThreadPool(3); //I actually pass the singleton instance Generator class into the task. public void submitTask(TaskModel model, TaskCallback callback) { this.exec.submit(new GeneratorTask(model, this, callback)); } } @Component public class Engine { public Engine() { //time-consuming initialization code here } } public class GeneratorTask implements Callable&lt;String&gt; { public GeneratorTask(TaskModel m, Generator g, ReceiptCallback c) { this.m = m; this.generator = g; this.c = c; } public String call() throws Exception { //This actually calls the Engine class of the generator. //Maybe I should have passed the Engine itself? this.generator.runEngine(c); } } </code></pre> <p>The Engine class takes a long time to initialize so I ideally want to initialize it only once per thread. I can't just make it a singleton instance because the instance can't be shared across multiple threads (it relies on sequential processing). It's perfectly fine to reuse the instance though, after a processing task has completed.</p> <p>I was thinking of making the <code>private Engine heavyEngine</code> variable a ThreadLocal variable. However, I'm also new to Spring so I was wondering if there might be another way to inject ThreadLocal variables using Spring annotations. I've looked at scoping the bean to <code>request</code> scope, but I'm not sure how I should go about it given my design.</p> <p>Any guidance on how to improve my design would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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