1 7 package org.jboss.jms.serverless.client; 8 9 import javax.naming.Context ; 10 import javax.naming.InitialContext ; 11 import javax.naming.NamingException ; 12 import org.jboss.logging.Logger; 13 import javax.jms.ConnectionFactory ; 14 import javax.jms.Connection ; 15 import javax.jms.Session ; 16 import javax.jms.MessageProducer ; 17 import javax.jms.MessageConsumer ; 18 import javax.jms.Destination ; 19 import javax.jms.TextMessage ; 20 import javax.jms.Topic ; 21 import javax.jms.MessageListener ; 22 import javax.jms.Message ; 23 import javax.jms.JMSException ; 24 25 31 public class CommonInterfaceQueueReceiver { 32 33 private static final Logger log = Logger.getLogger(CommonInterfaceQueueReceiver.class); 34 35 37 public static void main(String [] args) throws Exception { 38 39 Context initialContext = new InitialContext (); 40 41 ConnectionFactory connectionFactory = 42 (ConnectionFactory )initialContext.lookup("ConnectionFactory"); 43 44 Destination queue = (Destination )initialContext.lookup("Queue1"); 45 46 Connection connection = connectionFactory.createConnection(); 47 48 Session 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 { 59 60 private String receiverName; 61 62 public MessageListenerImpl(String receiverName) { 63 64 this.receiverName = receiverName; 65 } 66 67 public void onMessage(Message message) { 68 69 try { 70 log.info(receiverName+" got message: "+((TextMessage )message).getText()); 71 } 72 catch(JMSException e) { 73 log.error("Error handling the message", e); 74 } 75 } 76 } 77 78 } 79 80 81 82 | Popular Tags |