Develop and scheduling tasks
To develop and schedule tasks, use a configured scheduler.
- Look up a configured scheduler. Refer to the Accessing schedules topic. Each configured scheduler is available from two different programming models:
- A JEE server application, such as a servlet or EJB component, can use the Scheduler API. Schedulers are accessed by looking them up using a JNDI name or resource reference.
- JMX (JMX) applications, such as wsadmin scripts, can use the Scheduler API using WASScheduler MBeans.
- Develop the task.
The Scheduler API supports different implementations of the TaskInfo interface, each of which can be used to schedule a particular type of work. Refer to the Developing a task that calls a session bean topic and Develop a task that sends a Java Message Service (JMS) message topic for details. The task object referenced in the Develop a task that sends a Java Message Service (JMS) message topic can send a JMS message to either a queue or a topic. : Create and manipulating scheduled tasks through the Scheduler interface is only supported from within the EJB container or Web container (Enterprise beans or servlets). Looking up and using a configured scheduler from a Java EE application client container is not supported.
- Receive scheduler notifications. A notification sink is set on a task in order to receive the notification events that are generated by a scheduler when it performs an operation on the task.
- Use custom calendars. We can assign a UserCalendar session bean to a task that allows schedulers to use custom and predefined date algorithms to determine when a task should run. Refer to the UserCalendar interface topic for details.
- Submit tasks to a scheduler. After a TaskInfo object has been created, it can be submitted to the scheduler for task creation by calling the Scheduler.create() method.
- Manage tasks with a scheduler.
- Secure tasks with a scheduler.
Example
Use the SIMPLE and CRON calendars from any Java EE application. This example illustrates the process.
Use default scheduler calendars. The following code examples illustrates how to use connections correctly and incorrectly.Use default scheduler calendars involves looking up the default UserCalendarHome EJB home object, creating the UserCalendar bean and calling the applyDelta() method. For details on the applyDelta method as well as the syntax for the SIMPLE and CRON calendars, see UserCalendar interface topic.
import java.util.Date; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import com.ibm.websphere.scheduler.UserCalendar; import com.ibm.websphere.scheduler.UserCalendarHome; // Create an initial context InitialContext ctx = new InitialContext(); // Lookup and narrow the default UserCalendar home. UserCalendarHome defaultCalHome=(UserCalendarHome) PortableRemoteObject.narrow(ctx.lookup( UserCalendarHome.DEFAULT_CALENDAR_JNDI_NAME), UserCalendarHome.class); // Create the default UserCalendar instance. UserCalendar defaultCal = defaultCalHome.create(); // Calculate a date using CRON based on the current // date and time. Return the next date that is // Saturday at 2AM Date newDate = defaultCal.applyDelta(new Date(), "CRON", "0 0 2 ? * SAT");
- Access schedulers
Each configured scheduler is available using the Scheduler API from a JEE server application, such as a servlet or EJB module. Use a Java Naming and Directory Interface (JNDI). name or resource reference to access schedulers. Each scheduler is also available using the Java™ Management Extensions (JMX) API, using its associated WASScheduler MBean.
- Develop a task that calls a session bean
The Scheduler API and WASScheduler MBean API support different implementations of the TaskInfo interface, each of which can be used to schedule a particular type of work.
- Develop a task that sends a Java Message Service message
Create a task that sends a Java Message Service (JMS) message to a queue or topic. The Scheduler API and WASScheduler MBean API support different implementations of the TaskInfo interface, each of which can be used to schedule a particular type of work.
- Scheduling long-running tasks
The default behavior of the scheduler is designed to run business logic that runs for a short period of time. In version 6.0.2 and later, two API methods on the com.ibm.websphere.scheduler.TaskInfo interface help avoid some of the problems that can occur when running tasks for an extended time.
- Receiving scheduler notifications
Various notification events are generated by a scheduler when it performs an operation on a task. These notifications events are described in this topic.
- Submitting a task to a scheduler
Submit a task to a configured scheduler for creation.
- Task management methods using a scheduler
The scheduler provides several task management methods.
- Identifying tasks that are currently running
When a task runs, the task database record is locked until the task completes. Determine whether or not a task is running.
- Stopping tasks that are failing
The scheduler runs tasks in a global transactional context, by default. If a task is failing due to a configuration problem or application error, the scheduler attempts to retry the task until the scheduler failure threshold is reached. We can stop the tasks that are failing.
- Scheduler tasks and Java EE context
When a task is created using the Scheduler API create() method, the JEE (Java EE) thread context of the creator is stored with the scheduled task. When the task runs, the original Java EE thread context is reapplied to the thread before calling the customer TaskInfo instance.
- Secure scheduler tasks
Scheduled tasks are protected using application isolation and administrative roles.
- Scheduler configuration or topology
The scheduler uses a database to persist information concerning which tasks to run and when. Errors might occur when changing the application server topology or when changing the application or server configuration. When you change the configuration or topology, carefully consider how this action affects the scheduler.
- Scheduler interface
Use the com.ibm.websphere.scheduler.Scheduler Java object (in the Java™ Naming and Directory Interface (JNDI) namespace for the scheduler configuration) to find a reference to a scheduler and work with tasks.
Related tasks
Manage schedulers
MBean API documentation UserCalendar interface