近来总是在思考自己的项目中有一个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>
分享到:
- 2007-11-04 13:22
- 浏览 4194
- 评论(0)
- 论坛回复 / 浏览 (0 / 3385)
- 查看更多
相关推荐
3. **部署与管理**: WebLogic Server提供了管理控制台,可以进行应用部署、集群配置、资源监控、安全管理等一系列运维任务。中文版书籍可能会详细讲解如何使用这些功能。 4. **集群与高可用性**: 集群是WebLogic ...
- **域(Domain)**:WebLogic Server 中的最高层级管理单元,包含了一个或多个WebLogic Server实例,这些实例可以是管理服务器或受管服务器。 - **管理服务器(Administration Server)**:用于管理整个域中的...
2. **Server**:是WebLogic Server中的一个相对独立的工作单元,它可以执行特定的任务,如处理HTTP请求、运行应用程序等。一个Domain可以包含一个或多个Server,甚至可以包含Server集群。 **WebLogic Server集群的...
Weblogic Server 的线程数控制是指在 Weblogic Server 中控制和管理线程数的机制,以确保服务器的性能和稳定性。在本文中,我们将详细介绍 Weblogic Server 线程数控制的概念、解决方案、线程池管理、工作负荷管理器...
- **WebLogic Server Ant任务**:Ant任务提供了与weblogic.Server、weblogic.Admin(不推荐在新版本中使用)和weblogic.Deployer命令相似的功能,可用于大部分配置需求。 在使用这些工具时,确保环境设置正确至关...
在WebLogic Server的管理中,以下几个核心知识点至关重要: 1. **安装与配置**:指南会详细介绍如何在不同操作系统上安装WebLogic Server,包括设置环境变量、配置Java环境以及创建域(Domain)的过程。域是...
一个Domain可以包含一个或多个WebLogic Server 实例,其中一个Server作为管理Server,负责管理整个Domain,其他Server则负责执行特定任务。 **2. Domain and Server 的关系** - 每个Domain至少包含一个Server作为...
WebLogic Server是由BEA Systems(后被Oracle公司收购)开发的一款企业级Java应用服务器,它提供了用于构建、部署和管理企业应用程序的全面平台。本文主要介绍WebLogic Server的安装和配置流程,包括不同安装模式、...
4. **管理服务器(Admin Server)**:管理服务器负责整个域的管理任务,包括但不限于监控、配置更改、日志记录等。每个域必须有一个管理服务器。 5. **受管服务器(Managed Server)**:受管服务器是实际运行应用...
本书是WebLogic开发人员的权威参考手册,书中深入探讨了WebLogic Server所提供的功能,并列举了一些最佳开发实践。本书全面介绍了WebLogic在J2EE应用程序的各个方面所完成的任务:从使用servlet、JSP和EJB,到管理...
在部署过程中遇到的主要问题是WebLogic Server无法正确部署应用。具体表现为服务器选择的是旧版本的应用程序,而不是重新部署的新版本的应用程序。 #### 二、故障排除步骤 在解决这类问题时,并非所有步骤都必须...
WebLogic Server性能调优是针对BEA公司的应用服务器产品的一项关键任务,旨在优化服务器的运行效率,提升系统响应速度和处理能力。以下是基于提供的文件内容的详细解释: 1. **硬件,操作系统,网络调整** - **...
【WebLogic Server 9.2 ...总之,WebLogic Server 9.2 的集群配置是一项重要的任务,它可以提升系统的可用性、性能和扩展性。通过合理的集群设计和配置,可以确保企业应用在面对高流量或服务器故障时依然能够正常运行。
配置WebLogic Server 11g集群、Session复制、定时任务(Windows&Linux)
- **Oracle WebLogic Server 11g: Advanced Administration**:提供更深入的管理任务覆盖,适合已经具备基础管理经验的WebLogic Server管理员。 #### 七、总结 Oracle WebLogic Server 11g 提供了一个强大的平台来...
- **Apache-WebLogicServer插件的参数** - 插件使用的参数说明。 - **使用SSL协议** - 如何使用SSL协议进行加密通信。 - **与SSL-Apache配置有关的问题** - 配置SSL时可能遇到的问题及解决方案。 - **Httpd....
8. **监控与诊断**:书中会讲解WebLogic Server的管理控制台和WLST(WebLogic Scripting Tool)的使用,用于监控服务器状态、收集诊断信息和执行自动化任务。 9. **性能调优**:调优是确保WebLogic Server高效运行...
通过一系列的实验案例,如搭建Apache+WebLogic Server架构,学员将学习如何在5节点环境中构建WebLogic Server+Oracle的组合,以及如何设计和搭建EJB和JMS双机集群。 在WebLogic Server Domain的创建与配置中,学员...
13. **WebLogic Scripting Tool (WLST)**:提供命令行接口,允许通过脚本自动化执行WebLogic Server的管理和配置任务。 14. **持久化框架支持**:如Hibernate和OpenJPA等,方便数据的持久化操作。 15. **云部署**...