troubleshooting Question

JMS Send -Send a soap message instead of String ?

Avatar of myjobiscoding
myjobiscoding asked on
Programming Languages-OtherJava
7 Comments1 Solution1129 ViewsLast Modified:
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...
ASKER CERTIFIED SOLUTION
mccarl
IT Business Systems Analyst / Software Developer
Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros