Network Deployment (Distributed operating systems), v8.0 > Develop and deploying applications > Develop Scheduler service > Scheduling tasks


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

To create a task to call a method on a TaskHandler session bean, use these steps.


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 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 wsadmin.sh 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"
    
    

    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 Submitting tasks to schedulers topic.

  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 you can 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 Java™ Naming and Directory Interface (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 API documentation. 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 starts
    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 you can set the tasks Authentication Alias
    # Authentication aliases are created using the Admin Console.
    # $bti setAuthenticationAlias {myRealm/myAlias}
    
    puts "Task setup completed."
    
    


Results

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.
Task management methods using a scheduler
Develop a task that sends a JMS message
Receive scheduler notifications
Manage schedulers


Related


Scheduler interface
Submit a task to a scheduler

+

Search Tips   |   Advanced Search