0
我知道如果定時器遇到異常,它將停止運行。但這裏是我的代碼:從其他方法拋出異常後EJB定時器停止
@Startup
@Singleton
public class TimerBean
{
private HashMap myMap;
@EJB
private MyBean myBean;
@Schedule (minute="*/1" ....)
public void myTimer()
{
myMap.clear();
myMap = myBean.createData(); //this takes a few seconds to finish
}
...
}
所以計時器觸發每1分鐘,並呼籲爲myBean從數據庫中獲取數據並填充HashMap中。
在不同的類現在,客戶端發出RESTful Web服務調用來獲取HashMap中,這裏的代碼:
@EJB
private TimerBean timerBean;
@GET
@Path("query")
@Produces(MediaType.APPLICATION_JSON)
public MyObject getData()
{
timerBean.getMyMap(); //call timerBean to get the hashmap
//in case the hashmap returned is empty, meaning it's not ready yet
//(it's still in the process of populating)
//throw an WebApplicationException
//so that from the user's end, I can show a different web page
//and ask the user to wait.
}
什麼情況是,有時,當它拋出異常,也會導致計時器再次停止工作。爲什麼?計時器itselt沒有遇到任何異常。
我意識到潛在的問題是,當用戶嘗試獲取散列映射時,計時器可能正在填充散列映射。我該怎麼做才能防止這種情況?像阻止Web服務調用,直到它準備好了?
感謝
http://stackoverflow.com/questions/14530717/avoid-expunging-timer-on-glassfish這可能是有用的情況下,你不能避免拋出異常 –