中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> Bea专区 > WebLogic Server
WebLogic Server中的计划任务
作者:未知 时间:2006-08-09 14:46 出处:bea.com.cn 责编:月夜寒箫
              摘要:WebLogic Server中的计划任务 --编写特定于应用程序的计划任务

作者:Thomas Kunnumpurath, Vijay Chinta

  运行计划任务的需求不断增加,而且在所有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>

参考资料

关于Vijay Chinta
  Vijay Chinta是诺基亚的企业架构师。他擅长使用J2EE为通讯和运输行业开发企业应用程序。

关于Thomas Kunnumpurath
  Thomas Kunnumpurath是诺基亚的高级软件工程师。他在Java/J2EE应用程序开发方面经验丰富。

相关内容

图1:

图2:

原文出处

http://wldj.sys-con.com/read/48929.htm

 

源代码:

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>
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有