KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > invokers > ejb > JMSGatewayMDB


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.invokers.ejb;
23
24 import javax.ejb.EJBException JavaDoc;
25 import javax.ejb.MessageDrivenBean JavaDoc;
26 import javax.ejb.MessageDrivenContext JavaDoc;
27 import javax.jms.JMSException JavaDoc;
28 import javax.jms.Message JavaDoc;
29 import javax.jms.MessageListener JavaDoc;
30 import javax.jms.Queue JavaDoc;
31 import javax.jms.QueueConnection JavaDoc;
32 import javax.jms.QueueConnectionFactory JavaDoc;
33 import javax.jms.QueueSender JavaDoc;
34 import javax.jms.QueueSession JavaDoc;
35 import javax.jms.ObjectMessage JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38
39 import org.jboss.logging.Logger;
40 import org.jboss.test.invokers.interfaces.BusinessObjectLocal;
41 import org.jboss.test.invokers.interfaces.BusinessObjectLocalHome;
42
43 /** An MDB that acts a an async
44  * @author Scott.Stark@jboss.org
45  * @version $Revision: 37406 $
46  */

47 public class JMSGatewayMDB implements MessageDrivenBean JavaDoc, MessageListener JavaDoc
48 {
49    static Logger log = Logger.getLogger(JMSGatewayMDB.class);
50    private MessageDrivenContext JavaDoc ctx = null;
51    private QueueConnection JavaDoc queConn;
52    private QueueSession JavaDoc session;
53    private Context JavaDoc enc;
54
55    public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
56    {
57       this.ctx = ctx;
58       try
59       {
60          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
61          enc = (Context JavaDoc) iniCtx.lookup("java:comp/env");
62          QueueConnectionFactory JavaDoc factory = (QueueConnectionFactory JavaDoc) enc.lookup("jms/ConnectionFactory");
63          queConn = factory.createQueueConnection();
64          session = queConn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
65       }
66       catch(Exception JavaDoc e)
67       {
68          log.error("Setup failure", e);
69          throw new EJBException JavaDoc("Setup failure", e);
70       }
71    }
72    public void ejbCreate()
73    {
74    }
75    public void ejbRemove() throws EJBException JavaDoc
76    {
77       try
78       {
79          if( session != null )
80             session.close();
81          if( queConn != null )
82             queConn.close();
83       }
84       catch(Exception JavaDoc e)
85       {
86          log.error("Failed to close JMS resources", e);
87       }
88    }
89
90    /**
91     *
92     * @param message
93     */

94    public void onMessage(Message JavaDoc message)
95    {
96       log.info("onMessage, msg="+message);
97       try
98       {
99          ObjectMessage JavaDoc objMsg = (ObjectMessage JavaDoc) message;
100          Queue JavaDoc replyTo = (Queue JavaDoc) message.getJMSReplyTo();
101          String JavaDoc ejbName = message.getStringProperty("ejbName");
102          Object JavaDoc[] args = (Object JavaDoc[]) objMsg.getObject();
103          Object JavaDoc ref = enc.lookup("ejb/"+ejbName);
104          log.info("ejb/"+ejbName+" = "+ref);
105          BusinessObjectLocalHome home = (BusinessObjectLocalHome) ref;
106          BusinessObjectLocal bean = home.create();
107          String JavaDoc reply = bean.doSomethingSlowly(args[0], (String JavaDoc) args[1]);
108          reply = reply + "viaJMSGatewayMDB";
109          sendReply(reply, replyTo);
110       }
111       catch(Exception JavaDoc e)
112       {
113          log.error("onMessage failure", e);
114       }
115    }
116
117    private void sendReply(String JavaDoc reply, Queue JavaDoc replyTo) throws JMSException JavaDoc
118    {
119       QueueSender JavaDoc sender = session.createSender(replyTo);
120       Message JavaDoc replyMsg = session.createObjectMessage(reply);
121       sender.send(replyMsg);
122    }
123 }
124
Popular Tags