Site hosted by Angelfire.com: Build your free website today!

Task scheduling for the J2EE applications

Introduction
Task scheduling is a common need in enterprise applications and is used to provide various kinds of functionality – reminders, batch transfers, notifications etc. This document explains the implementation of a task-scheduling component that can be used by J2EE applications. The component makes use of the Message Driven Model in EJB 2.0 specification and JDK 1.3.


Architecture
The architecture of the component is explained below:


The ‘Task Trigger’ is a component or a set of components that decide the time of execution for a scheduled task and provides the scheduled application the required information for execution. The ‘subscriber id’ is the id of the application that has to run at the time of execution. The ‘Task Trigger’ invokes the ‘Task scheduler’ using RMI and passes the time of execution, the subscriber id and the required information. The task triggers themselves perform a set of tasks one of which is scheduling a task. The required information is used by the applications that execute the scheduled tasks. It can be passed as any object.

The ‘File Transfer Application’ and the ‘Reminder Application’ run at scheduled times. They are implemented as Message Driven Beans (specified in EJB 2.0 specification). They subscribe to the Timer Message Bus. They pick up any message written to the message bus. If the subscriber id matches the subscriber, the message body is read and the task is executed.


The task scheduler
The task scheduler component uses the java.util.Timer class specified in JDK 1.3 to schedule tasks. The Timer object creates threads to schedule tasks. For this reason, it should not be called or created directly by EJBs, JSPs or servlets (see J2EETM SDK documentation and J2EE Blueprints). Hence, it implemented as a RMI service that can exist at any location in the application server’s JNDI namespace.

The java.util.Timer class scales to large numbers of concurrently scheduled tasks (thousands should present no problem). Internally, it uses a binary heap to represent its task queue, so the cost to schedule a task is O(log n), where n is the number of concurrently scheduled tasks. (Source: JDK1.3 API Documentation)

The task scheduler may or may not be deployed within the same application server(s) as the J2EE application. In either case, the RMI stub should be available to the task triggers. The task scheduler writes a message with the subscriber id and the required information on the Timer Message Bus at the specified time of execution for a task.

The subscriber id is passed as a String property of the JMS Message object itself. The information passed from the task trigger application forms the message body. The task scheduler does not concern itself with the semantics of the subscriber id or the object being passed. It is only concerned with the scheduling of the task using the execution time(s) specified.

The task scheduler exposes interfaces for the task trigger to schedule tasks in different ways. Tasks could be scheduled to run
  • at a specific time
  • after a specified delay
  • repeatedly after fixed intervals of time beginning after a specified delay
  • repeatedly after fixed intervals of time beginning at a specified time

Deploying the RMI Server for the Scheduler
  • Extract the source files. The source files are in the ZIP file.
  • Modify the build script(build.cmd) to reflect the correct JDK path
  • Run the build script.
  • Extract the scheduler.properties file to the directory from where you will be running the RMI server. Modify the default values if required. The default values assume that the naming service is running on the same machine as the RMI server. Also the default initial context factory used is the Weblogic’s WLInitialContextFactory class. You will need to change this if you are using any other application server.
  • If you are using JavaSoft’s RMI registry, start the registry
    c:\jdk1.3\bin\rmiregistry
  • Start the RMI server for the Scheduler
    java com.planetasia.timer.SchedulerServer

Using the Scheduler
The following points should be noted while developing applications using the scheduler.
  • The Scheduler RMI service should be accessible by the J2EE application. All components accessing the service should import the package com.planetasia.timer.
  • The Scheduler writes to a JMS Topic that is bound to the application server’s naming service with the name ‘TimerBus’. All message driven beans should listen on the JMS Topic by this name. The topic does not need to be created. The scheduler will create the topic if not already created.
  • The scheduler requires a topic connection factory. This should be bound to the application server’s naming service with the JNDI name ‘javax.jms.TopicConnectionFactory’. Weblogic creates this by default at the time of installation.

Known issues at time of publishing
  • Does not work with JDK1.3’s RMI registry. There is an error while binding the remote object to the registry. You will need to use JDK1.2’s RMI registry.
  • The RMI Server cannot be deployed as a weblogic RMI service. JavaSoft and Weblogic’s wire protocol for RMI are different. For more information refer to http://newsgroups.bea.com

Please contact me at vikramr@planetasia.com for comments and suggestions.