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

  • SEARCH
  • Home
  • 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 8392107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:26:07+00:00 2026-06-09T19:26:07+00:00

Hi a long running thread that starts up in Postconstruct and updates database periodically.

  • 0

Hi a long running thread that starts up in Postconstruct and updates database periodically.

Looks like this

    public void postconstruct() {
    Runnable runnable;
    runnable = new Runnable() {
        @Override
        public void run() {
            int interval = poissonRandomNumber(15);
            while (true) {
                try {
                    Map<Account, AccountUpdate> recentUpdates =
                            simulator.getRecentUpdates(tickDAO, securityDAO);
                    for (Map.Entry<Account, AccountUpdate> entry : recentUpdates.entrySet()) {
                        Account account = entry.getKey();
                        //                            String realised = getAccountReturn(parts, account);
                        Account accountByName = accountDAO.findAccountByName(account.getName());
                        if( accountByName == null  )
                        {
                            account.setAccountId(null);
                            accountByName = accountDAO.create(account);
                        }
                        int realised = new Random().nextInt(50);
                        boolean nextBoolean = new Random().nextBoolean();
                        realised = nextBoolean == true ? realised : -realised;
                        AccountUpdate accountUpdate = entry.getValue();
                        accountUpdate.setAccountId(accountByName);
                        accountUpdate.setDateCreated(new Date());
                        accountUpdate.setUnRealisedPL(new BigDecimal(realised));
                        accountUpdateDAO.create(accountUpdate);
                    }
                    Thread.sleep(interval);
                } catch (Exception ex) {
                    Logger.getLogger(ApplicationManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    };
    Thread tickThread = new Thread(runnable);
    tickThread.start();

Whenever I startup the application i get lots of exceptions because it looks like the Container is not ready

I need to know how best to use DAOs and also how to detect if the conytainer is Ready

The exception is as shown below

SEVERE: java.lang.NullPointerException
at com.sun.ejb.containers.util.pool.NonBlockingPool.returnObject(NonBlockingPool.java:285)
at com.sun.ejb.containers.StatelessSessionContainer.releaseContext(StatelessSessionContainer.java:602)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2055)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89)
at $Proxy270.create(Unknown Source)
at ucl.atrade.rnpvms.server.services.ApplicationManager$1.run(ApplicationManager.java:126)
at java.lang.Thread.run(Thread.java:722)

SEVERE: javax.ejb.EJBException: Attempt to invoke when container is in Undeployed
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1999)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89)
    at $Proxy265.findSecurityBySymbol(Unknown Source)
    at ucl.atrade.rnpvms.server.datafeed.AccountUpdateSimulator.getRecentUpdates(AccountUpdateSimulator.java:74)
    at ucl.atrade.rnpvms.server.services.ApplicationManager$1.run(ApplicationManager.java:108)
    at java.lang.Thread.run(Thread.java:722)
  • 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-09T19:26:08+00:00Added an answer on June 9, 2026 at 7:26 pm

    As well explained in this link,

    The EJB specification assigns to the EJB container the responsibility for managing threads. Allowing enterprise bean instances to create and manage threads would interfere with the container’s ability to control its components’ lifecycle.

    This means that using Runnable in your EJB is not the way to go.

    If you want to run some code with a given frequency, you can use a Timer Service.

    If on the other way you need to run some code when an update to the db is done, i.e. asynchronously, you have to use other techniques like JMS, or database triggers, or Persistence Context sharing / propagation, or Entity Listeners, depending on your application’s requirements.

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

Sidebar

Related Questions

I have a console application that starts up, hosts a bunch of services (long-running
I'd like to start a long running task on the Backend instance. This backend
Let's say I have this blob of code that's made to be one long-running
I'm trying to execute a long-running piece of code in a background thread and
Here is the situation: You have one long-running calculation running in a background thread.
I have a long running process that is called via a Nancy Module Get
I have a long-running multithreaded program, and I'd like to occasionally like to call
I occasionally have some long running AJAX requests in my Wicket application. When this
I'd like to prevent multiple instances of the same long-running python command-line script from
We have an ASP.NET application that queues some of its long-running operations (generating reports,

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.