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 8919737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:06:02+00:00 2026-06-15T06:06:02+00:00

We have a servlet as follows: public class CacheRefresher extends HttpServlet { private static

  • 0

We have a servlet as follows:

public class CacheRefresher extends HttpServlet {
    private static final long START_TIMEOUT = 120*1000;

    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        new Thread(new Worker()).start();

    }

    private class Worker implements Runnable {
        public Worker() { }
        public void run() {   
            try {
                Thread.sleep(START_TIMEOUT);
            } catch (InterruptedException e) {
            }

            while(true) {
                MyService myService = null;
                try {
                    myService = ServiceFactory.getInstance().getMyService();
                    myService.doSomething();
                } catch (Exception ex){
                    ex.printStackTrace();
                }finally {
                    ServiceFactory.getInstance().releaseMyService(myService);
                }

                try {
                    Thread.sleep(timeout);
                } catch (InterruptedException e) {
                }
            }
        }

    }
} 

Its purpose is to periodically call a service. There will only be a single instance of this Servlet, which will be created on server startup. MyService is an EJB.

How bad is this? I know spawning threads from EJBs is not allowed, but what about the other way around? What will happen on server shutdown?

  • 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-15T06:06:03+00:00Added an answer on June 15, 2026 at 6:06 am

    Conceptualy i dont see a problem with invoking ejb methods from multiple threads (even if you created the threads yourself). For the ejb-container that will be just another client among others.

    From your example it looks like the soul purpose of you servlet is to start a bunch of timers. If you can use ejb 3.1, there is java ee standard way to do that.

    First a Singleton ejb that launches the timers on startup

    import javax.annotation.PostConstruct;
    import javax.ejb.EJB;
    import javax.ejb.Singleton;
    import javax.ejb.Startup;
    
    
    @Singleton
    @Startup
    public class SingletonBean {
    
        @EJB
        LabBean labBean;
    
        @PostConstruct
        public void init() {
            long interval = 4000;
            long initialExpiration = 2000;
            labBean.startTimer(initialExpiration, interval, "MyTimer");
        }
    }
    

    Then a SLSB that handles the timeout:

    import javax.annotation.Resource;
    import javax.ejb.Stateless;
    import javax.ejb.Timeout;
    import javax.ejb.Timer;
    import javax.ejb.TimerConfig;
    import javax.ejb.TimerService;
    
    
    @Stateless
    public class LabBean {
        @Resource
        protected TimerService timerService;
    
        @Timeout
        public void timeoutHandler(Timer timer) {
            String name = timer.getInfo().toString();
            System.out.println("Timer name=" + name);
        }
    
        public void stopTimer(String name) {
            for (Object o : this.timerService.getTimers())
                if (((Timer) o).getInfo().toString().startsWith(name)){
                    ((Timer)o).cancel();
                }       
        }
    
        public void startTimer(long initialExpiration, long interval, String name){
            stopTimer(name);        
            TimerConfig config = new TimerConfig();
            config.setInfo(name);
            config.setPersistent(false);
            timerService.createIntervalTimer(initialExpiration, interval, config);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following servlet: public class MyServlet extends HttpServlet { private static final
I have a Custom Tag Handler class: public class ProfileTag extends RequestContextAwareTag { @Override
I am new to jsp and servlet. My scenario is as follows I have
I have a servlet/tomcat server written in java. I have a mysql class that
I have a Servlet class I made to handle functions I don't want to
I am developing a webapplication based on JSP. I have a servlet class :
I have a servlet that does some business login and then redirects to a
I have a servlet running on GAE and I have a cron job that
We have a servlet which occupies more virtual memory on the server due to
We have a servlet which occupies more virtual memory on the server as it

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.