Develop a task that calls a session bean

 

Overview

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. This topic describes how to create a task that will call a method on a TaskHandler session bean

 

Procedure

  1. Create a new enterprise application with an EJB module. This application hosts the TaskHandler EJB module.

  2. Create a stateless session bean in the EJB Module 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 runs. The Home and Remote interfaces must be set as follows in the deployment descriptor bean:

    • com.ibm.websphere.scheduler.TaskHandlerHome

    • com.ibm.websphere.scheduler.TaskHandler

  3. Create an instance of the BeanTaskInfo interface by using the following example factory method. Using a JavaServer Pages (JSP) file, servlet or EJB component, create the instance as shown in the following code example. This code should coexist in the same application as the previously created TaskHandler EJB module

    // Assume that a scheduler has already been looked-up in JNDI.
    BeanTaskInfo taskInfo = (BeanTaskInfo) scheduler.createTaskInfo(BeanTaskInfo.class)
    
    
    We can also use the wsadmin tool to create the instance as shown in the following JACL scripting example

    set taskHandlerHomeJNDIName ejb/MyTaskHandler
    
    # Map the JNDI name to the mbean name.  The mbean name is formed by replacing the / in the jndi name
    # with . and prepending Scheduler_
    regsub -all {/} $jndiName "." jndiName
    set mbeanName Scheduler_$jndiName
    
    puts "Looking-up Scheduler MBean $mbeanName"
    set sched [$AdminControl queryNames WebSphere:*,type=WASScheduler,name=$mbeanName]
    puts $sched
    
    # Get the ObjectName format of the Scheduler MBean
    set schedO [$AdminControl makeObjectName $sched]
    
    # Create a BeanTaskInfo object using invoke_jmx
    puts "Creating BeanTaskInfo"
    set params [java::new {java.lang.Object[]} 1]
    $params set 0 [java::field com.ibm.websphere.scheduler.BeanTaskInfo class]
    
    set sigs [java::new {java.lang.String[]} 1]
    $sigs set 0 java.lang.Class
    
    set ti [$AdminControl invoke_jmx $schedO createTaskInfo $params $sigs]
    set bti [java::cast com.ibm.websphere.scheduler.BeanTaskInfo  $ti]
    puts "Created the BeanTaskInfo object: $bti"
    
    

    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, as described in the topic Submitting tasks to schedulers.

  4. Set parameters on the BeanTaskInfo object. These parameters define which session bean is called and when. The TaskInfo interface contains various set() methods that use to control execution of the task, including when the task runs and what work the task does when it runs.

    The BeanTaskInfo interface requires that the TaskHandler JNDI name or TaskHandlerHome is set using the setTaskHandler method. If using the WASScheduler MBean API to set the task handler, then the JNDI name must be the fully-qualified global JNDI name.

    The TaskInfo interface specifies additional control points, as documented in the Javadoc. Set parameters using the TaskInfo interface API method as shown in the following code 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);
    
    We can also set parameters using the following JACL scripting example:

    # Setup the task 
    puts "Setting up the task..."
    # Set the startTime if you want the task to run at a specific time, for example:
    $bti setStartTime [java::new {java.util.Date long} [java::call System currentTimeMillis]]
    
    # Set the StartTimeInterval so the task runs in 30 seconds from now
    $bti setStartTimeInterval 30seconds
    
    # Set JNDI name of the EJB which will get called when the task runs.  Since there is no
    # application J2EE Context when the task is created by the MBean, this must be a 
    # global JNDI name. 
    $bti setTaskHandler $taskHandlerHomeJNDIName
    
    # Do not purge the task when it's complete
    $bti setAutoPurge false
    
    # Set the name of the task.  This can be any string value.
    $bti setName Created_by_MBean
    
    # If the task needs to run with specific authorization one can set the tasks Authentication Alias
    # Authentication aliases are created using the Admin Console.
    # $bti setAuthenticationAlias {myRealm/myAlias}
    
    puts "Task setup completed."
    
    

 

Result

A BeanTaskInfo object has been created that contains all of the relevant data to call an EJB method.

 

What to do next

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


 

Related Tasks


Developing a task that sends a Java Message Service message
Receiving scheduler notifications
Manage tasks with a scheduler
Manage schedulers

 

See Also


Scheduler interface