开发发送 Java 消息服务消息的任务

创建用于将 Java™ 消息服务 (JMS) 消息发送到队列或主题的任务。Scheduler API 和 WASScheduler MBean API 支持 TaskInfo 接口的不同实现,每个实现都可以用来调度特定的一类工作。

关于此任务

要创建将 Java 消息服务 (JMS) 消息发送到队列或主题的任务,请使用以下步骤。

过程

  1. 使用 Scheduler.createTaskInfo() 工厂方法创建 MessageTaskInfo 接口的一个实例。 使用 JSP 文件、servlet 或 EJB 容器创建实例,如以下代码示例所示:
    //lookup the scheduler to be used
    Scheduler scheduler = (Scheduler)new InitialContext.lookup("java:comp/env/Scheduler");
    
    MessageTaskInfo taskInfo = (MessageTaskInfo) scheduler.createTaskInfo(MessageTaskInfo.class);
    还可以使用 wsadmin 工具,如以下 JACL 脚本编制示例中所示创建实例:# 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"
    注意: 创建 MessageTaskInfo 对象并不会将任务添加到持久存储中。它将为必要的数据创建一个占位符。直到调用调度程序中的 create() 方法,才会将任务添加到持久存储中,如“将任务提交给调度程序”主题中所述。
  2. 设置 MessageTaskInfo 对象中的参数。 TaskInfo 接口包含可用于控制任务执行的各种 set() 方法,其中包括运行任务的时间以及启动任务时它执行的操作。

    TaskInfo 接口指定了其他行为设置,如 API 文档中所述。使用 JSP 文件、servlet 或 EJB 容器创建实例,如以下代码示例所示:

    //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);
    您还可以使用 wsadmin 工具创建以下 JACL 脚本编制示例中所示的实例:
    # 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 you can set the tasks Authentication Alias
    # Authentication aliases are created using the Admin Console.
    # $mti setAuthenticationAlias {myRealm/myAlias}
    
    puts "Task setup completed."

结果

已创建一个 MessageTaskInfo 对象,它包含发送 JMS 消息的任务的所有相关数据。

下一步做什么

将任务提交给调度程序进行创建,如“将任务提交给调度程序”主题中所述。

指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tsch_schedulemtask
文件名:tsch_schedulemtask.html