开发任务以并行运行代码

通过将代码包装在 Callable 或 Runnable 中,然后将其提交或调度至受管执行程序,可以并行执行操作。

开始之前

(可选)您的管理员可以使用管理控制台配置至少一个工作管理器或更改缺省工作管理器的设置。

关于此任务

要并行运行代码,请将代码包装在 Callable 或 Runnable 中,然后将其提交或调度至受管执行程序。

过程

  1. 实现 Callable 或 Runnable 任务。

    任务实现 java.util.concurrent.Callable 接口或 java.lang.Runnable 接口。例如,可以创建动态预订主题和任何组件的任务。

    class SampleTask implements Callable<Object>
    {
       Set<MessageListener> listeners =      
          Collections.newSetFromMap(new ConcurrentHashMap<MessageListener, Boolean>();
       Topic targetTopic;
       TopicConnectionFactory tcf;
    
       public SampleWork(TopicConnectionFactory tcf, Topic targetTopic)
       {
          this.targetTopic = targetTopic;
          this.tcf = tcf;
       }
    
       public void addMessageListener(MessageListener listener) 
       {
          listeners.add(listener);
       }
    
       public Object call() throws JMSException
       {
          // setup our JMS stuff.TopicConnection
          tc = tcf.createConnection();
          try
          {
             TopicSession sess = tc.createSession(false, Session.AUTOACK);
             tc.start();
             while( !Thread.currentThread().isInterrupted() )
             {
                // block for up to 5 seconds.
                Message msg = sess.receiveMessage(5000);
                if( msg != null )
                   for (MessageListener listener : listeners)
                      listener.onMessage(msg);
             }
             tc.close();
          }
          finally
          {
             if (tc != null) tc.close();
          }
       }
    }

    结果,任何组件都可按需添加消息侦听器,这使组件能够通过比仅为每个客户机订户给予其自己的线程更灵活的方法来预订主题。

  2. 确定此应用程序组件需要的工作管理器数。
  3. java:comp 名称空间中使用受管执行程序、线程工厂、上下文服务资源环境引用或工作管理器资源引用(或逻辑名)查找工作管理器。(有关资源环境引用和资源引用的更多信息,请参阅“引用”主题。)
    InitialContext ic = new InitialContext();
    ManagedExecutorService executor = (ManagedExecutorService)ic.lookup("java:comp/env/concurrent/myWorkManager");
    受管执行程序的资源环境引用(在此例中为 concurrent/myWorkManager)必须声明为应用程序部署描述符中的资源环境引用或 @Resource 注释。
  4. 使用 Callable 或 Runnable 任务实例作为参数,调用 ManagedExecutorService.submit() 方法。 例如:
    Callable<Boolean> task = new MyTask(...);
    Future<Boolean> future = executor.submit(task);

    Future 是一个句柄,用于提供从组件到所提交任务的链接。

  5. 可选: 如果应用程序组件需要等待一个或多个其任务完成,请调用 Future.get() 方法。 例如:
    Future<String> futureA = executor.submit(taskA);
    Future<String> futureB = executor.submit(taskB);
    futureA.get(5, TimeUnit.SECONDS);
    futureB.get(5, TimeUnit.SECONDS);

    或者,使用 invokeAll() 方法来提交多个任务并等待所有任务完成。例如:

    tasks = Arrays.asList(taskA, taskB);
    futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS);
        // we can check future.isDone() to see which, if any, finished.

    此方法采用一系列任务,您的组件要在这些任务中等待所有工作对象完成。您还可指定超时值。

  6. 使用 Future.cancel(true) 方法尝试中断任务。

    实现任务以对立即中断进行响应,或尝试尽快停止运行。


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



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