KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > serverless > client > CommonInterfaceQueueReceiver


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.serverless.client;
8
9 import javax.naming.Context JavaDoc;
10 import javax.naming.InitialContext JavaDoc;
11 import javax.naming.NamingException JavaDoc;
12 import org.jboss.logging.Logger;
13 import javax.jms.ConnectionFactory JavaDoc;
14 import javax.jms.Connection JavaDoc;
15 import javax.jms.Session JavaDoc;
16 import javax.jms.MessageProducer JavaDoc;
17 import javax.jms.MessageConsumer JavaDoc;
18 import javax.jms.Destination JavaDoc;
19 import javax.jms.TextMessage JavaDoc;
20 import javax.jms.Topic JavaDoc;
21 import javax.jms.MessageListener JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.JMSException JavaDoc;
24
25 /**
26  * A simple JMS client that receives messages from a queue. It uses the common JMS 1.1 interfaces.
27  *
28  * @author Ovidiu Feodorov <ovidiu@jboss.org>
29  * @version $Revision: 1.1 $ $Date: 2004/04/15 22:54:20 $
30  **/

31 public class CommonInterfaceQueueReceiver {
32
33     private static final Logger log = Logger.getLogger(CommonInterfaceQueueReceiver.class);
34
35     /**
36      **/

37     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
38
39         Context JavaDoc initialContext = new InitialContext JavaDoc();
40
41         ConnectionFactory JavaDoc connectionFactory =
42             (ConnectionFactory JavaDoc)initialContext.lookup("ConnectionFactory");
43
44         Destination JavaDoc queue = (Destination JavaDoc)initialContext.lookup("Queue1");
45
46         Connection JavaDoc connection = connectionFactory.createConnection();
47
48         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
49         
50         session.createConsumer(queue).setMessageListener(new MessageListenerImpl("Receiver ONE"));
51         session.createConsumer(queue).setMessageListener(new MessageListenerImpl("Receiver TWO"));
52
53         connection.start();
54         log.info("Connection started, waiting for messages ...");
55     }
56
57
58     static class MessageListenerImpl implements MessageListener JavaDoc {
59
60         private String JavaDoc receiverName;
61
62         public MessageListenerImpl(String JavaDoc receiverName) {
63
64             this.receiverName = receiverName;
65         }
66
67         public void onMessage(Message JavaDoc message) {
68                     
69             try {
70                 log.info(receiverName+" got message: "+((TextMessage JavaDoc)message).getText());
71             }
72             catch(JMSException JavaDoc e) {
73                 log.error("Error handling the message", e);
74             }
75         }
76     }
77
78 }
79
80
81
82
Popular Tags