嗨,在 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)