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
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; 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 { jndiContext = new InitialContext(); } catch (NamingException e) { System.out.println("Could not create JNDI API " + "context: " + e.toString()); e.printStackTrace(); System.exit(1); } try { 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:
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; 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 { jndiContext = new InitialContext(); } catch (NamingException e) { System.out.println("Could not create JNDI API " + "context: " + e.toString()); e.printStackTrace(); System.exit(1); } try { 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:
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.*; import javax.naming.*; public class Producer { public static void main(String[] args) { new Producer(); } public Producer() { Context jndiContext = null; try { 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; 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:
Copyright 2005 - 2014 Informatica Corporation.