Develop a task that sends a Java Message Service message

 

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 sends a Java Message Service (JMS) message to a queue or topic.

 

Procedure

  1. Create an instance of the MessageTaskInfo interface using the Scheduler.createTaskInfo() factory method. Using a JavaServer Pages (JSP) file, servlet or EJB container, create the instance as shown in the following code example

    //lookup the scheduler to be used
    Scheduler scheduler = (Scheduler)new InitialContext.lookup("java:comp/env/Scheduler");
    
    MessageTaskInfo taskInfo = (MessageTaskInfo) scheduler.createTaskInfo(MessageTaskInfo.class);
    
    
    We can also use the wsadmin tool, create the instance as shown in the following JACL scripting example

    # Sample create a task using MessageTaskInfo task type
    # Call this mbean with the following parameters:
    #    <scheduler jndiName>     = JNDI name of the scheduler resource, 
    #                               for example scheduler/myScheduler
    #    <JNDI name of the QCF>   = The global JNDI name of the Queue Connection Factory.
    #    <JNDI name of the Queue> = The global JNDI name of the Queue destination
    
    set jndiName [lindex $argv 0]
    set jndiName_QCF [lindex $argv 1]
    set jndiName_Q [lindex $argv 2]
    
    # 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 MessageTaskInfo object using invoke_jmx
    puts "Creating MessageTaskInfo"
    set params [java::new {java.lang.Object[]} 1]
    $params set 0 [java::field com.ibm.websphere.scheduler.MessageTaskInfo 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 mti [java::cast com.ibm.websphere.scheduler.MessageTaskInfo  $ti]
    puts "Created the MessageTaskInfo object: $mti"
    
    

    Note: Creating a MessageTaskInfo 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 a task to a scheduler.

  2. Set parameters on the MessageTaskInfo object. The TaskInfo interface contains various set() methods that can be used to control execution of the task, including when the task runs and what work the task does when it starts.

    The TaskInfo interface specifies additional behavior settings, as documented in the API documentation. Using a JavaServer Pages (JSP) file, servlet or EJB container, create the instance 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);
    
    
    //now set the start time and the JNDI names for the queue connection factory and the queue
    taskInfo.setConnectionFactoryJndiName("jms/MyQueueConnectionFactory");
    taskInfo.setDestination("jms/MyQueue");
    taskInfo.setStartTime(startDate);
    
    We can also use the wsadmin tool, to create the instance as shown in 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:
    $mti setStartTime [java::new {java.util.Date long} [java::call System currentTimeMillis]]
    
    # Set the StartTimeInterval so the task runs in 30 seconds from now
    $mti setStartTimeInterval 30seconds
    
    # Set the global JNDI name of the QCF & Queue to send the message to.
    $mti setConnectionFactoryJndiName $jndiName_QCF
    $mti setDestinationJndiName $jndiName_Q
    
    # Set the message
    $mti setMessageData "Test Message"
    
    # Do not purge the task when it's complete
    $mti setAutoPurge false
    
    # Set the name of the task.  This can be any string value.
    $mti 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.
    # $mti setAuthenticationAlias {myRealm/myAlias}
    
    puts "Task setup completed."
    
    

 

Result

A MessageTaskInfo object has been created that contains all of the relevant data for a task that sends a JMS message.

 

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 calls a session bean
Receiving scheduler notifications
Manage tasks with a scheduler
Manage schedulers

 

See Also


Scheduler interface