6 Replies Latest reply on Dec 2, 2011 2:15 PM by Abbas Raza

    Is there a ClientSubscriber like a ClientConsumer?

    Abbas Raza Newbie

      Hi,

       

      Is there a way to create a subscriber similar to ClientConsumer? The subscriber would implement something like MessageHandler and gets called when a message is posted on the topic. Right now, I see that only ClientConsumer is created in a MessageHandler implementation. This client consumer listens only for queues. I need something like this for topics. Is there a way to do this or is it a new feature request?

       

      Thanks!

        • 1. Re: Is there a ClientSubscriber like a ClientConsumer?
          Yong Hao Gao Master

          With HornetQ you just need the same ClientConsumer for topics. In HornetQ core there isn't a concept of 'topics'. To achieve a 'topic' you just need to create multiple queues bound to one same address.

           

          Howard

          • 2. Re: Is there a ClientSubscriber like a ClientConsumer?
            Abbas Raza Newbie

            I am using JMS components within HornetQ. So, I am not sure how the suggested solution would work. Can you please elaborate? Thanks.

            • 3. Re: Is there a ClientSubscriber like a ClientConsumer?
              Clebert Suconic Master

              I didn't get what's your question.. if you are using JMS ,you can just use MessageListener within a Durable subscription:

               

                    Session sess = ...;

                    TopicSubscriber x = sess.createDurableSubscriber(null,null);

                   

                    x.setMessageListener(listener)


              • 4. Re: Is there a ClientSubscriber like a ClientConsumer?
                Abbas Raza Newbie

                Based on your suggestion, I created the following sample. It is getting registered but the onMessage method is not getting called when a message is published on the topic. Anything I am missing here?

                 

                import java.util.HashMap;

                import java.util.Map;

                 

                import javax.jms.Connection;

                import javax.jms.JMSException;

                import javax.jms.Message;

                import javax.jms.MessageListener;

                import javax.jms.Session;

                import javax.jms.TopicSubscriber;

                 

                import org.hornetq.api.core.TransportConfiguration;

                import org.hornetq.api.jms.HornetQJMSClient;

                import org.hornetq.api.jms.JMSFactoryType;

                import org.hornetq.core.remoting.impl.netty.NettyConnectorFactory;

                import org.hornetq.jms.client.HornetQConnectionFactory;

                 

                public class MyMessageHandler implements MessageListener {

                 

                  public MyMessageHandler() {

                    registerHandler();

                  }

                 

                  private void registerHandler() {

                    System.out.println("hey registering the handler");

                    Map<String, Object> map = new HashMap<String, Object>();

                    map.put("host", "localhost");

                    map.put("port", 5445);

                   

                    TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(), map);

                    HornetQConnectionFactory connectionFactory = HornetQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF,

                        transportConfiguration);

                    try {

                      Connection jmsConnection = connectionFactory.createConnection("fun", "fun");

                      jmsConnection.setClientID("1000");

                      Session session = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

                      TopicSubscriber topicSubscriber = session.createDurableSubscriber(

                          HornetQJMSClient.createTopic("test"), "test");

                      topicSubscriber.setMessageListener(this);

                      System.out.println("registered the handler");

                    } catch (JMSException e) {

                      e.printStackTrace();

                    }

                 

                  }

                 

                  @Override

                  public void onMessage(Message message) {

                    System.out.println("hey got the message");

                  }

                }

                 

                 

                I am getting it registered by having it configured as a bean in hornetq-beans.xml.

                     <bean name="MyMessageHandler" class="MyMessageHandler">

                      <start ignored="false"/>

                      <stop ignored="false"/>

                      <depends>JMSServerManager</depends>

                   </bean>

                • 5. Re: Is there a ClientSubscriber like a ClientConsumer?
                  Andy Taylor Master

                  you need to start your connection to receive messages, i would recommend reading a JMS tutorial, the Sun one is pretty good

                  1 of 1 people found this helpful
                  • 6. Re: Is there a ClientSubscriber like a ClientConsumer?
                    Abbas Raza Newbie

                    Duh. Just needed an extra pair of eyes I guess working late in the day. Missed that simple obvious one.