Sunday, March 25, 2007

Scheduling Jobs in WebLogic Server - src

The source code is in the following link:
http://photos.sys-con.com/story/res/48929/source.html

MyAppJobScheduler.java
/* * MyAppJobScheduler.java * */
import java.util.*;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.InstanceNotFoundException;
import weblogic.management.timer.Timer;
// Implementing NotificationListenerpublic final class MyAppJobScheduler implements NotificationListener
{
private static final long DAILY_PERIOD = Timer.ONE_DAY;
private static final long WEEKLY_PERIOD = 7 * Timer.ONE_DAY;
private Timer timer;
private Integer orderNotificationId;
private Integer inventoryNotificationId;
public MyAppJobScheduler() {
// Instantiating the Timer MBean
timer = new Timer();
// Registering this class as a listener
timer.addNotificationListener(this, null, "some handback ?object");
// These values should be read from a property file
int orderSubmissionHour=17;
int orderSubmissionMinute=0;
int inventorySubmissionDay = 6;
int inventorySubmissionHour = 22;
int inventorySubmissionMinute = 30;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,orderSubmissionHour);
calendar.set(Calendar.MINUTE,orderSubmissionMinute);
Date orderSubmissionDate = calendar.getTime();
calendar.set(Calendar.DAY_OF_WEEK, inventorySubmissionDay);
calendar.set(Calendar.HOUR_OF_DAY,inventorySubmissionHour);
calendar.set(Calendar.MINUTE,inventorySubmissionHour);
Date inventorySubmissionDate = calendar.getTime();
// Add the order notification which should run every day at ?5:00 PM
orderNotificationId = timer.addNotification("OrderSubmission", "Order Submission",
this,orderSubmissionDate, DAILY_PERIOD);
// Add the inventory notification which should run every Friday ?at 10:30 PM
inventoryNotificationId = timer.addNotification?("InventorySubmission", "Inventory Submission",
this,? inventorySubmissionDate, WEEKLY_PERIOD);
timer.start();
System.out.println( ">>> MyAppJobScheduler started." );
}

protected void finalize() throws Throwable {
System.out.println(">>> MyAppJobScheduler finalize called.");
cleanUp();
super.finalize(); }

public synchronized void cleanUp() {
System.out.println(">>> MyAppJobScheduler cleanUp method ?called.");
try {
timer.stop();
timer.removeNotification(orderNotificationId);
timer.removeNotification(inventoryNotificationId);
System.out.println(">>> MyAppJobScheduler Scheduler ?stopped.");
} catch (InstanceNotFoundException e){
e.printStackTrace();
}
}
/* callback method */
public void handleNotification(Notification notif, Object handback) {
String type = notif.getType();
if ( type.equals("OrderSubmission") ) {
// invoke the component or ejb that does the order processing ?and submission
} else if ( type.equals("InventorySubmission") ) {
// invoke the component or ejb that does the inventory ?processing and submission
}
}

public static void main(String[] args) {
MyAppJobScheduler myAppJobScheduler = new MyAppJobScheduler(); }
}


MyAppListener.java
/* * MyAppListener.java * */
import weblogic.application.ApplicationLifecycleListener;
import weblogic.application.ApplicationLifecycleEvent;
public class MyAppListener extends ApplicationLifecycleListener{
MyAppJobScheduler myAppJobScheduler;
public void preStart(ApplicationLifecycleEvent evt) {
System.out.println("MyAppListener:preStart Event");
}
public void postStart(ApplicationLifecycleEvent evt) {
System.out.println( "MyAppListener:postStart Event");
// Start the Scheduler myAppJobScheduler = new MyAppJobScheduler();
}

public void preStop(ApplicationLifecycleEvent evt) {
System.out.println( "MyAppListener:preStop Event");
// Stop the Scheduler
myAppJobScheduler.cleanUp();
}

public void postStop(ApplicationLifecycleEvent evt) {
System.out.println( "MyAppListener:postStop Event");
}
public static void main(String[] args) {
System.out.println( "MyAppListener:main method"); }
}

weblogic-application.xml
<weblogic-application>
<listener>
<listener-class>MyAppListener</listener-class>
</listener>
</weblogic-application>

No comments: