1 22 package org.jboss.test.cts.jms; 23 24 import java.util.HashMap ; 25 import javax.jms.JMSException ; 26 import javax.jms.Message ; 27 import javax.jms.MessageListener ; 28 import javax.jms.QueueConnection ; 29 import javax.jms.QueueConnectionFactory ; 30 import javax.jms.QueueReceiver ; 31 import javax.jms.QueueSession ; 32 import javax.jms.Session ; 33 import javax.jms.TextMessage ; 34 import javax.jms.Queue ; 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 import javax.naming.NamingException ; 38 39 import org.jboss.logging.Logger; 40 41 public class ContainerMBox 42 implements MessageListener 43 { 44 public final static String JMS_FACTORY="ConnectionFactory"; 45 public final static String QUEUE="queue/testQueue"; 46 47 private QueueConnectionFactory qconFactory; 48 private QueueConnection qcon; 49 private QueueSession qsession; 50 private QueueReceiver qreceiver; 51 private Queue queue; 52 53 private Logger log; 54 55 public static final String EJB_CREATE_MSG = "EJB_CREATE_MSG"; 56 public static final String EJB_POST_CREATE_MSG = "EJB_POST_CREATE_MSG"; 57 public static final String EJB_ACTIVATE_MSG = "EJB_ACTIVATE_MSG"; 58 public static final String EJB_PASSIVATE_MSG = "EJB_PASSIVATE_MSG"; 59 public static final String EJB_REMOVE_MSG = "EJB_REMOVE_MSG"; 60 public static final String EJB_LOAD_MSG = "EJB_LOAD_MSG"; 61 public static final String EJB_STORE_MSG = "EJB_STORE_MSG"; 62 public static final String SET_ENTITY_CONTEXT_MSG = "SET_ENTITY_CONTEXT_MSG"; 63 public static final String UNSET_ENTITY_CONTEXT_MSG = "UNSET_ENTITY_CONTEXT_MSG"; 64 65 private HashMap messageList = new HashMap ( ); 66 67 public ContainerMBox ( ) 68 { 69 log = Logger.getLogger(getClass().getName()); 70 try 71 { 72 init( new InitialContext (), QUEUE ); 73 } 74 catch(Exception ex) 75 { 76 log.error("MBox could not get initial context", ex); 77 } 78 } 79 80 public void onMessage(Message msg) 82 { 83 try 84 { 85 String msgText; 86 if (msg instanceof TextMessage ) 87 { 88 msgText = ((TextMessage )msg).getText(); 89 } 90 else 91 { 92 msgText = msg.toString(); 93 } 94 95 log.debug("[BEAN MESSAGE]: "+ msgText ); 96 messageList.put(msgText, "msg" ); 97 } 98 catch (JMSException jmse) 99 { 100 log.error("problem receiving MBox message", jmse); 101 } 102 } 103 104 108 public void init(Context ctx, String queueName) 109 throws NamingException , JMSException 110 { 111 qconFactory = (QueueConnectionFactory ) ctx.lookup(JMS_FACTORY); 112 qcon = qconFactory.createQueueConnection(); 113 qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 114 try 115 { 116 queue = (Queue ) ctx.lookup(queueName); 117 } 118 catch (NamingException ne) 119 { 120 queue = qsession.createQueue(queueName); 121 ctx.bind(queueName, queue); 122 } 123 qreceiver = qsession.createReceiver(queue); 124 qreceiver.setMessageListener(this); 125 qcon.start(); 126 } 127 128 131 public void close() 132 throws JMSException 133 { 134 qreceiver.close(); 135 qsession.close(); 136 qcon.close(); 137 } 138 139 public boolean messageReceived( String message ) 140 { 141 return messageList.containsKey(message); 142 } 143 144 public void clearMessages( ) 145 { 146 messageList = null; 147 messageList = new HashMap (); 148 } 149 150 } 151 152 153 154 155 156 157 | Popular Tags |