|
近来总是在思考自己的项目中有一个web应用采用weblogic,而还有一个数据处理专用socket服务器、还有一个涉及业务操作的socket服务器,里面用到好多定时处理的问题,我们都是采用老外的scheduling.jar包并继承处理,然而这样总是给客户带来诸多不便,因为启动一次机器要启动三个服务,并同时维护三个服务的程序还要分别部署,所以就研究向weblogic这样的中间件是否也支持计划任务和类似socket之类的监听,如果可以得话我们将在下一个版本中将三个服务合成到中间件中,可以节省客户和我们自己的维护成本啊,呵呵,以下是我搜罗的计划任务相关资料,分享一下:)
运行计划任务的需求不断增加,而且在所有J2EE应用程序中日益普及。当前J2EE规范无法提供一种轻松的方式来计划企业应用程序内部的任务。
大体上,我们可以将J2EE计划任务划分为两类:特定于服务器的计划任务和特定于应用程序的计划任务。本文将探讨如何在WebLogic Application Server内部计划特定于应用程序的任务。
特定于服务器和特定于应用程序的计划任务
特定于服务器的计划任务涉及到应用服务器的生命周期。通常,这些任务在服务器启动期间开始执行,一直运行到服务器停机。由此,计划任务所需的所有资源都必须在服务器的类路径中指定。
特定于应用程序的计划任务涉及到企业应用程序的生命周期。它们在应用程序部署之后启动,一直运行到应用程序取消部署。特定于应用程序的计划任务所需的资源打包在企业应用程序中(EAR – 企业归档资源)。
应用程序任务优先于服务器任务,原因如下:
* 应用程序任务的任何改变都可以使用应用程序(EAR文件)轻松地重新部署。对于服务器任务来说,新的改变要想生效,则必须重新启动服务器,这是非常不理想的,因为它影响了部署在该服务器上的其他应用程序的可用性。
* 由于计划任务通常调用业务功能,所以在EAR文件中将它们打包在一起更加符合逻辑。
* 通过关联资源和EAR文件,我们可以将EAR文件部署在WebLogic Server的另一个实例中,而不必重新配置该服务器的资源。
企业应用程序中的计划任务
我们将看一个如何实现特定于应用程序的计划任务的示例。我们将计划两个在某些指定间隔内调用业务功能的任务。
* 订单提交 – 每天都调用。
* 库存提交 – 每周调用一次。
在该示例中,我们将联合使用WebLogic的Application Lifecycle Events和WebLogic的Timer Notifications。
以下是设置计划任务时涉及到的三个步骤:
1. 实现负责添加、监听和处理定时通知的Timer Notification Listener。
2. 实现Application Lifecycle Listener,它实例化了期望应用程序生命周期事件上的Timer Notification Listener。
3. 在weblogic-application.xml中注册Application Lifecycle Listener。
实现MyAppJobScheduler
MyAppJobScheduler将实现javax.management.NotificationListener接口。
public final class MyAppJobScheduler implements NotificationListener
该类将实例化weblogic.management.timer.Timer类,并将自身注册为一个用于定时通知的监听器。Timer类是javax.management.TimerMBean的WebLogic实现。
timer = new Timer();
timer.addNotificationListener(this, null, "some handback object");
订单通知和库存通知在定时器启动之前添加到定时器中。
timer.addNotification("OrderSubmission", "Order Submission",
this,orderSubmissionDate, DAILY_PERIOD);
timer.addNotification("InventorySubmission",
"Inventory Submission", this,inventorySubmissionDate,
WEEKLY_PERIOD);
我们将计划一些任务,以使Order任务在每天下午5点执行,Inventory任务每周五下午10点30分执行一次。请检查代码清单,了解如何构造具有指定时间的Date对象。
回调方法将得到一个通知。根据这一通知,业务流程组件(EJB)将被调用,以履行计划任务。
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
}
}
MyAppJobScheduler的cleanup方法可以使定时器停止,并删除添加到其中的所有通知。我们可以按照上述的preStop方法,或者根据MyAppJobScheduler的finalize方法来调用cleanUp方法。
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(); } }
MyAppJobScheduler的final方法如下所示:
protected void finalize() throws Throwable
{
System.out.println(">>> MyAppJobScheduler finalize called.");
cleanUp();
super.finalize();
}
实现MyAppListener
我们将编写一段可以扩展weblogic.application.ApplicationLifeCyleListener的类MyAppListener。应用程序生命周期监听器事件提供了开发人员在部署、取消部署和重新部署期间用来控制行为的处理。
public class MyAppListener extends ApplicationLifecycleListener
我们想在应用程序部署时立即启动计划任务。因此,我们使用MyAppListener的postStart方法来调用MyAppJobScheduler。如果希望在启动之前调用计划任务,则调用将使用preStart方法。
public void postStart(ApplicationLifecycleEvent evt)
{
System.out.println( "MyAppListener:postStart Event");
//Start the Scheduler
myAppJobScheduler = new MyAppJobScheduler();
}
我们希望在应用程序取消部署之前计划任务。因此我们使用MyAppListener的preStop方法来调用MyAppJobScheduler的cleanUp方法。
public void preStop(ApplicationLifecycleEvent evt)
{
System.out.println( "MyAppListener:preStop Event");
//Stop the Scheduler
myAppJobScheduler.cleanUp();
}
注册MyAppListener
在weblogic-application.xml中,注册应用程序生命周期事件的MyAppListener类。
<listener>
<listener-class>MyAppListener</listener-class>
</listener>
在应用程序类路径中包含MyAppJobScheduler和MyAppListener,或者,可以使用<listener-uri>参数来指定jar文件。
<listener>
<listener-class>MyAppListener</listener-class>
<listener-uri>scheduler.jar</listener-uri>
</listener>
//源代码:
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 NotificationListener
public 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> |
|