Java example for JMS

The following Java example assumes the method getSOAPMessage returns a string that defines a SOAP message. Once the SOAP message is created, the remaining steps should be followed to send the message and receive a response. To focus on the JMS steps, the example omits error handling. Actual implementations should include code that checks for errors.

// Get a SOAP envelope using whatever means you want.
String soap = getSOAPMessage();
System.out.println("Message to send...\n   " + soap);

// Initialize JMS.
// Create a hash table of settings required to access JMS.
// Note that these settings are application server specific.
Hashtable<String, String> hashTable = new Hashtable<String, String>();
hashTable.put(javax.naming.InitialContext.URL_PKG_PREFIXES, "…");
hashTable.put(javax.naming.InitialContext.PROVIDER_URL, "…");
hashTable.put(javax.naming.InitialContext.INITIAL_CONTEXT_FACTORY, "…");

// Create an InitialContext object so you can discover
// various objects such as connection factories and queues.
javax.naming.InitialContext context = new javax.naming.InitialContext(hashTable);

// Attempt to get the connection factory and queue.
javax.jms.Queue scoringQueue = (javax.jms.Queue)context.lookup("queue/PASWScoring");
javax.jms.QueueConnectionFactory factory = (javax.jms.QueueConnectionFactory)context.lookup("ConnectionFactory");

// Create a connection to the queue and start it.
javax.jms.QueueConnection queueConnection = factory.createQueueConnection();
queueConnection.start();

// Open a temporary reply queue.        
javax.jms.QueueSession listenerSession = 
   queueConnection.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
javax.jms.TemporaryQueue temporaryQueue = listenerSession.createTemporaryQueue();

// Listen to the temporary reply queue for messages returned by the scoring service
javax.jms.MessageConsumer consumer = listenerSession.createConsumer(temporaryQueue);

// Create a JMS message using the SOAP message.
// With this session, you can send as many messages as you like.
javax.jms.QueueSession senderSession = 
   queueConnection.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
javax.jms.QueueSender sender = senderSession.createSender(scoringQueue);
javax.jms.BytesMessage message = senderSession.createBytesMessage();

// Set the reply-to queue on the JMS message.
message.setJMSReplyTo(temporaryQueue);

// Send the message to a JMS queue.
// Populate the message with the soap envelope and send it
message.writeBytes(soap.getBytes("UTF-8"));
sender.send(message);

// Receive the reply message.
// NOTE: This method blocks until a message is received.
Message replyJMSMessage = consumer.receive();

// The message format should be a bytes message.
if (replyJMSMessage != null && replyJMSMessage instanceof javax.jms.BytesMessage)
{
    javax.jms.BytesMessage bytesMessage = (javax.jms.BytesMessage) replyJMSMessage;
    byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
    bytesMessage.readBytes(bytes);
    System.out.println("Reply Message");
    // the reply message
    String replyMessage = new String(bytes, "UTF-8");
    System.out.println("   " + replyMessage);
    // the JMS correlation ID can be used to match a sent message with a response message 
    String jmsCorrelationID = replyJMSMessage.getJMSCorrelationID();
    System.out.println("   reply message ID = " + jmsCorrelationID);
}

// After the message is sent, get the message ID.
// You would keep the message ID around somewhere so you can match it to a reply later.
String messageID = message.getJMSMessageID();
System.out.println("   message ID = " + messageID + "\n");
sender.close();

// Cleanup
listenerSession.close();
queueConnection.stop();
senderSession.close();
queueConnection.close();