0

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

neo
  • 2,461
  • 9
  • 42
  • 67
  • http://stackoverflow.com/questions/14530717/avoid-expunging-timer-on-glassfish this might be usefull in situation where you can not avoid throwing an exception – Aksel Willgert Nov 10 '13 at 21:12

1 Answers1

0

I don't see why the timer would stop, but here is what you can do do prevent calling the method before the map is populated:

@PostConstruct
public void atStartup() {
  myMap = myBean.createData(); 
}

@Startup guarantees that this method will be executed before any other method is called.

Oliver
  • 3,815
  • 8
  • 35
  • 63
Assen Kolov
  • 4,143
  • 2
  • 22
  • 32