6. Asynchronous Message Delivery

You can program asynchronous message delivery using the MessageListener class. The application registers a callback handler to receive messages asynchronously.

// 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);

// set the exception listener callback
connection.setExceptionListener(this);

// Create a topic destination
Destination destination = session.createTopic("TOPIC.1");

// create the consumer 
MessageConsumer msgConsumer = session.createConsumer(destination);

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

// start the connection 
connection.start();

// The exception listener
public void onException(JMSException e)
{
    // print the connection exception status 
        System.err.println("Exception occurred: "+ e.getMessage());
}

// The message listener callback
public void onMessage(Message msg)
{
    try
    {
        System.err.println("Received message: " + msg);
    }
    catch(Exception e)
    {
            System.err.println("Exception occurred: "+ e.getMessage());
                System.exit(-1);
        }
}
       

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