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,以及如何检测容器是否就绪

异常如下图

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)
4

1 回答 1

2

正如此链接中所解释的那样,

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

这意味着Runnable在您的 EJB 中使用不是要走的路。

如果你想以给定的频率运行一些代码,你可以使用Timer Service

如果在另一种方式上您需要在数据库更新完成时运行一些代码,即异步,您必须使用其他技术,如JMS、数据库触发器、持久性上下文共享/传播或实体侦听器,具体取决于您的应用程序的要求。

于 2012-08-16T09:37:42.827 回答