4 Replies Latest reply on Jan 30, 2013 11:37 AM by Clebert Suconic

    Produce using Core, consume using JMS

    Diego Lopez Leon Newbie

      Hi there,

      would be possible to produce messages using core and then consume them using JMS? I successfuly produced messages to a jms.queue.* queue from Core, but when I try to consume them from JMS I get

      java.lang.ClassCastException: org.hornetq.jms.client.HornetQMessage cannot be cast to javax.jms.ObjectMessage

      which seems reasonable but anyway, maybe there is some hidden jewel for making it work transparently.

       

      Regards

       

      ps=I'm using 2.2.14

        • 1. Re: Produce using Core, consume using JMS
          Clebert Suconic Master

          I would use CoreMessage on your producer instead of the JMS Message.

           

          The JMS Messages are encapsulating the core-message. The JMs Wrapper will convert them to core-message right before sending.

           

           

          In case you want to send a TextMessage that will be received as a text message, on that case you will have to look at the encoding done on HOrnetQTextMessage, or use HornetQTextMessage directly to create your core message

           

           

          hint: there's a method to expose the core message on HornetQMessage which is what you need.

          • 2. Re: Produce using Core, consume using JMS
            Diego Lopez Leon Newbie

            Clebert Suconic wrote:

             

            I would use CoreMessage on your producer instead of the JMS Message.

            I think I'm producing CoreMessages. This is a snippet of my producer code

             

            ClientMessage message = session.createMessage(true);
            message.getBodyBuffer().writeBytes(toByteArray(messagePayload));
            producer.send(message);
            message.acknowledge();
            

             

            Then I'm consuming those messages in another app, via Spring's JmsTemplate, as an ObjectMessage.

             

            It's worth to mention that producing JMS messages using hornetq-jms-client to that queue works flawessly, but I always try to keep my dependencies to the minumum, that's why I would prefer to produce those messages using core

            • 3. Re: Produce using Core, consume using JMS
              Clebert Suconic Master

              That's what I was referring to the body and content of the message...

               

               

              If you want to be able to cast it to ObjectMessage, you need to oby the protocol the JMS Producer would have to send the message.

               

               

              For starter, you could use createMessag(byte type, boolean durable) signature...

               

               

              You would have to pass the HornetQObjectMessage.TYPE (which is the same to Message.OBJECT_TYPE = 2)...

               

               

              On the body of your message, you would have to write an Integer with the size of the array, and the data from the ObjectOUtputStream:

               

               

               

              Look at setObject:

               

                          ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);

               

                          ObjectOutputStream oos = new ObjectOutputStream(baos);

               

                          oos.writeObject(object);

               

                          oos.flush();

               

                          data = baos.toByteArray();

               

               

               

              And Look at before sen:

               

                       message.getBodyBuffer().writeInt(data.length);

                       message.getBodyBuffer().writeBytes(data);

               

               

              So, on your producer you would have to do this:

               

               

              ClientMessage msg = session.createMessage(Message.OBJECT_TYPE, true);

              byte [] payload = SomeSerializationUtil.covertToByteArray(someObject);

              msg.getBodyBuffer().writeInt(message.length);

              msg.getBodyBuffer().writeBytes(payload);

               

               

              ˆˆ^something like this... notice that I didn't test this code.. consider this as pseudo-english.

              • 4. Re: Produce using Core, consume using JMS
                Clebert Suconic Master

                One thing you could do also is directly instantiate HornetQObjectMessage and use it directly. But there's no guarantee that internal APIs will never change.   If you do the method I wrote on the previous post it should work fine.