You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by an...@apache.org on 2014/07/06 14:03:01 UTC

svn commit: r1608192 - /tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/Pool.java

Author: andygumbrecht
Date: Sun Jul  6 12:03:00 2014
New Revision: 1608192

URL: http://svn.apache.org/r1608192
Log:
Flush and sweep before calling stop (which stops the sweeper executor!)

Modified:
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/Pool.java

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/Pool.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/Pool.java?rev=1608192&r1=1608191&r2=1608192&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/Pool.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/Pool.java Sun Jul  6 12:03:00 2014
@@ -28,6 +28,7 @@ import java.util.NoSuchElementException;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.RejectedExecutionException;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.ThreadFactory;
@@ -55,6 +56,7 @@ import static java.util.concurrent.TimeU
  *
  * @version $Rev$ $Date$
  */
+@SuppressWarnings("StatementWithEmptyBody")
 public class Pool<T> {
 
     private final LinkedList<Entry> pool = new LinkedList<Entry>();
@@ -123,7 +125,9 @@ public class Pool<T> {
     }
 
     public Pool start() {
-        if (this.scheduler.compareAndSet(null, Executors.newScheduledThreadPool(1, new SchedulerThreadFactory()))) {
+        final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1, new SchedulerThreadFactory());
+
+        if (this.scheduler.compareAndSet(null, scheduledExecutorService)) {
             this.scheduler.get().scheduleAtFixedRate(sweeper, 0, this.sweepInterval, MILLISECONDS);
         }
         return this;
@@ -149,8 +153,8 @@ public class Pool<T> {
 
     private Executor createExecutor() {
         return new ThreadPoolExecutor(3, 10,
-                60L, TimeUnit.SECONDS,
-                new LinkedBlockingQueue<Runnable>(2), new DaemonThreadFactory("org.apache.openejb.util.Pool", hashCode()));
+            60L, TimeUnit.SECONDS,
+            new LinkedBlockingQueue<Runnable>(2), new DaemonThreadFactory("org.apache.openejb.util.Pool", hashCode()));
     }
 
     private void greater(final String maxName, final long max, final String minName, final long min) {
@@ -172,8 +176,7 @@ public class Pool<T> {
      * @return an entry from the pool or null indicating permission to create and push() an instance into the pool
      * @throws InterruptedException  vm level thread interruption
      * @throws IllegalStateException if a permit could not be acquired
-     * @throws TimeoutException
-     *                               if no instance could be obtained within the timeout
+     * @throws TimeoutException      if no instance could be obtained within the timeout
      */
     public Entry pop(final long timeout, final TimeUnit unit) throws InterruptedException, TimeoutException {
         return pop(timeout, unit, true);
@@ -418,23 +421,27 @@ public class Pool<T> {
     public boolean close(final long timeout, final TimeUnit unit) throws InterruptedException {
         // drain all keys so no new instances will be accepted into the pool
         while (instances.tryAcquire()) {
-            ; //NOPMD
+            //NOPMD
         }
         while (minimum.tryAcquire()) {
-            ; //NOPMD
+            //NOPMD
         }
 
-        // Stop the sweeper thread
-        stop();
-
         // flush and sweep
         flush();
-        sweeper.run();
+        try {
+            sweeper.run();
+        } catch (final RejectedExecutionException e) {
+            //Ignore
+        }
+
+        // Stop the sweeper thread
+        stop();
 
         // Drain all leases
         if (!(available instanceof Overdraft)) {
             while (available.tryAcquire()) {
-                ; //NOPMD
+                //NOPMD
             }
         }
 
@@ -498,8 +505,8 @@ public class Pool<T> {
             }
             final Instance instance = new Instance(obj);
             this.soft = garbageCollection ?
-                    new SoftReference<Instance>(instance) :
-                    new HardReference<Instance>(instance);
+                new SoftReference<Instance>(instance) :
+                new HardReference<Instance>(instance);
             this.version = poolVersion.get();
             this.active.set(instance);
             this.created = now() + offset;
@@ -539,11 +546,11 @@ public class Pool<T> {
         public String toString() {
             final long now = now();
             return "Entry{" +
-                    "min=" + (hard.get() != null) +
-                    ", age=" + (now - created) +
-                    ", idle=" + (now - used) +
-                    ", bean=" + soft.get() +
-                    '}';
+                "min=" + (hard.get() != null) +
+                ", age=" + (now - created) +
+                ", idle=" + (now - used) +
+                ", bean=" + soft.get() +
+                '}';
         }
 
         private class Discarded implements Runnable {
@@ -965,7 +972,7 @@ public class Pool<T> {
         }
     }
 
-    @SuppressWarnings("PMD.UnusedPrivateField")
+    @SuppressWarnings({"PMD.UnusedPrivateField", "UnusedDeclaration"})
     @Managed
     private final class Stats {
 
@@ -1003,7 +1010,7 @@ public class Pool<T> {
         private final int maxSize;
 
         @Managed
-        private long idleTimeout;
+        private final long idleTimeout;
 
         private Stats(final int minSize, final int maxSize, final long idleTimeout) {
             this.minSize = minSize;
@@ -1047,6 +1054,7 @@ public class Pool<T> {
         }
     }
 
+    @SuppressWarnings("UnusedDeclaration")
     public static class Builder<T> {
 
         private int max = 10;
@@ -1107,8 +1115,7 @@ public class Pool<T> {
         /**
          * Alias for pool size
          *
-         * @param max
-         * @return
+         * @param max int
          */
         public void setPoolSize(final int max) {
             setMaxSize(max);