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.invokers.ejb;
023:
024: import javax.ejb.EJBException;
025: import javax.ejb.MessageDrivenBean;
026: import javax.ejb.MessageDrivenContext;
027: import javax.jms.JMSException;
028: import javax.jms.Message;
029: import javax.jms.MessageListener;
030: import javax.jms.Queue;
031: import javax.jms.QueueConnection;
032: import javax.jms.QueueConnectionFactory;
033: import javax.jms.QueueSender;
034: import javax.jms.QueueSession;
035: import javax.jms.ObjectMessage;
036: import javax.naming.Context;
037: import javax.naming.InitialContext;
038:
039: import org.jboss.logging.Logger;
040: import org.jboss.test.invokers.interfaces.BusinessObjectLocal;
041: import org.jboss.test.invokers.interfaces.BusinessObjectLocalHome;
042:
043: /** An MDB that acts a an async
044: * @author Scott.Stark@jboss.org
045: * @version $Revision: 57211 $
046: */
047: public class JMSGatewayMDB implements MessageDrivenBean,
048: MessageListener {
049: static Logger log = Logger.getLogger(JMSGatewayMDB.class);
050: private MessageDrivenContext ctx = null;
051: private QueueConnection queConn;
052: private QueueSession session;
053: private Context enc;
054:
055: public void setMessageDrivenContext(MessageDrivenContext ctx) {
056: this .ctx = ctx;
057: try {
058: InitialContext iniCtx = new InitialContext();
059: enc = (Context) iniCtx.lookup("java:comp/env");
060: QueueConnectionFactory factory = (QueueConnectionFactory) enc
061: .lookup("jms/ConnectionFactory");
062: queConn = factory.createQueueConnection();
063: session = queConn.createQueueSession(false,
064: QueueSession.AUTO_ACKNOWLEDGE);
065: } catch (Exception e) {
066: log.error("Setup failure", e);
067: throw new EJBException("Setup failure", e);
068: }
069: }
070:
071: public void ejbCreate() {
072: }
073:
074: public void ejbRemove() throws EJBException {
075: try {
076: if (session != null)
077: session.close();
078: if (queConn != null)
079: queConn.close();
080: } catch (Exception e) {
081: log.error("Failed to close JMS resources", e);
082: }
083: }
084:
085: /**
086: *
087: * @param message
088: */
089: public void onMessage(Message message) {
090: log.info("onMessage, msg=" + message);
091: try {
092: ObjectMessage objMsg = (ObjectMessage) message;
093: Queue replyTo = (Queue) message.getJMSReplyTo();
094: String ejbName = message.getStringProperty("ejbName");
095: Object[] args = (Object[]) objMsg.getObject();
096: Object ref = enc.lookup("ejb/" + ejbName);
097: log.info("ejb/" + ejbName + " = " + ref);
098: BusinessObjectLocalHome home = (BusinessObjectLocalHome) ref;
099: BusinessObjectLocal bean = home.create();
100: String reply = bean.doSomethingSlowly(args[0],
101: (String) args[1]);
102: reply = reply + "viaJMSGatewayMDB";
103: sendReply(reply, replyTo);
104: } catch (Exception e) {
105: log.error("onMessage failure", e);
106: }
107: }
108:
109: private void sendReply(String reply, Queue replyTo)
110: throws JMSException {
111: QueueSender sender = session.createSender(replyTo);
112: Message replyMsg = session.createObjectMessage(reply);
113: sender.send(replyMsg);
114: }
115: }
|