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)
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"
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.
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 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 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."