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.foedeployer.ejb.message;
023:
024: import java.rmi.RemoteException;
025:
026: import javax.ejb.CreateException;
027: import javax.ejb.MessageDrivenBean;
028: import javax.ejb.MessageDrivenContext;
029:
030: import javax.jms.JMSException;
031: import javax.jms.Message;
032: import javax.jms.MessageListener;
033: import javax.jms.ObjectMessage;
034: import javax.jms.TextMessage;
035: import javax.jms.QueueConnectionFactory;
036: import javax.jms.QueueConnection;
037: import javax.jms.QueueSession;
038: import javax.jms.Session;
039: import javax.jms.QueueSender;
040: import javax.jms.Queue;
041:
042: import javax.naming.InitialContext;
043: import javax.naming.NamingException;
044:
045: import org.apache.log4j.Category;
046:
047: /**
048: * A simple Message Driven Bean
049: *
050: * @ejb.bean
051: * name="MessageTrader"
052: * generate="true"
053: * jndi-name="MessageTraderBean"
054: * transaction-type="Container"
055: * acknowledge-mode="Auto-acknowledge"
056: * destination-type="javax.jms.Topic"
057: * subscription-durability="NonDurable"
058: *
059: * @jboss.destination-jndi-name name="topic/testTopic"
060: * @weblogic.message-driven destination-jndi-name="topic/testTopic"
061: *
062: * @author <a href="mailto:loubyansky@hotmail.com">Alex Loubyansky</a>
063: */
064: public class MessageTraderBean implements MessageDrivenBean,
065: MessageListener {
066: // Constants -------------------------------------------------
067: private static final String QUEUE_CONNECTION_FACTORY = "ConnectionFactory";
068: private static final String QUEUE = "queue/testQueue";
069:
070: // Attributes ------------------------------------------------
071: protected Category log;
072: private MessageDrivenContext mdc;
073: private transient QueueConnection queueConnection;
074: private transient Queue queue;
075:
076: // MessageDrivenBean implementation --------------------------
077: /**
078: * Sets the session context.
079: *
080: * @param ctx MessageDrivenContext Context for session
081: */
082: public void setMessageDrivenContext(MessageDrivenContext ctx) {
083: mdc = ctx;
084: }
085:
086: // Implementation of MessageListener -----------------------------------
087: /**
088: * Handle the message.
089: */
090: public void onMessage(Message msg) {
091: getLog()
092: .debug(
093: "received message of type: "
094: + msg.getClass().getName());
095:
096: if (!(msg instanceof ObjectMessage))
097: getLog().error("message isn't of type ObjectMessage");
098:
099: try {
100: QuoteMessage qm = (QuoteMessage) ((ObjectMessage) msg)
101: .getObject();
102:
103: getLog().debug("received new quote: " + qm.getQuote());
104:
105: send(msg);
106: } catch (Exception ex) {
107: getLog().error("ERROR: ", ex);
108: }
109: }
110:
111: public void ejbCreate() {
112: }
113:
114: public void ejbRemove() {
115: if (queueConnection != null) {
116: getLog().debug("closing connection");
117: try {
118: queueConnection.close();
119: } catch (JMSException jmse) {
120: getLog().debug(
121: "Exception while closing queue connection: ",
122: jmse);
123: }
124: } else {
125: // it could be null because not all MDBs in pool might be used
126: getLog().debug("queue connection is null");
127: }
128: }
129:
130: // Private --------------------------------------------------------
131: private void send(Message msg) throws Exception {
132: QueueSession queueSession = getQueueSession();
133: queue = getQueue();
134:
135: getLog().debug("creating sender");
136: QueueSender queueSender = queueSession.createSender(queue);
137:
138: ObjectMessage objMsg = (ObjectMessage) msg;
139: QuoteMessage qm = (QuoteMessage) objMsg.getObject();
140: getLog().debug("resending the message: " + qm.getQuote());
141: queueSender.send(msg);
142: }
143:
144: private QueueSession getQueueSession() throws Exception {
145: if (queueConnection == null) {
146: getLog().debug(
147: "looking for queue connection factory: "
148: + QUEUE_CONNECTION_FACTORY);
149:
150: InitialContext ctx = new InitialContext();
151: QueueConnectionFactory queueFactory = (QueueConnectionFactory) ctx
152: .lookup(QUEUE_CONNECTION_FACTORY);
153: queueConnection = queueFactory.createQueueConnection();
154: }
155:
156: getLog().debug("creating queue connection");
157: return queueConnection.createQueueSession(false,
158: Session.AUTO_ACKNOWLEDGE);
159: }
160:
161: private Queue getQueue() throws Exception {
162: if (queue == null) {
163: getLog().debug("looking for queue: " + QUEUE);
164:
165: InitialContext ctx = new InitialContext();
166: queue = (Queue) ctx.lookup(QUEUE);
167: }
168:
169: return queue;
170: }
171:
172: private Category getLog() {
173: if (log != null)
174: return log;
175: log = Category.getInstance(this.getClass());
176: return log;
177: }
178: }
|