KickJava   Java API By Example, From Geeks To Geeks.

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


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.EJBException JavaDoc;
25 import javax.ejb.SessionContext JavaDoc;
26 import javax.ejb.SessionBean JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import org.jboss.logging.Logger;
30
31 /** A session bean that asserts the count of instances active in the
32  * methodA business method do not exceed the maxActiveCount.
33  *
34  * @author Scott.Stark@jboss.org
35  * @version $Revision: 37406 $
36  */

37 public class StrictlyPooledSessionBean implements SessionBean JavaDoc
38 {
39    private static Logger log = Logger.getLogger(StatelessSessionBean.class);
40    /** The class wide max count of instances allows */
41    private static int maxActiveCount = 5;
42    /** The class wide count of instances active in business code */
43    private static int activeCount;
44
45    private SessionContext JavaDoc ctx;
46
47    private static synchronized int incActiveCount()
48    {
49       return activeCount ++;
50    }
51    private static synchronized int decActiveCount()
52    {
53       return activeCount --;
54    }
55
56    public void ejbCreate()
57    {
58    }
59    public void ejbActivate()
60    {
61    }
62
63    public void ejbPassivate()
64    {
65    }
66
67    public void ejbRemove()
68    {
69    }
70
71    public void setSessionContext(SessionContext JavaDoc ctx)
72    {
73       this.ctx = ctx;
74       try
75       {
76          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
77          Integer JavaDoc i = (Integer JavaDoc) iniCtx.lookup("java:comp/env/maxActiveCount");
78          maxActiveCount = i.intValue();
79       }
80       catch(NamingException JavaDoc e)
81       {
82          // Use default count of 5
83
}
84    }
85
86    public void methodA()
87    {
88       int count = incActiveCount();
89       log.debug("Begin methodA, activeCount="+count+", ctx="+ctx);
90       try
91       {
92          if( count > maxActiveCount )
93          {
94             String JavaDoc msg = "IllegalState, activeCount > maxActiveCount, "
95                   + count + " > " + maxActiveCount;
96             throw new EJBException JavaDoc(msg);
97          }
98          // Sleep to let the client thread pile up
99
Thread.currentThread().sleep(1000);
100       }
101       catch(InterruptedException JavaDoc e)
102       {
103       }
104       finally
105       {
106          count = decActiveCount();
107          log.debug("End methodA, activeCount="+count+", ctx="+ctx);
108       }
109    }
110 }
111
Popular Tags