1 22 package org.jboss.test.cts.ejb; 23 24 import javax.ejb.EJBException ; 25 import javax.ejb.SessionContext ; 26 import javax.ejb.SessionBean ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingException ; 29 import org.jboss.logging.Logger; 30 31 37 public class StrictlyPooledSessionBean implements SessionBean 38 { 39 private static Logger log = Logger.getLogger(StatelessSessionBean.class); 40 41 private static int maxActiveCount = 5; 42 43 private static int activeCount; 44 45 private SessionContext 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 ctx) 72 { 73 this.ctx = ctx; 74 try 75 { 76 InitialContext iniCtx = new InitialContext (); 77 Integer i = (Integer ) iniCtx.lookup("java:comp/env/maxActiveCount"); 78 maxActiveCount = i.intValue(); 79 } 80 catch(NamingException e) 81 { 82 } 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 msg = "IllegalState, activeCount > maxActiveCount, " 95 + count + " > " + maxActiveCount; 96 throw new EJBException (msg); 97 } 98 Thread.currentThread().sleep(1000); 100 } 101 catch(InterruptedException e) 102 { 103 } 104 finally 105 { 106 count = decActiveCount(); 107 log.debug("End methodA, activeCount="+count+", ctx="+ctx); 108 } 109 } 110 } 111 | Popular Tags |