job Scheduler in Commonj.TimerManager using WAS 6.0
1) create a JNDI using Resource => AsyncBeans=> TimerManager in WAS 6.0
2) Create a reference in the web.xml file like this.
a) Go to deployment descriptor file
b) Select the reference button
c) Select give the resource name, JNDI name and all the details in that tab.
d) Save it and refreence will be created in web.xml.
3) Create a CachingTimer.java
public class CachingTimer {
public void loadTimer(){
try {
InitialContext ctx = new InitialContext();
TimerManager tm = (TimerManager)ctx.lookup("java:comp/env/tm/PipCaching");
//Set the timer to expire the cache every thirty minutes
//from now and repeat for every 30 minutes
Timer timer =tm.scheduleAtFixedRate(new CacheAdminImplTimerListener(),1000*30, 1000*30);
}catch(Exception e){
e.printStackTrace();
}
}
Here timer also have a method called schedule() which fires job based on the interval time. say for an example,
Timer timer =tm.schedule(new CacheAdminImplTimerListener(),1000*30, 1000*30);
In this case, if the first job start time is 2.00 Am and it takes 20 minutes to complete then after the gap of 30 minutes the second job will be fired.
It is not fired at fixed rate frequency.
4) Create a java class which implements TimerListener interface.
public class CacheAdminImplTimerListener
implements TimerListener {
public void timerExpired(Timer timer) {
// Here actually write the business logic.
CacheAdmin c =new CacheAdminImpl();
c.removeAllCache();
}
}
timerExpired() method is the abstract method in TimerListener class and this method needs to be overridden in the implementation class.
5) To Stop the timer or Cancel the timer then we need to implement the StopTimerListener class and CancelTimerListener class.
Monday, March 17, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment