Why and when to perform this task
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
Steps for this task
// Assume that a scheduler has already been looked-up in JNDI. BeanTaskInfo taskInfo = (BeanTaskInfo) scheduler.createTaskInfo(BeanTaskInfo.class)You 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"
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);You 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."
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
Managing tasks with a scheduler
Managing schedulers
Related reference
Scheduler interface