Ejemplo: creación de tareas Callable y Runnable

Puede crear una tarea que se suscriba dinámicamente a un tema y cualquier componente puede añadir un escucha de mensajes a petición.

El siguiente es un ejemplo de un a tarea que se suscribe dinámicamente a un tema:
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
   {
      // Configurar JMS.
      TopicConnection tc = tcf.createConnection();
      try
      {
         TopicSession sess = tc.createSession(false, Session.AUTOACK);
         tc.start();

         while( !Thread.currentThread().isInterrupted() )
         {
            // Bloquear hasta 5 segundos.
            Message msg = sess.receiveMessage(5000);
            if(msg != null)
            {
               for (MessageListener listener : listeners)
                  // Activar un suceso cuando se obtenga un mensaje.
                  listener.onMessage(msg);
            }
         }
         tc.close();
      }
      finally
      {
         if (tc != null) tc.close();
      }
      return null;
   }
}

Como resultado, todo componente puede añadir un escucha de mensajes a petición, lo que permite a los componentes suscribirse a un tema de forma más escalable que si se da a cada suscriptor del cliente su propia hebra.


Icon that indicates the type of topic Reference topic



Timestamp icon Last updated: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=xasb_workobject
File name: xasb_workobject.html