9. Request/Reply Sample Applications

The following example applications demonstrate the request/reply model.

9.1. Request Sample Application

// Obtain a ConnectionFactory via lookup or direct instantiation
ConnectionFactory factory = (ConnectionFactory)jndiContext.lookup("uJMSConnectionFactory");


// Create a connection - assuming no username/password required for UM
Connection connection = factory.createConnection();

// Create a Session
Session session = connection.createSession(false,
                              javax.jms.Session.AUTO_ACKNOWLEDGE);
// Create request and reply destinations
Destination requestTopic = lookupDestination(requestTopicName);
Destination replyTopic = lookupDestination(replyTopicName);
        
MessageProducer requestProducer = session.createProducer(requestTopic);
MessageConsumer replyConsumer = session.createConsumer(replyTopic);

TextMessage requestMessage = session.createTextMessage();
requestMessage.setText("Hello world.");
requestMessage.setJMSReplyTo(replyTopic);
requestProducer.send(requestMessage);

// start the connection 
connection.start();

// Wait for the reply
Message replyMessage = replyConsumer.receive();

// Could check correlationID, not really necessary with sync request reply
if (replyMessage.getJMSCorrelationID() != requestMessage.getJMSMessageID())
{
    System.err.println("Unexpected reply message"+ e.getMessage());
}
           

9.2. Reply Sample Application

// Obtain a ConnectionFactory via lookup or direct instantiation
ConnectionFactory factory = (ConnectionFactory)jndiContext.lookup("uJMSConnectionFactory");


// Create a connection - assuming no username/password required for UM
Connection connection = factory.createConnection();

// Create a Session
Session session = connection.createSession(false,
                              javax.jms.Session.AUTO_ACKNOWLEDGE);
// Create request destination
Destination requestTopic = lookupDestination(requestTopicName);

// Create consumer on request topic     
MessageConsumer requestConsumer = session.createConsumer(requestTopic);

// Create a producer, don't know reply destination at this point.
MessageProducer replyProducer = session.createProducer(null);


// set the message listener callback
msgConsumer.setMessageListener(this);

// start the connection 
connection.start();


// The message listener callback
public void onMessage(Message message)
{
    try
    {
        TextMessage requestMessage = (TextMessage) message;
        String contents = requestMessage.getText();

        // get the reply to destination
        Destination replyDestination = requestMessage.getJMSReplyTo();

        TextMessage replyMessage = session.createTextMessage();
        replyMessage.setText(contents);
        
        replyMessage.setJMSCorrelationID(requestMessage.getJMSMessageID());
        replyProducer.send(replyDestination, replyMessage);
    }
    catch(Exception e)
    {
            System.err.println("Exception occurred: "+ e.getMessage());
                System.exit(-1);
        }
}
           

Copyright (c) 2004 - 2014 Informatica Corporation. All rights reserved.