This section includes and describes two code samples: one that sends a JMS message with a SOAP payload, and another that receives the JMS/SOAP message and processes the SOAP message.
Example 5–5 illustrates the use of the JMS API, the SAAJ API, and the JAF API to send a SOAP message with attachments as the payload to a JMS message. The code shown for the SendSOAPMessageWithJMS includes the following methods:
A constructor that calls the init method to initialize all the JMS objects required to publish a message
A send method that creates the SOAP message and an attachment, converts the SOAP message into a JMS message, and publishes the JMS message
A close method that closes the connection
A main method that calls the send and close methods
//Libraries needed to build SOAP message
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.Name
//Libraries needed to work with attachments (Java Activation Framework API)
import java.net.URL;
import javax.activation.DataHandler;
//Libraries needed to convert the SOAP message to a JMS message and to send it
import com.sun.messaging.xml.MessageTransformer;
import com.sun.messaging.BasicConnectionFactory;
//Libraries needed to set up a JMS connection and to send a message
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.Message;
import javax.jms.TopicSession;
import javax.jms.Topic;
import javax.jms.TopicPublisher;
//Define class that sends JMS message with SOAP payload
public class SendSOAPMessageWithJMS{
TopicConnectionFactory tcf = null;
TopicConnection tc = null;
TopicSession session = null;
Topic topic = null;
TopicPublisher publisher = null;
//default constructor method
public SendSOAPMessageWithJMS(String topicName){
init(topicName);
}
//Method to nitialize JMS Connection, Session, Topic, and Publisher
public void init(String topicName) {
try {
tcf = new com.sun.messaging.TopicConnectionFactory();
tc = tcf.createTopicConnection();
session = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = session.createTopic(topicName);
publisher = session.createPublisher(topic);
}
//Method to create and send the SOAP/JMS message
public void send() throws Exception{
MessageFactory mf = MessageFactory.newInstance(); //create default factory
SOAPMessage soapMessage=mfcreateMessage(); //create SOAP message object
SOAPPart soapPart = soapMessage.getSOAPPart();//start to drill down to body
SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); //first the envelope
SOAPBody soapBody = soapEnvelope.getBody();
Name myName = soapEnvelope.createName("HelloWorld", "hw",
http://www.sun.com/imq’);
//name for body element
SOAPElement element = soapBody.addChildElement(myName); //add body element
element.addTextNode("Welcome to SUnOne Web Services."); //add text value
//Create an attachment with the Java Framework Activation API
URL url = new URL("http://java.sun.com/webservices/");
DataHandler dh = new DataHnadler (url);
AttachmentPart ap = soapMessage.createAttachmentPart(dh);
//Set content type and ID
ap.setContentType("text/html");
ap.setContentID(’cid-001");
//Add attachment to the SOAP message
soapMessage.addAttachmentPart(ap);
soapMessage.saveChanges();
//Convert SOAP to JMS message.
Message m = MessageTransformer.SOAPMessageIntoJMSMessage
(soapMessage,session);
//Publish JMS message
publisher.publish(m);
//Close JMS connection
public void close() throws JMSException {
tc.close();
}
//Main program to send SOAP message with JMS
public static void main (String[] args) {
try {
String topicName = System.getProperty("TopicName");
if(topicName == null) {
topicName = "test";
}
SendSOAPMEssageWithJMS ssm = new SendSOAPMEssageWithJMS(topicName);
ssm.send();
ssm.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Example 5–6 illustrates the use of the JMS API, SAAJ, and the DOM API to receive a SOAP message with attachments as the payload to a JMS message. The code shown for the ReceiveSOAPMessageWithJMS includes the following methods:
A constructor that calls the init method to initialize all the JMS objects needed to receive a message.
An onMessage method that delivers the message and which is called by the listener. The onMessage method also calls the message transformer utility to convert the JMS message into a SOAP message and then uses SAAJ to process the SOAP body and uses SAAJ and the DOM API to process the message attachments.
A main method that initializes the ReceiveSOAPMessageWithJMS class.
//Libraries that support SOAP processing
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.AttachmentPart
//Library containing the JMS to SOAP transformer
import com.sun.messaging.xml.MessageTransformer;
//Libraries for JMS messaging support
import com.sun.messaging.TopicConnectionFactory
//Interfaces for JMS messaging
import javax.jms.MessageListener;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.JMSException;
import javax.jms.TopicSubscriber
//Library to support parsing attachment part (from DOM API)
import java.util.iterator;
public class ReceiveSOAPMessageWithJMS implements MessageListener{
TopicConnectionFactory tcf = null;
TopicConnection tc = null;
TopicSession session = null;
Topic topic = null;
TopicSubscriber subscriber = null;
MessageFactory messageFactory = null;
//Default constructor
public ReceiveSOAPMessageWithJMS(String topicName) {
init(topicName);
}
//Set up JMS connection and related objects
public void init(String topicName){
try {
//Construct default SOAP message factory
messageFactory = MessageFactory.newInstance();
//JMS set up
tcf = new. com.sun.messaging.TopicConnectionFactory();
tc = tcf.createTopicConnection();
session = tc.createTopicSesstion(false, Session.AUTO_ACKNOWLEDGE);
topic = session.createTopic(topicName);
subscriber = session.createSubscriber(topic);
subscriber.setMessageListener(this);
tc.start();
System.out.println("ready to receive SOAP m essages...");
}catch (Exception jmse){
jmse.printStackTrace();
}
}
//JMS messages are delivered to the onMessage method
public void onMessage(Message message){
try {
//Convert JMS to SOAP message
SOAPMessage soapMessage = MessageTransformer.SOAPMessageFromJMSMessage
(message, messageFactory);
//Print attchment counts
System.out.println("message received! Attachment counts:
" + soapMessage.countAttachments());
//Get attachment parts of the SOAP message
Iterator iterator = soapMessage.getAttachments();
while (iterator.hasNext()) {
//Get next attachment
AttachmentPart ap = (AttachmentPart) iterator.next();
//Get content type
String contentType = ap.getContentType();
System.out.println("content type: " + conent TYpe);
//Get content id
String contentID = ap.getContentID();
System.out.println("content Id:" + contentId);
//Check to see if this is text
if(contentType.indexOf"text")>=0 {
//Get and print string content if it is a text attachment
String content = (String) ap.getContent();
System.outprintln("*** attachment content: " + content);
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
//Main method to start sample receiver
public static void main (String[] args){
try {
String topicName = System.getProperty("TopicName");
if( topicName == null) {
topicName = "test";
}
ReceiveSOAPMessageWithJMS rsm = new ReceiveSOAPMessageWithJMS(topicName);
}catch (Exception e) {
e.printStackTrace();
}
}
}