6 Replies Latest reply on Nov 30, 2005 2:21 PM by crazytony

    how to send XML Message using JMS

    rohit_mca2000

      Hello friends,
      I need to know , how do we send XML Messages usins JMS message service.
      I found that we can send it using TextMessage but for this i will have to manually make a string out of my XML some what in this fashion.


      StringBuffer buffer = new StringBuffer();
       buffer.append("<?xml version='1.0'?>\n");
       buffer.append("<UserProfiles>\n");
       buffer.append("<User>\n");
       buffer.append("<Action>Create</Action>\n");
       buffer.append("<Username>"+username+"</Username>\n");
       buffer.append("<FirstName>"+firstName+"</FirstName>\n");
       buffer.append("<LastName>"+lastName+"</FirstName>\n");
       buffer.append("<Email>"+email+"</Email>\n");
       buffer.append("</User>\n");
       buffer.append("</UserProfiles>");





      But i dont want to do these. I will create XML using DOM , so that means my entire XML would be inside Document object, So is there any way of converting this Document object to String so that i can send it using TextMessage. thanks.

        • 1. Re: how to send XML Message using JMS
          genman


          I would recommend passing XML not as text but use the ObjectMessage format.

          Since Xerces (probably what you use) uses serializable org.w3c.dom objects, just set the message to use the Document object.

          • 2. Re: how to send XML Message using JMS
            crazytony

            Watch your stack size if you are serializing a DOM object.

            If you use the JBoss recommended stack size of 128k (http://wiki.jboss.org/wiki/Wiki.jsp?page=OutOfMemoryExceptionWhenCannotCreateThread) you may run into problems serializing large DOM nodes.
            (StackOverflowException)

            • 3. Re: how to send XML Message using JMS
              rohit_mca2000

              i have done it this way.

              document.appendChild(rootElement);
               TransformerFactory tFactory = TransformerFactory.newInstance();
               Transformer transformer = tFactory.newTransformer();
               DOMSource source = new DOMSource(document);
               StringWriter buffer = new StringWriter();
               StreamResult result = new StreamResult(buffer);
               transformer.transform(source, result);
               xmlString = buffer.toString();

              where document is DOcument object of DOM , and passing this xmlString in the TestMessage body .
              Is this wrong way of doing it.

              and crazytony please also tell me what was that stack thing and outofmemory thing you were talkin about. since my Document object is going to be large.

              and genman i will try ObjectMessage and let you know, but i had read in the specs that XML messages can be send using TextMessages , so did that
              thanks ,

              • 4. Re: how to send XML Message using JMS
                crazytony

                genman was saying that you should just send the whole DOM tree (put the parent Node in an ObjectMessage).

                Just watch out if you change the stack size because DOM is a deeply recursive structure and the default java serialization is also very recursive.

                Basically, what he was saying was: session.createObjectMessage( rootElement );

                We've found that this will blow the 128k stack about 1/2 of the time (for our XML, YMMV). We've actually adopted a similar idea to you, send the string and parse it on the other side (message receiver)

                • 5. Re: how to send XML Message using JMS
                  genman

                  It does depend on the XML tree depth. If you use something like JDom or Dom4J as your object model, you might get different allowable tree depths.

                  XML serialization/parsing is inefficient compared to Java serialization.

                  • 6. Re: how to send XML Message using JMS
                    crazytony

                    Genman,
                    Agreed, an additional serialize/parse is a bad thing. I was just pointing out a pitfall which caused us quite a headache.

                    In an ideal world, we wouldn't need to change the stack size to fix a problem with thread creation and this would all be moot.

                    We didn't need the node on the sender side of the message (it came from an XML HTTP request) so there was no additional overhead involved in sending it as a string.