5 Replies Latest reply on Dec 4, 2011 11:09 PM by dan wang

    Why TextMessage double size?

    dan wang Newbie

      Hi, All,

       

      I send one TextMessage to ExampleQueue,but when I capture the message sent using Wireshark, I found the message size is doubled.

       

      I create message which size is 1K, but capture two packet to send it, first size is 1460, second is 709.

       

      Same case not happend in ByteMessage, which only one packet to send and the size is 1140.

       

      And I using hornetQ 2.2.5.

       

      Is it a bug ?

        • 1. Re: Why TextMessage double size?
          Leos Bitto Novice

          TextMessage in JMS is basically a Message which contains a String. Are you aware of the fact that Java's String uses UTF-16, which requires 16 bits (2 bytes) to store each letter?

          • 2. Re: Why TextMessage double size?
            Clebert Suconic Master

            Leos Bitto wrote:

             

            TextMessage in JMS is basically a Message which contains a String. Are you aware of the fact that Java's String uses UTF-16, which requires 16 bits (2 bytes) to store each letter?

            +1000

            • 3. Re: Why TextMessage double size?
              dan wang Newbie

              Yes, you are correct about 2 bytes to store each letter for Java.

               

              But I test same case using other MQ, I find only one packet is sent and the size is not doubled either.

               

              And I test String#getBytes methods, when test String is "abc", it returns only 3 bytes. When test String is some Chinese letter, it returns 6 bytes.

               

              I check the source code of SimpleString, it always stores 2 bytes for each letter, I think it's the diffence.

              • 4. Re: Why TextMessage double size?
                Leos Bitto Novice

                dan wang wrote:

                 

                Yes, you are correct about 2 bytes to store each letter for Java.

                 

                But I test same case using other MQ, I find only one packet is sent and the size is not doubled either.

                 

                This is an implementation detail. JMS does not specify the format of the packets between the client and the server. Maybe that other JMS providers have some optimalizations, for example by using another encoding than UTF-16 which they guess would be more efficient. There would be a tradeoff when extracting the String from the TextMessage, because reencoding into UTF-16 would have to happen.

                 

                And I test String#getBytes methods, when test String is "abc", it returns only 3 bytes. When test String is some Chinese letter, it returns 6 bytes.

                 

                See http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#getBytes() : "Encodes this String into a sequence of bytes using the platform's default charset" - so I would guess that your platform's default charet is UTF-8. Because of this platform dependency I always avoid getBytes() and use getBytes(explicit charset) instead.

                 

                If you would like to always use some explicit charset encoding in the packets sent between the clients and the server, I would suggest to use BytesMessage and stuff it with your bytes by calling writeBytes(byte[]).

                • 5. Re: Why TextMessage double size?
                  dan wang Newbie

                  Thank you for your reply!!!!!! And you are correct about my platform's charset too.

                   

                  I check ActiveMQ source code, and find "text = MarshallingSupport.readUTF8(dataIn);" in ActiveMQTextMessage.

                   

                  I think it's a tradeoff in SimpleString for this class is design for performance.

                   

                  And I decide to use ByteMessage instead.