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.ejb.CreateException ; 28 import javax.naming.InitialContext ; 29 import javax.naming.NamingException ; 30 import org.jboss.logging.Logger; 31 32 38 public class StrictlyPooledCreateExceptionBean implements SessionBean 39 { 40 private static Logger log = Logger.getLogger(StatelessSessionBean.class); 41 42 private static boolean ejbCreateFailed = false; 43 44 private static int maxActiveCount = 5; 45 46 private static int activeCount; 47 48 private SessionContext ctx; 49 50 private static synchronized int incActiveCount() 51 { 52 return activeCount ++; 53 } 54 private static synchronized int decActiveCount() 55 { 56 return activeCount --; 57 } 58 59 public void ejbCreate() throws CreateException 60 { 61 log.info("ejbCreate"); 62 if( ejbCreateFailed == false ) 64 { 65 ejbCreateFailed = true; 66 throw new CreateException ("First ejbCreate Failure"); 67 } 68 } 69 public void ejbActivate() 70 { 71 } 72 73 public void ejbPassivate() 74 { 75 } 76 77 public void ejbRemove() 78 { 79 } 80 81 public void setSessionContext(SessionContext ctx) 82 { 83 this.ctx = ctx; 84 try 85 { 86 InitialContext iniCtx = new InitialContext (); 87 Integer i = (Integer ) iniCtx.lookup("java:comp/env/maxActiveCount"); 88 maxActiveCount = i.intValue(); 89 } 90 catch(NamingException e) 91 { 92 } 94 } 95 96 public void methodA() 97 { 98 int count = incActiveCount(); 99 log.debug("Begin methodA, activeCount="+count+", ctx="+ctx); 100 try 101 { 102 if( count > maxActiveCount ) 103 { 104 String msg = "IllegalState, activeCount > maxActiveCount, " 105 + count + " > " + maxActiveCount; 106 throw new EJBException (msg); 107 } 108 Thread.sleep(1000); 110 } 111 catch(InterruptedException e) 112 { 113 } 114 finally 115 { 116 count = decActiveCount(); 117 log.debug("End methodA, activeCount="+count+", ctx="+ctx); 118 } 119 } 120 } 121 | Popular Tags |