JMS Send -Send a soap message instead of String  ?

myjobiscoding
myjobiscoding used Ask the Experts™
on
Hi,

I am googling to find how to send a message over JMS Queue. I get it work. But I need to send a soap message instead of String
===
below is my codes:

package com.osm.jms.queue;
import java.io.FileInputStream;
import java.util.Hashtable;
import javax.naming.*;
import javax.jms.JMSException;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.Queue;
import javax.jms.TextMessage;
import javax.jms.QueueSender;


import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;


import javax.jms.Message;

/* need two jar files
 * wlclient.jar - provide Java EE functionality for Thin Client
 * wljmsclient.jar - provide Java EE and WebLogic JMS functionality
 */

public class JMSSender {
         private static InitialContext ctx = null;
         private static QueueConnectionFactory queueConnectionFactory = null;
         private static QueueConnection qc = null;
         private static QueueSession qsess = null;
         private static Queue q = null;
         private static QueueSender qsndr = null;
         private static TextMessage message = null;
         // NOTE: The next two lines set the name of the Queue Connection Factory
         //       and the Queue that we want to use.
         private static final String QCF_NAME = "javax.jms.QueueConnectionFactory";
         private static final String SOP_QUEUE_NAME = "au.com.telstra.ngbcom.sop.oss";
         public JMSSender() {
             super();
         }
         public static void sendMessage(String messageText) {
             // create InitialContext
             Hashtable<String, String> properties = new Hashtable();
             properties.put(Context.INITIAL_CONTEXT_FACTORY,  "weblogic.jndi.WLInitialContextFactory");
             // NOTE: The port number of the server is provided in the next line,
             //       followed by the userid and password on the next two lines.
             properties.put(Context.PROVIDER_URL, "t3://olbgen.in.telstra.com.au:7002");
             properties.put(Context.SECURITY_PRINCIPAL, "admin");
             properties.put(Context.SECURITY_CREDENTIALS, "dskdsk123");
             try {
                 ctx = new InitialContext(properties);
             } catch (NamingException ne) {
                 ne.printStackTrace(System.err);
                 System.exit(0);
             }
             System.out.println("Got InitialContext " + ctx.toString());
             // create QueueConnectionFactory
             try {
                 queueConnectionFactory = (QueueConnectionFactory)ctx.lookup(QCF_NAME);
             }
             catch (NamingException ne) {
                 ne.printStackTrace(System.err);
                 System.exit(0);
             }
             System.out.println("Got QueueConnectionFactory " + queueConnectionFactory.toString());
             // create QueueConnection
             try {
                 qc = queueConnectionFactory.createQueueConnection();
             }
             catch (JMSException jmse) {
                 jmse.printStackTrace(System.err);
                 System.exit(0);
             }
             System.out.println("Got QueueConnection " + qc.toString());
             // create QueueSession
             try {
                 qsess = qc.createQueueSession(false, 0);
             }
             catch (JMSException jmse) {
                 jmse.printStackTrace(System.err);
                 System.exit(0);
             }
             System.out.println("Got QueueSession " + qsess.toString());
             // lookup Queue
             try {
                 q = (Queue) ctx.lookup(SOP_QUEUE_NAME);
             }
             catch (NamingException ne) {
                 ne.printStackTrace(System.err);
                 System.exit(0);
             }
             System.out.println("Got Queue " + q.toString());
             // create QueueSender
             try {
                 qsndr = qsess.createSender(q);
             }
             catch (JMSException jmse) {
                 jmse.printStackTrace(System.err);
                 System.exit(0);
             }
             System.out.println("Got QueueSender " + qsndr.toString());
            
             // create TextMessage
             try {
                // message = (SOAPMessage) qsess.createTextMessage();
                
                 message = qsess.createTextMessage();
             }
             catch (JMSException jmse) {
                 jmse.printStackTrace(System.err);
                 System.exit(0);
             }
             System.out.println("Got TextMessage " + message.toString());
             // set message text in TextMessage
             try {
                 ((TextMessage) message).setText(messageText);
                
                 // message.setText(messageText);
             }
             catch (JMSException jmse) {
                 jmse.printStackTrace(System.err);
                 System.exit(0);
             }
             System.out.println("Set text in TextMessage " + message.toString());
             // send message
             try {
                //  qsndr.send(message);
              //   qsndr.send(message);
                qsndr.send((Message) message);
             }
             catch (JMSException jmse) {
                 jmse.printStackTrace(System.err);
                 System.exit(0);
             }
             System.out.println("Sent message ");
             // clean up
             try {
                 message = null;
                 qsndr.close();
                 qsndr = null;
                 q = null;
                 qsess.close();
                 qsess = null;
                 qc.close();
                 qc = null;
                 queueConnectionFactory = null;
                 ctx = null;
             }
             catch (JMSException jmse) {
                 jmse.printStackTrace(System.err);
             }
             System.out.println("Cleaned up and done.");
         }
         public static void main(String args[]) {
               String sopMessage = "text";
             sendMessage("test");
         }


}
=================================================
It work find, I need to change from


             // set message text in TextMessage
             try {
                 ((TextMessage) message).setText(messageText);
                
                 // message.setText(messageText);
             }

to ==>

             // Create SOAP Message
              /** Create SOAPMessage request. **/
              // compose a request message
              MessageFactory messageFactory = MessageFactory.newInstance();
              SOAPMessage message = messageFactory.createMessage();

              //Create objects for the message parts
              SOAPPart soapPart = message.getSOAPPart();
              SOAPEnvelope envelope = soapPart.getEnvelope();
              SOAPBody body = envelope.getBody();

              //Populate the Message.  In here, I populate the message from a xml file
              StreamSource preppedMsgSrc = new StreamSource(new FileInputStream("SaleOrder.xml"));
              soapPart.setContent(preppedMsgSrc);

             // create TextMessage
             try {
                // message = (SOAPMessage) qsess.createTextMessage();
                
                 message = qsess.createTextMessage();
             }
             catch (JMSException jmse) {
                 jmse.printStackTrace(System.err);
                 System.exit(0);
             }


the above not working at all. please help...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2018

Commented:
Just do:

try {
                 ((TextMessage) message).setText(  ((SOAPMessage) qsess.createTextMessage()).toString() );
                
                 // message.setText(messageText);
             }

Open in new window

Author

Commented:
Sorry, girionis

I didn't see how to convert the messageText to SOAP message

The messageText contains SOAP messsage format.

your suggestion is

 ((TextMessage) message).setText(  ((SOAPMessage) qsess.createTextMessage()).toString() );

where do I add the messageText to message

please advise
Distinguished Expert 2018

Commented:
My suggestion is how to convert a SOAP message to text, not the other way round. If you want to create a SOAP message from a string you will have to use any other method to do it.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Author

Commented:
hi girionis.

I have already got a soap message. that is exactly what i want.. convert a SOAP message to text.

--
 ((TextMessage) message).setText(((SOAPMessage) qsess.createTextMessage()).toString());

I got the following errors

weblogic.jms.common.TextMessageImpl cannot be cast to javax.xml.soap.SOAPMessage
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
Firstly, you will have 2 variables called message (the JMS TextMessage and the SOAPMessage) if you do it exactly the way you describe above, I have changed the name of the SOAPMessage variable.

// Create SOAP Message
              /** Create SOAPMessage request. **/
              // compose a request message
              MessageFactory messageFactory = MessageFactory.newInstance();
              SOAPMessage soapMessage = messageFactory.createMessage();

              //Create objects for the message parts
              SOAPPart soapPart = soapMessage.getSOAPPart();
              SOAPEnvelope envelope = soapPart.getEnvelope();
              SOAPBody body = envelope.getBody();

              //Populate the Message.  In here, I populate the message from a xml file
              StreamSource preppedMsgSrc = new StreamSource(new FileInputStream("SaleOrder.xml"));
              soapPart.setContent(preppedMsgSrc);

             // create TextMessage
             try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                soapMessage.writeTo(baos);
                message.setText(baos.toString());
             }
             catch (JMSException jmse) {
                 jmse.printStackTrace(System.err);
                 System.exit(0);
             }

Open in new window

Author

Commented:
many thanks. i understand now..

Author

Commented:
Hi mccarl, are you able to help me in java.. i will pay you for your help. sometimes i dont find people here answering my questions straight away.. this is my first job.. i need some help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial