4 Replies Latest reply on Mar 14, 2012 12:45 PM by Piero Giacomelli

    HornetQ core client API with standalone non-clustered server

    Piero Giacomelli Newbie

      Hi all,

       

      maybe I missed something but I went through the examples and I'm not sure how to solve this.

       

      I would like to implement a Message Producer / Message consumer using HornetQ core api.

       

      However the examples I have found use and embedded server to push/read messages.

       

      So I have my HornetQ standalone server running on localhost I need to connect to an existing queue (not to create a new one) and using the HornetQ api send a message and read it.

       

      Could you please post a link to an example or give to lines of code to understand if this possible?

       

      Piero

        • 1. Re: HornetQ core client API with standalone non-clustered server
          Justin Bertram Master

          Connecting to a remote HornetQ server using the "core" API is basically the same as connecting to an embedded server except you (of course) don't start an embedded server and you pass a few different parameters to the server locator factory.  For example:

           

            HashMap<String, Object> connectionParams = new HashMap<String, Object>();

            connectionParams.put(TransportConstants.HOST_PROP_NAME, "remoteHost");

            connectionParams.put(TransportConstants.PORT_PROP_NAME, "5445);

            ServerLocator serverLocator = HornetQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName(), connectionParams));

            ClientSessionFactory sf = serverLocator.createSessionFactory();

            session = sf.createSession(false, true, true);

             ...

          1 of 1 people found this helpful
          • 2. Re: HornetQ core client API with standalone non-clustered server
            Piero Giacomelli Newbie

            Thanks Justin for your reply I was able to manage the connection however the consumerclient gave me an error

             

            the code is this

             



            HashMap map = new HashMap();


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


            map.put("port", 5445);

             

                    TransportConfiguration configuration = new TransportConfiguration(

                            NettyConnectorFactory.class.getName(), map);

                    ServerLocator serverLocator = null;

                    ClientSessionFactory factory = null;

                    ClientSession session = null;

             

                    try {

             

                        serverLocator = HornetQClient.createServerLocatorWithoutHA(configuration);

             

                        factory = serverLocator.createSessionFactory();

                        session = factory.createSession();

                        ClientProducer producer = session.createProducer("compris.channel");

                        System.out.println(producer.isClosed());

                        ClientMessage message = session.createMessage(true);

                        message.getBodyBuffer().writeString("Hello");

                        message.setExpiration(60000)

            ;            System.out.println("message = "

                                + message.getBodyBuffer().readString());

                        producer.send(message);

                       

                        session.start();

                        ClientConsumer consumer = session.createConsumer("compris.channel");

                       

             

                        ClientMessage msgReceived = consumer.receive(100);

                        System.out.println("message = "+ msgReceived.getBodyBuffer().readString());

                        session.close();           

                    } catch (Exception e) {

                        e.printStackTrace();

                    }finally

                    {

                           if (session != null)

                           {

                              try {






            session.close();




            } catch (HornetQException e) {





            // TODO Auto-generated catch block





            e.printStackTrace();




            }

                           }

             

                           if (factory != null)

                           {

                               factory.close();

                           }

                           if(serverLocator != null)

                           {

                               serverLocator.close();

                           }

                     }

             

            I get an errror like this

             

            false

            message = Hello

            HornetQException[errorCode=100 message=Queue compris.channel does not exist]

                      at org.hornetq.core.protocol.core.impl.ChannelImpl.sendBlocking(ChannelImpl.java:286)

                      at org.hornetq.core.client.impl.ClientSessionImpl.internalCreateConsumer(ClientSessionImpl.java:1685)

                      at org.hornetq.core.client.impl.ClientSessionImpl.createConsumer(ClientSessionImpl.java:461)

                      at org.hornetq.core.client.impl.ClientSessionImpl.createConsumer(ClientSessionImpl.java:427)

                      at org.hornetq.core.client.impl.ClientSessionImpl.createConsumer(ClientSessionImpl.java:396)

                      at org.hornetq.core.client.impl.ClientSessionImpl.createConsumer(ClientSessionImpl.java:401)

                      at org.hornetq.core.client.impl.DelegatingSession.createConsumer(DelegatingSession.java:231)

                      at Example1.main(Example1.java:54)

             

            when I try to

            execute

             

                        ClientConsumer consumer = session.createConsumer("compris.channel");

             

            but it seems that the message send method works great

             

            If someone can gave some suggestions

             

            Thks

             

             


            • 3. Re: HornetQ core client API with standalone non-clustered server
              Justin Bertram Master

              Can you confirm that you've created the proper artifacts on the server?  If you send a message to an address (since you always send to an address and never directly to a queue) which doesn't have any queues bound to it the message simply vanishes, but if you try to create a consumer for a queue which doesn't exist you will receive an error.

              • 4. Re: HornetQ core client API with standalone non-clustered server
                Piero Giacomelli Newbie

                Ok so I miss to bound the address to the queue but how can I do this?

                 

                Piero