Although there are some articles talking about EJB 3 timers or job schedulers , we were not able to find any detailed instructions on how to make EJB 3 timer work in a weblogic cluster. In this article we will go through a sample project to show how EJB 3 timers are used in a weblogic cluster. The sample project will create two recurring timers, the first recurring timer will periodically print out some simple information, the second recurring timer will create a couple of one-timer timers, each of which will print out some information. In this article, we will show you how to use weblogic adminconsole to configure the cluster, how the application uses the related configuration from the console and how to invoke timers, also explain what problems we faced and how we solved them.
Environment:
web logic server 10.3.2, oracle database 11gR1, Eclipse Helios
Code:
Timer1SessionBeanLocal: local interface for creating timer
@Local publicinterface Timer1SessionBeanLocal { publicvoid createTimer();
}
Timer1SessionBean: a recurring timer that prints out something
@Stateless publicclass Timer1SessionBean implements Timer1SessionBeanLocal {
Timer2SessionBean: a recurring timer that creates a bunch of one-time timers,alsothe number of one-time timers created is roughly based on the maximum allowed number of active timers minus the number of active timers at that time.
// used to control the total number of threads running in the app
// use 10 as maximum in this example. int numberOfActiveTimers = timer3Bean.getCountOfActiveTimers();
if (numberOfActiveTimers < 10) { int toCreateNum = 10 - numberOfActiveTimers;
for (int i = 0; i < toCreateNum; i++) {
Timer3Info info = new Timer3Info();
// set start delays to be 30,60,90... seconds
info.setDelay(30000 * (i + 1));
timer3Bean.createTimer(info);
}
}
System.out.println("Exit timeout in timer2");
}
}
Timer3SessionBean:one-time timer created by another timer, provides the number of active timers for this bean, and prints out something.
/**
*
* @return the number of active timers
*/ publicint getCountOfActiveTimers(){ int retVal = 0; try {
//In rare occasions, could throw NullPointerException //because of a bug in weblogic
@SuppressWarnings("unchecked")
Collection<Timer> timersCol = timerService.getTimers();
if (timersCol != null)
retVal = timersCol.size();
} catch (Exception e) {
//if it failed, use the maximum (10 in this example), so no //new timers can be created
retVal = 10;
}
return retVal;
}
}
TestTimerCreateServlet: used to create recurring timers.
Overall,the code is quite simple. Some things are worth noting here is that local interfaces are used for the session beans, a servlet which needs to be invoked externally is used to create timers, also ‘timerService.getTimers()’is used to find out the number of active timers for a session bean and also help control the number of running timers in the system, so the system will not be over stretched.
weblogic-ejb-jar.xml: some important configurations
The most important things are that making sure the timers are cluster aware, and also using proper logical name for the persistent store (timerst), which will be configured in the administrator console.