How to use Weblogic Timer:
Purpose of Timer class:
Timer class can be used as a scheduler to execute the method of a class (method
may perform any kind of functionality like sending mail notifications or
updating the database etc.) at defined intervals (for every minute, once in 24
hrs etc.) in weblogic server. This is similar to a cron job in UNIX or
Jobs in Database, which executes a process at defined intervals.
Sometimes, we may require executing a process with in weblogic server
itself. In this case, we can go for Timer class to execute a process.
The Timer class is supported by Weblogic Server:
Class Name:
weblogic.management.timer.Timer.
The Web logic Timer Service does not provide the centralized service means does
not support all applications deployed in the server instance. Each application
has to instantiate the Timer instance and use notification methods, if the
application requires Timer service.
We have used the Timer Service in below scenario:
To refresh the data in cache memory once in 24 hrs. This can be achieved by
using threads in a Java Program. But this is little cumbersome compared to
using the Timer class of weblogic server.
Using
Timer class is very simple. Here are the steps
provided:
- Create a listener class to receive the notifications.
- Inside the listener, class instantiate the Timer of weblogic.management.timer.Timer.
- Create a notification Listener with optional filter and register the listener with Timer.
- Invoke the add Notification API on Timer instance to send out a notification object at a specific time or at a recurring interval.
- Invoke Timer.start () to start the Timer service.
- Add the handleNotification (Notification, Object) method at the end of the listener class to perform the required functionality. ( Refreshing the cache memory with new data)
- Remove these notifications by using any of the methods removeNotification (java.lang.Integer id) or removeAllNotifications (), or removeNotifications (java.lang.String type).
Sample code of
using timer class:
1). sampleListener.java
import
javax.servlet.ServletContextListener;
import
javax.servlet.ServletContextEvent;
import
java.util.Date;
import
javax.management.Notification;
import
javax.management.NotificationListener;
import
javax.management.InstanceNotFoundException;
import
javax.servlet.ServletContextEvent;
import
javax.servlet.ServletContextListener;
import
weblogic.management.timer.Timer;
public final class
sampleListener
implements
ServletContextListener, NotificationListener
{
private static final long PERIOD = Timer.ONE_MINUTE;
private Timer timer;
private Integer notificationId;
public void contextInitialized(ServletContextEvent event) {
System.out.println (“>>> contextInitialized
called.”);
timer = new Timer();
SimpleServlet sim = new SimpleServlet ();
timer.addNotificationListener(this, null, “hand object”);
// Adding the notification to the Timer and assigning the
// ID that the Timer returns to a variable
Date timerTriggerAt = new Date ((new Date ()).getTime () + 5000L);
notificationId = timer.addNotification (“sample”, “sample”, this,
timerTriggerAt, PERIOD);
timer.start ();
System.out.println (“>>> timer started.”);
}
public void contextDestroyed (ServletContextEvent event)
{
System.out.println (“>>> contextDestroyed
called.”);
try {
timer.stop();
timer.removeNotification(notificationId);
System.out.println(“>>> timer stopped.”);
}
catch (InstanceNotFoundException e) {
e.printStackTrace(); }
} /* callback method */
public void handleNotification(Notification notif, Object handback) {
SimpleServlet sim = new SimpleServlet ();
sim.displayMethod (); // It displays a string value.
}
}
2). Web.xml
Here, SimpleServlet is a simple class with one method, which displays a
message. Prepare a WAR file with SimpleServlet.class, sampleListener.class,
web.xml and deploy the WAR file in Web logic server.