6. Ultra Messaging JMS Programming Quick Start

This source code example is provided by Informatica for educational and evaluation purposes only. Error handling in these programs is primitive. A production program would want to have better error handling, but for the purposes of a minimal example, it would just be a distraction. Also, a production program would want to support a configuration file to override default values on options.

When building on Windows, use the following compile line. (QueueReceiver.java is the application being compiled.)

$JAVA_HOME/bin/javac QueueReceiver.java -cp $JMS_HOME/lib/uJMS_1.0.jar;$JMS_HOME/lib/jms.jar
     

When building on Unix, use the following compile line. (QueueReceiver.java is the application being compiled.)

$JAVA_HOME/bin/javac QueueReceiver.java -cp $JMS_HOME/lib/JMS_API.jar:$JMS_HOME/lib/jms.jar
     

6.1. Ultra Messaging JMS Synchronous Consumer Implementation

This is a source code listing of a simple synchronous consumer program. You may find it helpful to download the source code (most browsers let you right-click on the link and use the save link target function, or some variation).

/*file: SimpleSyncConsumer.java
 *
 * Copyright (c) 2005-2014 Informatica Corporation. All Rights Reserved.
 * Permission is granted to licensees to use
 * or alter this software for any purpose, including commercial applications,
 * according to the terms laid out in the Software License Agreement.
 */
package examples;

   (1)
import javax.jms.*;
import javax.naming.*;

public class SyncConsumer implements ExceptionListener {

    public static void main(String[] args) {
        new SyncConsumer();
    }

    public SyncConsumer() {
        Context jndiContext = null;

        try {   (2)
            jndiContext = new InitialContext();
        } catch (NamingException e) {
            System.out.println("Could not create JNDI API "
                    + "context: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }

        try {   (3)
            ConnectionFactory factory = (ConnectionFactory) jndiContext.lookup("uJMSConnectionFactory");
            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);
            connection.start();

            while (true) {
                System.out.println("Received message " + msgConsumer.receive());
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

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

Notes:

(1)
The SimpleSyncConsumer class consists only of a main method, which receives one or more messages from a topic using synchronous message delivery Run this program in conjunction with SimpleProducer.
(2)
Create a JNDI API InitialContext object if none exists yet.
(3)
Look up connection factory and topic. If either does not exist, exit.

6.2. Ultra Messaging JMS Asynchronous Consumer Implementation

This is a source code listing of a simple asynchronous consumer program. You may find it helpful to download the source code (most browsers let you right-click on the link and use the save link target function, or some variation).

/*file: SimpleAsyncConsumer.java
 *
 * Copyright (c) 2005-2014 Informatica Corporation. All Rights Reserved.
 * Permission is granted to licensees to use
 * or alter this software for any purpose, including commercial applications,
 * according to the terms laid out in the Software License Agreement.
 */
package examples;   (1)

import javax.jms.*;
import javax.naming.*;

public class AsyncConsumer implements MessageListener, ExceptionListener {

    public static void main(String[] args) {
        new AsyncConsumer();
        while (true) {
            try {
                Thread.sleep(1000000);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public AsyncConsumer() {
        Context jndiContext = null;

        try {   (2)
            jndiContext = new InitialContext();
        } catch (NamingException e) {
            System.out.println("Could not create JNDI API "
                    + "context: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }

        try {   (3)
            ConnectionFactory factory = (ConnectionFactory) jndiContext.lookup("uJMSConnectionFactory");
            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();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

// The exception listener
    public void onException(JMSException e) {
        // print the connection exception status
        e.printStackTrace();
    }

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

Notes:

(1)
The SimpleAsyncConsumer class consists only of a main method, which receives one or more messages from a topic using asynchronous message delivery. It uses the message listener TextListener. Run this program in conjunction with SimpleProducer. To end the program, enter Q or q on the command line.
(2)
Create a JNDI API InitialContext object if none exists yet.
(3)
Look up connection factory and topic. If either does not exist, exit.

6.3. Ultra Messaging JMS Simple Producer Implementation

This is a source code listing of a simple producer program. You may find it helpful to download the source code (most browsers let you right-click on the link and use the save link target function, or some variation).

/*file: SimpleProducer.java
 *
 * Copyright (c) 2005-2014 Informatica Corporation. All Rights Reserved.
 * Permission is granted to licensees to use
 * or alter this software for any purpose, including commercial applications,
 * according to the terms laid out in the Software License Agreement.
 */
package examples;

import javax.jms.*;   (1)
import javax.naming.*;

public class Producer {

    public static void main(String[] args) {
        new Producer();
    }

    public Producer() {
        Context jndiContext = null;

        try {   (2)
            jndiContext = new InitialContext();
        } catch (NamingException e) {
            System.out.println("Could not create JNDI API "
                    + "context: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }

        Connection connection = null;   (3)
        try {
            ConnectionFactory factory = (ConnectionFactory) jndiContext.lookup("uJMSConnectionFactory");
            connection = factory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            connection.start();
            Destination destination = session.createTopic("TOPIC.1");
            MessageProducer producer = session.createProducer(destination);
            TextMessage message = session.createTextMessage();
            for (int i = 0; i < 10; i++) {
                ((TextMessage) message).setText("********* This is a test *********** ");
                producer.send(message);
                Thread.sleep(10);
                System.out.println("Sent " + message.getText());
            }
        } catch (Exception e) {
            System.out.println("JNDI API lookup failed: "
                    + e.toString());
            e.printStackTrace();
            System.exit(1);

        } finally {
            if (connection != null) {
                try {

                    connection.close();
                    System.exit(0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Notes:

(1)
The SimpleProducer class consists only of a main method, which sends10 messages to a topic. Run this program in conjunction with SimpleAsyncConsumer and SimpleSyncConsumer.
(2)
Create a JNDI API InitialContext object if none exists yet.
(3)
Look up connection factory and topic. If either does not exist, exit.

Copyright 2005 - 2014 Informatica Corporation.