01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.jms.serverless.client;
23:
24: import javax.naming.Context;
25: import javax.naming.InitialContext;
26: import javax.naming.NamingException;
27: import org.jboss.logging.Logger;
28: import javax.jms.ConnectionFactory;
29: import javax.jms.Connection;
30: import javax.jms.Session;
31: import javax.jms.MessageProducer;
32: import javax.jms.MessageConsumer;
33: import javax.jms.Destination;
34: import javax.jms.TextMessage;
35: import javax.jms.Topic;
36: import javax.jms.MessageListener;
37: import javax.jms.Message;
38: import javax.jms.JMSException;
39:
40: /**
41: * A simple JMS client that receives messages from a queue. It uses the common JMS 1.1 interfaces.
42: *
43: * @author Ovidiu Feodorov <ovidiu@jboss.org>
44: * @version $Revision: 57195 $ $Date: 2006-09-26 08:08:17 -0400 (Tue, 26 Sep 2006) $
45: **/
46: public class CommonInterfaceQueueReceiver {
47:
48: private static final Logger log = Logger
49: .getLogger(CommonInterfaceQueueReceiver.class);
50:
51: /**
52: **/
53: public static void main(String[] args) throws Exception {
54:
55: Context initialContext = new InitialContext();
56:
57: ConnectionFactory connectionFactory = (ConnectionFactory) initialContext
58: .lookup("ConnectionFactory");
59:
60: Destination queue = (Destination) initialContext
61: .lookup("Queue1");
62:
63: Connection connection = connectionFactory.createConnection();
64:
65: Session session = connection.createSession(false,
66: Session.AUTO_ACKNOWLEDGE);
67:
68: session.createConsumer(queue).setMessageListener(
69: new MessageListenerImpl("Receiver ONE"));
70: session.createConsumer(queue).setMessageListener(
71: new MessageListenerImpl("Receiver TWO"));
72:
73: connection.start();
74: log.info("Connection started, waiting for messages ...");
75: }
76:
77: static class MessageListenerImpl implements MessageListener {
78:
79: private String receiverName;
80:
81: public MessageListenerImpl(String receiverName) {
82:
83: this .receiverName = receiverName;
84: }
85:
86: public void onMessage(Message message) {
87:
88: try {
89: log.info(receiverName + " got message: "
90: + ((TextMessage) message).getText());
91: } catch (JMSException e) {
92: log.error("Error handling the message", e);
93: }
94: }
95: }
96:
97: }
|