EJB 3.1 StartUp singleton postconstruct方法中长时间运行的Thread中的异常

时间:2012-08-16 08:59:30

标签: java glassfish-3 ejb-3.1

一个长时间运行的线程,它在Postconstruct中启动并定期更新数据库。

看起来像这样

    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();

每当我启动应用程序时,我都会遇到很多异常,因为它看起来似乎没有准备好

我需要知道如何最好地使用DAO以及如何检测conytainer是否准备就绪

例外情况如下所示

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 个答案:

答案 0 :(得分:2)

正如本link中所述,

  

EJB规范为EJB容器分配管理线程的责任。允许企业bean实例创建和管理线程会干扰容器控制其组件生命周期的能力。

这意味着在EJB中使用Runnable是不可取的。

如果要运行某个频率的代码,可以使用Timer Service

如果在另一方面,在完成对db的更新(即异步)时需要运行一些代码,则必须使用其他技术,如JMS,或数据库触发器,或持久性上下文共享/传播,或Entity Listeners,具体取决于您的申请要求。