KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cts > ejb > StrictlyPooledMDB


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.cts.ejb;
23
24 import javax.ejb.MessageDrivenBean JavaDoc;
25 import javax.ejb.MessageDrivenContext JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27 import javax.jms.MessageListener JavaDoc;
28 import javax.jms.QueueConnection JavaDoc;
29 import javax.jms.QueueSession JavaDoc;
30 import javax.jms.QueueSender JavaDoc;
31 import javax.jms.QueueConnectionFactory JavaDoc;
32 import javax.jms.Queue JavaDoc;
33 import javax.jms.Message JavaDoc;
34 import javax.jms.TextMessage JavaDoc;
35 import javax.jms.JMSException JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import org.jboss.logging.Logger;
38
39 /** An MDB that validates that no more than maxActiveCount MDB instances
40  are active in the onMessage method.
41
42 @author Scott.Stark@jboss.org
43 @version $Revision: 37406 $
44  */

45 public class StrictlyPooledMDB implements MessageDrivenBean JavaDoc, MessageListener JavaDoc
46 {
47    private static Logger log = Logger.getLogger(StrictlyPooledMDB.class);
48    /** The class wide max count of instances allows */
49    private static int maxActiveCount = 5;
50    /** The class wide count of instances active in business code */
51    private static int activeCount;
52
53    private MessageDrivenContext JavaDoc ctx = null;
54    private QueueConnection JavaDoc queConn;
55    private QueueSession JavaDoc session;
56    private QueueSender JavaDoc sender;
57
58    private static synchronized int incActiveCount()
59    {
60       return activeCount ++;
61    }
62    private static synchronized int decActiveCount()
63    {
64       return activeCount --;
65    }
66
67    public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
68       throws EJBException JavaDoc
69    {
70       this.ctx = ctx;
71       try
72       {
73          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
74          Integer JavaDoc i = (Integer JavaDoc) iniCtx.lookup("java:comp/env/maxActiveCount");
75          maxActiveCount = i.intValue();
76          QueueConnectionFactory JavaDoc factory = (QueueConnectionFactory JavaDoc) iniCtx.lookup("java:/ConnectionFactory");
77          queConn = factory.createQueueConnection();
78          session = queConn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
79          Queue JavaDoc queue = (Queue JavaDoc) iniCtx.lookup("queue/B");
80          sender = session.createSender(queue);
81       }
82       catch(Exception JavaDoc e)
83       {
84          log.error("Setup failure", e);
85          throw new EJBException JavaDoc("Setup failure", e);
86       }
87    }
88
89    public void ejbCreate()
90    {
91    }
92
93    public void ejbRemove()
94    {
95       try
96       {
97          if( sender != null )
98             sender.close();
99          if( session != null )
100             session.close();
101          if( queConn != null )
102             queConn.close();
103       }
104       catch(Exception JavaDoc e)
105       {
106          log.error("Failed to close JMS resources", e);
107       }
108    }
109
110    public void onMessage(Message JavaDoc message)
111    {
112       int count = incActiveCount();
113       log.debug("Begin onMessage, activeCount="+count+", ctx="+ctx);
114       try
115       {
116          Message JavaDoc reply = null;
117          if( count > maxActiveCount )
118          {
119             String JavaDoc msg = "IllegalState, activeCount > maxActiveCount, "
120                   + count + " > " + maxActiveCount;
121             // Send an exception
122
Exception JavaDoc e = new IllegalStateException JavaDoc(msg);
123             reply = session.createObjectMessage(e);
124          }
125          else
126          {
127             TextMessage JavaDoc tm = (TextMessage JavaDoc) message;
128             // Send an ack
129
reply = session.createTextMessage("Recevied msg="+tm.getText());
130          }
131          Thread.currentThread().sleep(1000);
132          sender.send(reply);
133       }
134       catch(JMSException JavaDoc e)
135       {
136          log.error("Failed to send error message", e);
137       }
138       catch(InterruptedException JavaDoc e)
139       {
140       }
141       finally
142       {
143          count = decActiveCount();
144          log.debug("End onMessage, activeCount="+count+", ctx="+ctx);
145       }
146    }
147 }
148
Popular Tags