4 Replies Latest reply on Mar 20, 2014 11:52 AM by jgabrielygalan

    Empty body consuming messages via Stomp

    jgabrielygalan

      Hi,

       

      I am trying to setup the following: I have a producer in Java using the Core API, and I'd like to have a Stomp client in Ruby, to have the logstash stomp plugin consuming from HornetQ. Logstash has a Stomp plugin that uses the OnStomp ruby client.

      This is the relevant code of the producer:

       

              Map<String, Object> props = newHashMap();
              props.put("host", "localhost");
              props.put("port", "5445");
              ServerLocator serverLocator = HornetQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName(), props));
              ClientSessionFactory sf = null;
              ClientSession session = null;
              ClientProducer producer = null;
              try {
                  sf = serverLocator.createSessionFactory();
                  session = sf.createSession();
                  producer = session.createProducer("testaddress");
                  ClientMessage message = session.createMessage(true);
                  message.putStringProperty("type", "testLog");
                  message.getBodyBuffer().writeString("test from java");
                  producer.send(message);

              } finally {
                  producer.close();
                  session.close();
                  sf.close();
                  serverLocator.close();
              }

       

      And here is the consumer:

       

      require 'onstomp'

       

      client = OnStomp::Client.new("stomp://localhost:5445", :login => "guest", :passcode => "guest")
      client.connect
      client.subscribe("test") do |msg|
          p msg
          puts "Body: #{msg.body}"
      end

       

      gets
      client.close

       

      I run the producer to generate 1 message in the queue. Then I run the client:

       

      #<OnStomp::Components::Frame:0x0000000185af38 @command="MESSAGE", @headers=#<OnStomp::Components::FrameHeaders:0x0000000185aee8 @values={:subscription=>["1"], :"message-id"=>["988093"], :destination=>["testaddress"], :expires=>["0"], :redelivered=>["false"], :priority=>["4"], :timestamp=>["1395163541751"], :type=>["testLog"]}>, @body="">
      Body:

       

      As you can see the body is empty. Trying with the Stomp ruby client, instead of OnStomp, I get a little bit more info:

       

      #<Stomp::Message:0x000000012ae210 @command="MESSAGE", @headers={"subscription"=>"a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", "message-id"=>"988082", "destination"=>"testaddress", "expires"=>"0", "redelivered"=>"false", "priority"=>"4", "timestamp"=>"1395163068430", "type"=>"testLog"}, @body="", @original="MESSAGE\nsubscription:a94a8fe5ccb19ba61c4c0873d391e987982fbbd3\nmessage-id:988082\ndestination:testaddress\nexpires:0\nredelivered:false\npriority:4\ntimestamp:1395163068430\ntype:testLog\n\n\u0000">

       

      As you see there's a \u0000 after the Stomp headers.

       

      So, is there a way to have the body correctly generated from HornetQ Core API to the Stomp interface? Am i doing anything wrong?

       

      Thanks,

       

      Jesus.

        • 1. Re: Empty body consuming messages via Stomp
          gaohoward

          Hi,

           

          I think you need to create a 'text' type of core message for sending.

           

          Howard

          • 2. Re: Empty body consuming messages via Stomp
            jgabrielygalan

            Hi,

             

            Thanks for your answer. Unfortunately the result is the same:

            Changes to the above code:

            ClientMessage message = session.createMessage(Message.TEXT_TYPE, true);

             

            #<Stomp::Message:0x00000001746458 @command="MESSAGE", @headers={"subscription"=>"a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", "message-id"=>"988146", "destination"=>"testaddress", "expires"=>"0", "redelivered"=>"false", "priority"=>"4", "timestamp"=>"1395308072211"}, @body="", @original="MESSAGE\nsubscription:a94a8fe5ccb19ba61c4c0873d391e987982fbbd3\nmessage-id:988146\ndestination:testaddress\nexpires:0\nredelivered:false\npriority:4\ntimestamp:1395308072211\n\n\u0000">

             

            Still body is empty. I don't know exactly what it means for HornetQ that the message is of type TEXT, but in my real use case I want to pass a byte[], my body is going to be an object serialized with protobuf.

             

            Jesus.

            • 3. Re: Empty body consuming messages via Stomp
              gaohoward

              I'm sorry about that. The type actually doesn't matter. It's that you have to use this API:

               

              msg.getBodyBuffer().writeNullableSimpleString(new SimpleString("hello world!"));

               

              to get it work, or use bytes:

               

              ClientMessage msg = coreSession.createMessage(Message.BYTES_TYPE, true);

              msg.getBodyBuffer().writeBytes(m.getBytes("UTF-8"));

               

               

              Howard

              • 4. Re: Empty body consuming messages via Stomp
                jgabrielygalan

                Thanks,

                 

                It worked like a charm. I have been able to send java Strings using the writeNullableSimpleString, and also a protobuf serialized object using the createMessage(Message.BYTES_TYPE).

                 

                Jesus.