[jboss-cvs] JBossAS SVN: r91831 - in projects/ejb3/trunk/core/src: test/java/org/jboss/ejb3/core/test and 2 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Jul 30 09:04:12 EDT 2009
Author: wolfc
Date: 2009-07-30 09:04:12 -0400 (Thu, 30 Jul 2009)
New Revision: 91831
Added:
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/NoopBean.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/NoopLocal.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/unit/
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/unit/ThreadLocalPoolStatsTestCase.java
Modified:
projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/pool/ThreadlocalPool.java
Log:
EJBTHREE-1703: dynamic maxSize on ThreadlocalPool
Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/pool/ThreadlocalPool.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/pool/ThreadlocalPool.java 2009-07-30 12:49:41 UTC (rev 91830)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/pool/ThreadlocalPool.java 2009-07-30 13:04:12 UTC (rev 91831)
@@ -42,7 +42,6 @@
protected Pool pool = new InfinitePool();
protected WeakThreadLocal<BeanContext> currentBeanContext = new WeakThreadLocal<BeanContext>();
private int inUse = 0;
- private int maxSize = 30;
public ThreadlocalPool()
{
@@ -120,7 +119,6 @@
public void initialize(Container container, int maxSize, long timeout)
{
pool.initialize(container, maxSize, timeout);
- this.maxSize = maxSize;
}
public void release(BeanContext ctx)
@@ -157,7 +155,7 @@
public int getAvailableCount()
{
- return maxSize - inUse;
+ return getMaxSize() - inUse;
}
public int getCreateCount()
@@ -167,7 +165,9 @@
public int getMaxSize()
{
- return maxSize;
+ // the thread local pool dynamically grows for new threads
+ // if a bean is reentrant it'll grow and shrink over the reentrant call
+ return getCurrentSize();
}
public int getRemoveCount()
@@ -182,6 +182,7 @@
public void setMaxSize(int maxSize)
{
- this.maxSize = maxSize;
+ //this.maxSize = maxSize;
+ log.warn("EJBTHREE-1703: setting a max size on ThreadlocalPool is bogus");
}
}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/NoopBean.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/NoopBean.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/NoopBean.java 2009-07-30 13:04:12 UTC (rev 91831)
@@ -0,0 +1,85 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.ejbthree1703;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+
+import java.util.concurrent.BrokenBarrierException;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.TimeoutException;
+
+import javax.annotation.Resource;
+import javax.ejb.SessionContext;
+import javax.ejb.Stateless;
+
+import org.jboss.logging.Logger;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at Stateless
+public class NoopBean implements NoopLocal
+{
+ private static final Logger log = Logger.getLogger(NoopBean.class);
+
+ @Resource
+ private SessionContext ctx;
+
+ public void noop()
+ {
+ // really do nothing :-)
+ }
+
+ public void reentrant(int count, CyclicBarrier entree, CyclicBarrier exit)
+ {
+ if(count == 0)
+ {
+ shoo(entree, exit);
+ return;
+ }
+
+ ctx.getBusinessObject(NoopLocal.class).reentrant(count - 1, entree, exit);
+ }
+
+ public void shoo(CyclicBarrier entree, CyclicBarrier exit)
+ {
+ log.debug("entering barriers");
+ try
+ {
+ entree.await(5, SECONDS);
+ exit.await(5, SECONDS);
+ }
+ catch(BrokenBarrierException e)
+ {
+ throw new RuntimeException(e);
+ }
+ catch(InterruptedException e)
+ {
+ throw new RuntimeException(e);
+ }
+ catch(TimeoutException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/NoopLocal.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/NoopLocal.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/NoopLocal.java 2009-07-30 13:04:12 UTC (rev 91831)
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.ejbthree1703;
+
+import java.util.concurrent.CyclicBarrier;
+
+import javax.ejb.Local;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at Local
+public interface NoopLocal
+{
+ void noop();
+
+ void reentrant(int count, CyclicBarrier entree, CyclicBarrier exit);
+
+ void shoo(CyclicBarrier entree, CyclicBarrier exit);
+}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/unit/ThreadLocalPoolStatsTestCase.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/unit/ThreadLocalPoolStatsTestCase.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1703/unit/ThreadLocalPoolStatsTestCase.java 2009-07-30 13:04:12 UTC (rev 91831)
@@ -0,0 +1,210 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.ejbthree1703.unit;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static junit.framework.Assert.assertEquals;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import org.jboss.deployers.spi.DeploymentException;
+import org.jboss.ejb3.core.test.common.AbstractEJB3TestCase;
+import org.jboss.ejb3.core.test.ejbthree1703.NoopBean;
+import org.jboss.ejb3.core.test.ejbthree1703.NoopLocal;
+import org.jboss.ejb3.pool.Pool;
+import org.jboss.ejb3.session.SessionContainer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+
+/**
+ * (created - removed) = (inUse + available)
+ *
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class ThreadLocalPoolStatsTestCase extends AbstractEJB3TestCase
+{
+ private SessionContainer container;
+
+ @After
+ public void after()
+ {
+ undeployEjb(container);
+ }
+
+ @Before
+ public void before() throws DeploymentException
+ {
+ container = deploySessionEjb(NoopBean.class);
+ }
+
+ /*
+ @BeforeClass
+ public static void beforeClass() throws Exception
+ {
+ AbstractEJB3TestCase.beforeClass();
+
+ container = deploySessionEjb(NoopBean.class);
+ }
+ */
+
+ @Test
+ public void testCreateCount() throws Exception
+ {
+ NoopLocal bean = lookup("NoopBean/local", NoopLocal.class);
+ bean.noop();
+ assertEquals(1, container.getPool().getCreateCount());
+ assertEquals("the pool should have grown", 1, container.getPool().getMaxSize());
+ assertEquals("the instance should be available", 1, container.getPool().getAvailableCount());
+ }
+
+ /**
+ * InUse is calculated as: inUse = maxSize - available
+ */
+ @Test
+ public void testInUse() throws Exception
+ {
+ final NoopLocal bean = lookup("NoopBean/local", NoopLocal.class);
+ ExecutorService service = Executors.newSingleThreadExecutor();
+ // exercise the thread
+ /*
+ Future<Void> future = service.submit(new Callable<Void> () {
+ public Void call() throws Exception
+ {
+ bean.noop();
+ return null;
+ }
+ });
+ future.get(5, SECONDS);
+ assertEquals(1, container.getPool().getCreateCount());
+ assertEquals("the pool should have grown", 1, container.getPool().getMaxSize());
+ assertEquals("the instance should be available", 1, container.getPool().getAvailableCount());
+ */
+
+ final CyclicBarrier entree = new CyclicBarrier(2);
+ final CyclicBarrier exit = new CyclicBarrier(2);
+ Future<Void> future = service.submit(new Callable<Void> () {
+ public Void call() throws Exception
+ {
+ bean.shoo(entree, exit);
+ return null;
+ }
+ });
+
+ entree.await(5, SECONDS);
+
+ Pool pool = container.getPool();
+ assertEquals(1, pool.getCreateCount());
+ assertEquals("the pool should have grown", 1, pool.getMaxSize());
+ int inUse = pool.getMaxSize() - pool.getAvailableCount();
+ assertEquals("the instance should be in use", 1, inUse);
+
+ exit.await(5, SECONDS);
+
+ future.get(5, SECONDS);
+
+ service.shutdown();
+ }
+
+ @Test
+ public void testMaxSize()
+ {
+ Pool pool = container.getPool();
+ int actual = pool.getMaxSize();
+ assertEquals("a ThreadLocalPool starts empty and grows", 0, actual);
+ }
+
+ @Test
+ public void testReentrant() throws Exception
+ {
+ final NoopLocal bean = lookup("NoopBean/local", NoopLocal.class);
+ ExecutorService service = Executors.newSingleThreadExecutor();
+ final CyclicBarrier entree = new CyclicBarrier(2);
+ final CyclicBarrier exit = new CyclicBarrier(2);
+ Future<Void> future = service.submit(new Callable<Void> () {
+ public Void call() throws Exception
+ {
+ bean.reentrant(1, entree, exit);
+ return null;
+ }
+ });
+
+ entree.await(5, SECONDS);
+
+ Pool pool = container.getPool();
+ assertEquals(2, pool.getCreateCount());
+ assertEquals("the pool should have grown", 2, pool.getMaxSize());
+ int inUse = pool.getMaxSize() - pool.getAvailableCount();
+ assertEquals("two instances should be in use", 2, inUse);
+
+ exit.await(5, SECONDS);
+
+ future.get(5, SECONDS);
+
+ assertEquals("the pool should have shrunk", 1, pool.getMaxSize());
+ assertEquals("the pool should have 1 available instance", 1, pool.getAvailableCount());
+
+ service.shutdown();
+ }
+
+ @Test
+ public void testThreeThreads() throws Exception
+ {
+ final NoopLocal bean = lookup("NoopBean/local", NoopLocal.class);
+ ExecutorService service = Executors.newFixedThreadPool(2);
+
+ final CyclicBarrier entree = new CyclicBarrier(3);
+ final CyclicBarrier exit = new CyclicBarrier(3);
+ Callable<Void> task = new Callable<Void> () {
+ public Void call() throws Exception
+ {
+ bean.shoo(entree, exit);
+ return null;
+ }
+ };
+ Future<?> futures[] = new Future[2];
+ futures[0] = service.submit(task);
+ futures[1] = service.submit(task);
+
+ entree.await(5, SECONDS);
+
+ Pool pool = container.getPool();
+ assertEquals(2, pool.getCreateCount());
+ assertEquals("the pool should have grown", 2, pool.getMaxSize());
+ int inUse = pool.getMaxSize() - pool.getAvailableCount();
+ assertEquals("the instance should be in use", 2, inUse);
+
+ exit.await(5, SECONDS);
+
+ futures[0].get(5, SECONDS);
+ futures[1].get(5, SECONDS);
+
+ service.shutdown();
+
+ }
+}
More information about the jboss-cvs-commits
mailing list