[Version 5.0.1 and later]Developing a task that calls a session bean

Why and when to perform this task

The scheduler API supports different implementations of the TaskInfo interface, each of which can be used to schedule a particular type of work. This topic describes how to call a method on a task handler session bean using the BeanTaskInfo implementation.

Steps for this task

  1. Create a stateless session bean that implements the process() method in the com.ibm.websphere.scheduler.TaskHandler remote interface. Place the business logic you want created in the process() method. The process() method is called when the task fires.
    The Home and Remote interfaces must be set as follows in the bean's deployment descriptor:
    • com.ibm.websphere.scheduler.TaskHandlerHome
    • com.ibm.websphere.scheduler.TaskHandler
  2. In a java server page (JSP), servlet or session bean, create an instance of the BeanTaskInfo interface by using the following Scheduler.createBeanTaskInfo()method:
    //lookup the scheduler instance to be used
    Scheduler scheduler = (Scheduler)new InitialContext.lookup("java:comp/env/Scheduler");
    
    BeanTaskInfo taskInfo = (BeanTaskInfo) scheduler.createTaskInfo(BeanTaskInfo.class)
    

    Note: Creating a BeanTaskInfo object does not add the task to the persistent store. Rather, it creates a placeholder for the necessary data. The task is not added to the persistent store until the create() method is called on a Scheduler instance, as described in the topic Submitting a task to a scheduler.

  3. Now, set parameters on the BeanTaskInfo object. These parameters define which task is to run, which session bean is called, and so on.

    The TaskInfo interface contains various set() methods that you can use to control execution of the task, including when the task will fire and what work the task will do when it fires. For example:

    //create a date object which represents 30 seconds from now
    java.util.Date startDate = new java.util.Date(System.currentTimeMillis()+30000);
    
    //find the session bean to be called when the task executes
    Object o = new InitialContext().lookup("java:comp/env/ejb/MyTaskHandlerHome");
    TaskHandlerHome home = (TaskHandlerHome)javax.rmi.PortableRemoteObject.narrow(o,TaskHandlerHome.class);
    
    //now set the start time and task handler to be called in the task info
    taskInfo.setTaskHandler(home);
    taskInfo.setStartTime(startDate);

    The TaskInfo interface specifies additional control points, as documented in Javadoc.

Results

A TaskInfo object has been created that contains all of the relevant data for a task.

What to do next

Submit the task to a scheduler instance for creation, as described in the topic Submitting a task to a scheduler.

Related tasks
Developing a task that sends a JMS message
Receiving scheduler notifications
Managing tasks with a scheduler
Managing the scheduler service
Related reference
Scheduler interface



Searchable topic ID:   tsch_schedulebtask
Last updated: Jun 21, 2007 8:07:48 PM CDT    WebSphere Business Integration Server Foundation, Version 5.0.2
http://publib.boulder.ibm.com/infocenter/wasinfo/index.jsp?topic=/com.ibm.wasee.doc/info/ee/scheduler/tasks/tsch_schedulebtask.html

Library | Support | Terms of Use | Feedback