Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The SimpleQueueReceiver class consists only of a main method,
- * which fetches one or more messages from a queue using
- * synchronous message delivery. Run this program in conjunction
- * with SimpleQueueSender. Specify a queue name on the command
- * line when you run the program.
- */
- import javax.jms.*;
- import javax.naming.*;
- public class SimpleQueueReceiver {
- /**
- * Main method.
- *
- * @param args the queue used by the example
- */
- public static void main(String[] args) {
- String queueName = "wwseMatchedOrderQueue";
- Context jndiContext = null;
- QueueConnectionFactory queueConnectionFactory = null;
- QueueConnection queueConnection = null;
- QueueSession queueSession = null;
- Queue queue = null;
- QueueReceiver queueReceiver = null;
- TextMessage message = null;
- /*
- * Create a JNDI API InitialContext object if none exists
- * yet.
- */
- try {
- jndiContext = new InitialContext();
- } catch (NamingException e) {
- System.out.println("Could not create JNDI API " +
- "context: " + e.toString());
- System.exit(1);
- }
- /*
- * Look up connection factory and queue. If either does
- * not exist, exit.
- */
- try {
- queueConnectionFactory = (QueueConnectionFactory)
- jndiContext.lookup("wwseConnFactory");
- queue = (Queue) jndiContext.lookup(queueName);
- } catch (NamingException e) {
- System.out.println("JNDI API lookup failed: " +
- e.toString());
- System.exit(1);
- }
- /*
- * Create connection.
- * Create session from connection; false means session is
- * not transacted.
- * Create receiver, then start message delivery.
- * Receive all text messages from queue until
- * a non-text message is received indicating end of
- * message stream.
- * Close connection.
- */
- try {
- queueConnection =
- queueConnectionFactory.createQueueConnection();
- queueSession =
- queueConnection.createQueueSession(false,
- Session.AUTO_ACKNOWLEDGE);
- queueReceiver = queueSession.createReceiver(queue);
- queueConnection.start();
- while (true) {
- Message m = queueReceiver.receive(1);
- if (m != null) {
- if (m instanceof TextMessage) {
- message = (TextMessage) m;
- System.out.println("Reading message: " +
- message.getText());
- } else {
- break;
- }
- }
- }
- } catch (JMSException e) {
- System.out.println("Exception occurred: " +
- e.toString());
- } finally {
- if (queueConnection != null) {
- try {
- queueConnection.close();
- } catch (JMSException e) {}
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement