例: Callable タスクおよび Runnable タスクの作成

トピックに動的にサブスクライブするタスクを作成することができ、 任意のコンポーネントはメッセージ・リスナーをオンデマンドで追加できます。

以下に、トピックに動的にサブスクライブするタスクの例を示します。
class SampleTask implements Callable<Object>
{
   Set<MessageListener> listeners =
       Collections.newSetFromMap(new ConcurrentHashMap<MessageListener, Boolean>);
   	Topic targetTopic;
   	TopicConnectionFactory tcf;

   public SampleTask(TopicConnectionFactory tcf, Topic targetTopic)
   {
      		this.targetTopic = targetTopic;
      		this.tcf = tcf;
   }

   public void addMessageListener(MessageListener listener)
   {
      listeners.add(listener);
   }

   public Object call() throws JMSException
   {
      // Set up JMS.
      		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)
                  // Fire an event when we get a message.
                  listener.onMessage(msg);
            }
         }
         tc.close();
      }
      finally
      {
         if (tc != null) tc.close();
      }
      return null;
   }
}

この結果、どのコンポーネントもメッセージ・リスナーをオンデマンドで追加でき、これにより、 コンポーネントは、単に各クライアント・サブスクライバーに自身のスレッドを与えるよりもスケーラブルな方法でトピックにサブスクライブすることができます。


トピックのタイプを示すアイコン 参照トピック



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=xasb_workobject
ファイル名:xasb_workobject.html