Contents

Overview

The Timer and Work Manager API is defined in a specification created jointly by Oracle and IBM. This API enables concurrent programming of EJBs and Servlets within a Java EE application. This API is often referred to as CommonJ.

The CommonJ API contains the following components:

Timer API

The Timer API allows applications to schedule and receive timer notification callbacks for a specific listener defined within an application. Timers allow you to schedule and perform work at specific times or intervals. See Timer API Overview.

You implement this API by importing the commonj.timer package.

Work Manager API

The Work Manager API allows an application to prioritize work within an EJB or servlet. Applications can programmatically execute multiple work items within a container. See Work Manager API.

You implement this API by importing the commonj.work package.

In addition to the CommonJ Work Manager API, WebLogic Server includes server-level Work Managers that provide prioritization and thread management. These can be configured globally or for a specific module in an application.

Although commonj.timer and commonj.work are part of the same API, each provides different functionality. Which one you implement depends on the specific needs of your application. The CommonJ Timer API is ideal for scheduling work at specific intervals; for example, when you know that a certain job should run at a specific time. The CommonJ Work API is ideal for handling work based on priority. For example, you may not be able to predict exactly when a specific job will occur, but when it does you want it to be given a higher (or lower) priority.

The following sections describe the CommonJ APIs in detail.

Timer API Overview

The Timer API consist of three interfaces:

TimerManager

TimerListener

Timer

The TimerManager interface provides the framework for creating and using timers within a managed environment. The TimerListener receives timer notifications. The TimerManager.schedule method is used to schedule the TimerListener to run at a specific time or interval.

TimerManager Interface

The TimerManager interface provides the general scheduling framework within an application. A managed environment can support multiple TimerManager instances. Within an application you can have multiple instances of a TimerManager.

Creating and Configuring a TimerManager

A TimerManager is configured during deployment by means of deployment descriptors. The TimerManager definition may also contain additional implementation-specific configuration information.

Once a TimerManager is defined in a deployment descriptor, instances of it can be accessed using a JNDI lookup in the local Java environment. Each invocation of the JNDI lookup() on a TimerManager returns a new logical instance of a TimerManager.

Using the Job Scheduler

This section explains how to use the Job Scheduler functionality. The Job Scheduler allows you to implement the commonj.timer API within a clustered environment.

The Job Scheduler is essentially an implementation of the commonj.timer API package that can be used within a cluster. In this context, a job is defined as a commonj.timers.TimerListener instance that is submitted to the Job Scheduler for execution.

Life Cycle of Timers

When you implement the commonj.timer API within an application, you can configure two possible life cycles for a timer:

Local timer

A local timer is scheduled within a single server JVM and is handled within this JVM throughout its life cycle. The timer continues running as long as the JVM is running and fails when the JVM exits. The application is responsible for rescheduling the timer after server startup.

This is the basic implementation of the commonj.timers package.

Cluster-wide timer

A cluster-wide timer is aware of the other JVMs containing each server within the cluster and is therefore able to perform load balancing and failover. The life cycle of a cluster-wide timer is not bound to the server that created it, but continues to function throughout the life cycle of the cluster. If at least one cluster member is alive, the timer continues to function. This functionality is referred to as the Job Scheduler.

Implementing the Timer API for a Job Scheduler has the following requirements in addition to those listed in Implementing the Timer API:

The Timer Listener class must be serializable.

The Timer Listener class must be present in the server system classpath.

The minimum time for recurring execution of a timer is 30 seconds because Job Schedulers pick up timers for execution every 30 seconds.

Each timer has its own advantages and disadvantages. Local timers can process jobs with much smaller time intervals between jobs. Due to the persistence requirements within a cluster, Job Schedulers cannot handle jobs with as much precision. On the other hand, Job Schedulers are better suited for tasks that must be performed even if the initial server that created the task has failed.

Implementing and Configuring Job Schedulers

This section describes the basic procedure for implementing Job Schedulers within an application and for configuring your WebLogic Server environment to utilize them. The following topics are included:

Database Configuration

To maintain persistence and make timers cluster-aware, Job Schedulers require a database connection. The Job Scheduler functionality supports the same databases that are supported by server migration.

For convenience, you can use the same database used for session persistence, server migration, and so on. For example, see "Configure server migration in a cluster" in the Oracle WebLogic Server Administration Console Help for information about how to create or select a data source for server migration.

In the database, you must create a table named WEBLOGIC_TIMERS. Schemas for creating this table are in the following location:

WL_HOME/server/db/dbname/scheduler.ddl

In the preceding path, dbname represents the name of the database.

Data Source Configuration

After you create a table with the required schema, you must define a data source that is referenced from within the cluster configuration. Job Scheduler functionality is available only if a valid data source is defined in the ClusterMBean.DataSourceForJobScheduler attribute. For information about how to use the WebLogic Server Administration Console to configure this attribute, see "Configure a data source for a job scheduler" in the Oracle WebLogic Server Administration Console Help.

Leasing

Leasing must be enabled for Job Schedulers. You can use either high-availability database leasing or non-database consensus leasing. When using high-availability database leasing, you must create the leasing table in the database.

Schemas for creating this table are in the following location:

WL_HOME/server/db/dbname/leasing.ddl

In the preceding path, dbname represents the name of the database.

For more information, see "Leasing" in Using Clusters for Oracle WebLogic Server.

JNDI Access within a Job Scheduler

The procedure for performing JNDI lookup within a clustered timer is different from that used in the general commonj.timer API. The following code snippet shows how to cast a JNDI lookup to a TimerManager.

Canceling Jobs

You can cancel jobs programmatically or by using the WebLogic Server Administration Console.

To cancel a job programmatically, invoke the cancel method of the job's corresponding JobRuntimeMBean. You can access a JobRuntimeMBean using either of the following ways:

Invoke JobSchedulerRuntimeMBean.getJob(id) with the ID of a scheduled job. To get the ID, invoke the JobScheduler.schedule method to return a Timer object, then use the Timer's toString method to return the ID.

Invoke JobSchedulerRuntimeMBean.getExecutedJobs() to return an array of JobRunTimes for all jobs that have been executed at least once.

You cannot invoke the cancel method to cancel a scheduled job that has not executed at least once.

For information about how to use the WebLogic Server Administration Console to cancel jobs, see "Cancel jobs" in Oracle WebLogic Server Administration Console Help.

Debugging

Unsupported Methods and Interfaces

The following methods and interfaces in the commonj.timer package are not supported in the Job Scheduler environment:

CancelTimerListener interface

StopTimerListener interface

The following methods of the TimerManager interface:

suspend

resume

scheduleAtFixedRate

stop

waitForStop

waitForSuspend

Work Manager API

The Work Manager API, commonj.work, provides a set of interfaces that allows an application to execute multiple work items concurrently within a container.

Essentially this API provides a container-managed alternative to the java.lang.Thread API. The latter should not be used within applications that are hosted in a managed Java EE environment. In such environments, the Work Manager API is a better choice because it allows the container to have full visibility and control over all executing threads.

Note:

The Work Manager API provides no failover or persistence mechanisms. If the Managed Server environment fails or is shut down, any current work is lost.

Work Manager Interfaces

This section summarizes the interfaces in the Work Manager API. For details about using these interfaces, see commonj.work in the Oracle WebLogic Server API Reference.

The Work Manager API contains the following interfaces:

WorkManager - Provides a set of scheduling methods that are used to schedule work for execution.

A WorkManager is defined by system administrators at the server level. A WorkManager instance is obtained by performing a JNDI lookup. A managed environment can support multiple WorkManager instances. You configure WorkManagers during deployment as resource-refs. See Work Manager Deployment.

At the application level, each instance of WorkManager returns a WorkItem. For more information about implementing a WorkManager within an application, see WorkManager in the Oracle WebLogic Server API Reference.

Work - Allows you to run application code asynchronously. By creating a class that implements this interface, you can create blocks of code that can be scheduled to run at a specific time or at defined intervals. In other words, this is the "work" that is handled within the Work Manager API.

WorkItem - Determines the status of a completed Work instance. A WorkItem is returned by a WorkManager after a Work instance has been submitted to that WorkManager.

For more information, see Work in the Oracle WebLogic Server API Reference.

WorkListener - Provides communication between the WorkManager and the scheduled work defined within the Work instance. WorkListener is a callback interface.

You can use WorkListener to determine the current status of the Work item. For more information, see WorkListener in the Oracle WebLogic Server API Reference.

Note:

WorkListener instances are always executed in the same JVM as the original thread used to schedule the Work by means of the WorkManager.

WorkEvent - A WorkEvent is sent to a WorkListener as Work is processed by a WorkManager.

For more information, see WorkEvent in the Oracle WebLogic Server API Reference.

RemoteWorkItem - The RemoteWorkItem interface is an extension of the WorkItem interface that allows work to be executed remotely. This interface allows serializable work to be executed on any member of a cluster.

For more information, see RemoteWorkItem in the Oracle WebLogic Server API Reference.

Work Manager Deployment

Work Managers are defined at the server level by means of a resource-ref in the appropriate deployment descriptor. This can be web.xml or ejb-jar.xml, among others.

The following deployment descriptor snippet shows the configuration of a WorkManager:

This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited.

The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing.

If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data delivered to U.S. Government customers are "commercial computer software" or "commercial technical data" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, the use, duplication, disclosure, modification, and adaptation shall be subject to the restrictions and license terms set forth in the applicable Government contract, and, to the extent applicable by the terms of the Government contract, the additional rights set forth in FAR 52.227-19, Commercial Computer Software License (December 2007). Oracle America, Inc., 500 Oracle Parkway, Redwood City, CA 94065.

This software or hardware is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications.

Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark of The Open Group.

This software or hardware and documentation may provide access to or information on content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services.

Scripting on this page enhances content navigation, but does not change the content in any way.