I understand that if the timer encounter an exception, it will stop running. But here's my code:
@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
}
...
}
so the timer fires every 1 minute, and call myBean to get data from database and populate the hashmap.
Now in a different class, the client makes restFul web service call to get the hashmap, here's code:
@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.
}
what happens is that sometimes, when it throw the exception, it will also cause the timer to stop working again. Why? the timer itselt hasn't encounter any exception.
I realized that the potential problem is that when the user try to get the hashmap, the timer may be in the middle of populating the hashmap. what should I do to prevent this? like blocking the web service call until it's ready?
thanks