001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cts.jms;
023:
024: import java.util.HashMap;
025: import javax.jms.JMSException;
026: import javax.jms.Message;
027: import javax.jms.MessageListener;
028: import javax.jms.QueueConnection;
029: import javax.jms.QueueConnectionFactory;
030: import javax.jms.QueueReceiver;
031: import javax.jms.QueueSession;
032: import javax.jms.Session;
033: import javax.jms.TextMessage;
034: import javax.jms.Queue;
035: import javax.naming.Context;
036: import javax.naming.InitialContext;
037: import javax.naming.NamingException;
038:
039: import org.apache.log4j.Logger;
040:
041: public class ContainerMBox implements MessageListener {
042: public final static String JMS_FACTORY = "ConnectionFactory";
043: public final static String QUEUE = "queue/testQueue";
044:
045: private QueueConnectionFactory qconFactory;
046: private QueueConnection qcon;
047: private QueueSession qsession;
048: private QueueReceiver qreceiver;
049: private Queue queue;
050:
051: private Logger log;
052:
053: public static final String EJB_CREATE_MSG = "EJB_CREATE_MSG";
054: public static final String EJB_POST_CREATE_MSG = "EJB_POST_CREATE_MSG";
055: public static final String EJB_ACTIVATE_MSG = "EJB_ACTIVATE_MSG";
056: public static final String EJB_PASSIVATE_MSG = "EJB_PASSIVATE_MSG";
057: public static final String EJB_REMOVE_MSG = "EJB_REMOVE_MSG";
058: public static final String EJB_LOAD_MSG = "EJB_LOAD_MSG";
059: public static final String EJB_STORE_MSG = "EJB_STORE_MSG";
060: public static final String SET_ENTITY_CONTEXT_MSG = "SET_ENTITY_CONTEXT_MSG";
061: public static final String UNSET_ENTITY_CONTEXT_MSG = "UNSET_ENTITY_CONTEXT_MSG";
062:
063: private HashMap messageList = new HashMap();
064:
065: public ContainerMBox() {
066: log = Logger.getLogger(getClass());
067: try {
068: init(new InitialContext(), QUEUE);
069: } catch (Exception ex) {
070: log.error("MBox could not get initial context", ex);
071: }
072: }
073:
074: // MessageListener interface
075: public void onMessage(Message msg) {
076: try {
077: String msgText;
078: if (msg instanceof TextMessage) {
079: msgText = ((TextMessage) msg).getText();
080: } else {
081: msgText = msg.toString();
082: }
083:
084: log.debug("[BEAN MESSAGE]: " + msgText);
085: messageList.put(msgText, "msg");
086: } catch (JMSException jmse) {
087: log.error("problem receiving MBox message", jmse);
088: }
089: }
090:
091: /**
092: * Create all the necessary objects for receiving
093: * messages from a JMS queue.
094: */
095: public void init(Context ctx, String queueName)
096: throws NamingException, JMSException {
097: qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
098: qcon = qconFactory.createQueueConnection();
099: qsession = qcon.createQueueSession(false,
100: Session.AUTO_ACKNOWLEDGE);
101: try {
102: queue = (Queue) ctx.lookup(queueName);
103: } catch (NamingException ne) {
104: queue = qsession.createQueue(queueName);
105: ctx.bind(queueName, queue);
106: }
107: qreceiver = qsession.createReceiver(queue);
108: qreceiver.setMessageListener(this );
109: qcon.start();
110: }
111:
112: /**
113: * Close JMS objects.
114: */
115: public void close() throws JMSException {
116: qreceiver.close();
117: qsession.close();
118: qcon.close();
119: }
120:
121: public boolean messageReceived(String message) {
122: return messageList.containsKey(message);
123: }
124:
125: public void clearMessages() {
126: messageList = null;
127: messageList = new HashMap();
128: }
129:
130: }
|