myjobiscoding
asked on
JMS Send -Send a soap message instead of String ?
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.QueueConnectionF actory;
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.MessageFact ory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelop e;
import javax.xml.soap.SOAPMessage ;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.stream .StreamSou rce;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBind ing;
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.QueueConnection Factory";
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.INI TIAL_CONTE XT_FACTORY , "weblogic.jndi.WLInitialCo ntextFacto ry");
// 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.PRO VIDER_URL, "t3://olbgen.in.telstra.co m.au:7002" );
properties.put(Context.SEC URITY_PRIN CIPAL, "admin");
properties.put(Context.SEC URITY_CRED ENTIALS, "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)ct x.lookup(Q CF_NAME);
}
catch (NamingException ne) {
ne.printStackTrace(System. err);
System.exit(0);
}
System.out.println("Got QueueConnectionFactory " + queueConnectionFactory.toS tring());
// create QueueConnection
try {
qc = queueConnectionFactory.cre ateQueueCo nnection() ;
}
catch (JMSException jmse) {
jmse.printStackTrace(Syste m.err);
System.exit(0);
}
System.out.println("Got QueueConnection " + qc.toString());
// create QueueSession
try {
qsess = qc.createQueueSession(fals e, 0);
}
catch (JMSException jmse) {
jmse.printStackTrace(Syste m.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(Syste m.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(Syste m.err);
System.exit(0);
}
System.out.println("Got TextMessage " + message.toString());
// set message text in TextMessage
try {
((TextMessage) message).setText(messageTe xt);
// message.setText(messageTex t);
}
catch (JMSException jmse) {
jmse.printStackTrace(Syste m.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(Syste m.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(Syste m.err);
}
System.out.println("Cleane d 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(messageTe xt);
// message.setText(messageTex t);
}
to ==>
// Create SOAP Message
/** Create SOAPMessage request. **/
// compose a request message
MessageFactory messageFactory = MessageFactory.newInstance ();
SOAPMessage message = messageFactory.createMessa ge();
//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(preppe dMsgSrc);
// create TextMessage
try {
// message = (SOAPMessage) qsess.createTextMessage();
message = qsess.createTextMessage();
}
catch (JMSException jmse) {
jmse.printStackTrace(Syste m.err);
System.exit(0);
}
the above not working at all. please help...
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.QueueConnectionF
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.MessageFact
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelop
import javax.xml.soap.SOAPMessage
import javax.xml.soap.SOAPPart;
import javax.xml.transform.stream
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBind
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.QueueConnection
private static final String SOP_QUEUE_NAME = "au.com.telstra.ngbcom.sop
public JMSSender() {
super();
}
public static void sendMessage(String messageText) {
// create InitialContext
Hashtable<String, String> properties = new Hashtable();
properties.put(Context.INI
// 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.PRO
properties.put(Context.SEC
properties.put(Context.SEC
try {
ctx = new InitialContext(properties)
} catch (NamingException ne) {
ne.printStackTrace(System.
System.exit(0);
}
System.out.println("Got InitialContext " + ctx.toString());
// create QueueConnectionFactory
try {
queueConnectionFactory = (QueueConnectionFactory)ct
}
catch (NamingException ne) {
ne.printStackTrace(System.
System.exit(0);
}
System.out.println("Got QueueConnectionFactory " + queueConnectionFactory.toS
// create QueueConnection
try {
qc = queueConnectionFactory.cre
}
catch (JMSException jmse) {
jmse.printStackTrace(Syste
System.exit(0);
}
System.out.println("Got QueueConnection " + qc.toString());
// create QueueSession
try {
qsess = qc.createQueueSession(fals
}
catch (JMSException jmse) {
jmse.printStackTrace(Syste
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.
System.exit(0);
}
System.out.println("Got Queue " + q.toString());
// create QueueSender
try {
qsndr = qsess.createSender(q);
}
catch (JMSException jmse) {
jmse.printStackTrace(Syste
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(Syste
System.exit(0);
}
System.out.println("Got TextMessage " + message.toString());
// set message text in TextMessage
try {
((TextMessage) message).setText(messageTe
// message.setText(messageTex
}
catch (JMSException jmse) {
jmse.printStackTrace(Syste
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(Syste
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(Syste
}
System.out.println("Cleane
}
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(messageTe
// message.setText(messageTex
}
to ==>
// Create SOAP Message
/** Create SOAPMessage request. **/
// compose a request message
MessageFactory messageFactory = MessageFactory.newInstance
SOAPMessage message = messageFactory.createMessa
//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
soapPart.setContent(preppe
// create TextMessage
try {
// message = (SOAPMessage) qsess.createTextMessage();
message = qsess.createTextMessage();
}
catch (JMSException jmse) {
jmse.printStackTrace(Syste
System.exit(0);
}
the above not working at all. please help...
ASKER
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
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())
where do I add the messageText to message
please advise
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.
ASKER
hi girionis.
I have already got a soap message. that is exactly what i want.. convert a SOAP message to text.
--
((TextMessage) message).setText(((SOAPMes sage) qsess.createTextMessage()) .toString( ));
I got the following errors
weblogic.jms.common.TextMe ssageImpl cannot be cast to javax.xml.soap.SOAPMessage
I have already got a soap message. that is exactly what i want.. convert a SOAP message to text.
--
((TextMessage) message).setText(((SOAPMes
I got the following errors
weblogic.jms.common.TextMe
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
many thanks. i understand now..
ASKER
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
Open in new window