开发调用会话 Bean 的任务

Scheduler API 和 WASScheduler MBean API 支持 TaskInfo 接口的不同实现,每个实现都可以用来调度特定的一类工作。

关于此任务

要创建一个调用 TaskHandler 会话 Bean 中的方法的任务,请执行以下步骤。

过程

  1. 使用 Enterprise JavaBeans (EJB) 模块创建新的企业应用程序。 此应用程序将主管 TaskHandler EJB 模块。
  2. 在 EJB 模块中创建一个无状态会话 Bean,该 Bean 实现了 com.ibm.websphere.scheduler.TaskHandler 远程接口中的 process() 方法。将您要创建的业务逻辑放入 process() 方法中。当运行任务时,将调用 process() 方法。Home 和 Remote 接口在部署描述符 Bean 中必须设置如下:
    • com.ibm.websphere.scheduler.TaskHandlerHome
    • com.ibm.websphere.scheduler.TaskHandler
  3. 通过使用以下示例工厂方法创建 BeanTaskInfo 接口的一个实例。 通过使用 JSP 文件、servlet 或 EJB 组件来创建实例,如以下代码示例所示。此代码必须与先前创建的 TaskHandler EJB 模块位于同一应用程序中:
    // Assume that a scheduler has already been looked-up in JNDI.
    BeanTaskInfo taskInfo = (BeanTaskInfo) scheduler.createTaskInfo(BeanTaskInfo.class)

    您还可以使用 wsadmin 工具创建以下 JACL 脚本编制示例中显示的实例:

    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"
    要点: 创建 BeanTaskInfo 对象并不会将任务添加至持久存储中。它将为必要的数据创建一个占位符。直到调用调度程序中的 create() 方法,才会将任务添加至持久存储中,如“将任务提交给调度程序”主题中所述。
  4. 设置 BeanTaskInfo 对象中的参数。 这些参数定义了调用哪些会话 Bean 以及何时调用它们。TaskInfo 接口包含可用于控制任务执行的各种 set() 方法,其中包括运行任务的时间以及运行任务时它执行的操作。

    BeanTaskInfo 接口要求使用 setTaskHandler 方法设置 TaskHandler Java™ 命名和目录接口 (JNDI) 名称或 TaskHandlerHome。如果使用 WASScheduler MBean API 来设置任务处理程序,那么 JNDI 名称必须是标准的全局 JNDI 名称。

    TaskInfo 接口指定了其他控制点,如 API 文档中所述。使用 TaskInfo 接口 API 方法设置参数,如以下代码示例所示:

    //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);

    您还可以使用以下 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:
    $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."

结果

已创建一个 BeanTaskInfo 对象,它包含调用 EJB 方法的所有相关数据。

下一步做什么

将任务提交给调度程序以进行创建。

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



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