Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8633139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:26:55+00:00 2026-06-12T09:26:55+00:00

In my web application, I have a background service. This service uses Generator class

  • 0

In my web application, I have a background service. This service uses Generator class that contains an Engine class and an ExecutorService configured to use multiple threads and that accepts GeneratorTasks.

@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<String> {
    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);  
    }
}

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.

I was thinking of making the private Engine heavyEngine 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 request scope, but I’m not sure how I should go about it given my design.

Any guidance on how to improve my design would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:26:56+00:00Added an answer on June 12, 2026 at 9:26 am

    First of all abandon ThreadLocal – there is something scary in that class. What you need is just object pooling. It’s not well known feature, but Spring supports this as well:

    <bean id="engineProto" class="Engine" scope="prototype" lazy-init="true"/>
    
    <bean id="engine" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="targetSource">
            <bean class="org.springframework.aop.target.CommonsPoolTargetSource">
                <property name="targetClass" value="Engine"/>
                <property name="targetBeanName" value="engineProto"/>
                <property name="maxSize" value="3"/>
                <property name="maxWait" value="5000"/>
            </bean>
        </property>
    </bean>
    

    Now when you inject engine, you’ll actually receive proxy object (Engine will need an interface) that will delegate all calls to free object in the pool. Pool size is configurable. Of course there is nothing preventing you from using ThreadLocalTargetSource which uses ThreadLocal instead of Commons Pool. Both approaches guarantee exclusive, thread safe access to Engine.

    Finally you can use pooling manually (but the beauty of solution above is that it’s completely transparent) or switch to EJBs, which are pooled by definition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an android application that has a background service which polls a web
I am writing an application that connects to a web service in a background
Background: I have a winform application written in VB.NET that uses a WebService to
Background We have an asp.net 4.0 web application written in C# that calls a
Background - Visual Studio 2010, C#, .Net Framework 4.0 application I have a web
In my web application I have taken a ajax calendar in that i want
I am building a web application and have been told that using object oriented
I have started a new web application and have decided to use jquery as
I will have a client application using a proxy to a WCF Service. This
I have a web service that creates a thread to process some data in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.