KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > demo > SimpleConsumer


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 /**
20  * The SimpleQueueReceiver class consists only of a main method,
21  * which fetches one or more messages from a queue using
22  * synchronous message delivery. Run this program in conjunction
23  * with SimpleQueueSender. Specify a queue name on the command
24  * line when you run the program.
25  */

26 package org.apache.activemq.demo;
27
28 import javax.jms.Connection JavaDoc;
29 import javax.jms.ConnectionFactory JavaDoc;
30 import javax.jms.Destination JavaDoc;
31 import javax.jms.JMSException JavaDoc;
32 import javax.jms.Message JavaDoc;
33 import javax.jms.MessageConsumer JavaDoc;
34 import javax.jms.Session JavaDoc;
35 import javax.jms.TextMessage JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39
40 /**
41  * A simple polymorphic JMS consumer which can work with Queues or Topics
42  * which uses JNDI to lookup the JMS connection factory and destination
43  *
44  * @version $Revision: 1.2 $
45  */

46 public class SimpleConsumer {
47     
48     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
49             .getLog(SimpleConsumer.class);
50
51     /**
52      * @param args the queue used by the example
53      */

54     public static void main(String JavaDoc[] args) {
55         String JavaDoc destinationName = null;
56         Context JavaDoc jndiContext = null;
57         ConnectionFactory JavaDoc connectionFactory = null;
58         Connection JavaDoc connection = null;
59         Session JavaDoc session = null;
60         Destination JavaDoc destination = null;
61         MessageConsumer JavaDoc consumer = null;
62
63         /*
64          * Read destination name from command line and display it.
65          */

66         if (args.length != 1) {
67             log.info("Usage: java SimpleConsumer <destination-name>");
68             System.exit(1);
69         }
70         destinationName = args[0];
71         log.info("Destination name is " + destinationName);
72
73         /*
74          * Create a JNDI API InitialContext object
75          */

76         try {
77             jndiContext = new InitialContext JavaDoc();
78         }
79         catch (NamingException JavaDoc e) {
80             log.info("Could not create JNDI API " +
81                     "context: " + e.toString());
82             System.exit(1);
83         }
84
85         /*
86          * Look up connection factory and destination.
87          */

88         try {
89             connectionFactory = (ConnectionFactory JavaDoc)
90                     jndiContext.lookup("ConnectionFactory");
91             destination = (Destination JavaDoc) jndiContext.lookup(destinationName);
92         }
93         catch (NamingException JavaDoc e) {
94             log.info("JNDI API lookup failed: " +
95                     e.toString());
96             System.exit(1);
97         }
98
99         /*
100          * Create connection.
101          * Create session from connection; false means session is
102          * not transacted.
103          * Create receiver, then start message delivery.
104          * Receive all text messages from destination until
105          * a non-text message is received indicating end of
106          * message stream.
107          * Close connection.
108          */

109         try {
110             connection = connectionFactory.createConnection();
111             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
112             consumer = session.createConsumer(destination);
113             connection.start();
114             while (true) {
115                 Message JavaDoc m = consumer.receive(1);
116                 if (m != null) {
117                     if (m instanceof TextMessage JavaDoc) {
118                         TextMessage JavaDoc message = (TextMessage JavaDoc) m;
119                         log.info("Reading message: " + message.getText());
120                     }
121                     else {
122                         break;
123                     }
124                 }
125             }
126         }
127         catch (JMSException JavaDoc e) {
128             log.info("Exception occurred: " + e);
129         }
130         finally {
131             if (connection != null) {
132                 try {
133                     connection.close();
134                 }
135                 catch (JMSException JavaDoc e) {
136                 }
137             }
138         }
139     }
140 }
141
Popular Tags