Simplify stop & cleanups
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/de09ee0b Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/de09ee0b Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/de09ee0b Branch: refs/heads/master Commit: de09ee0b365a28d0a347732a3c8347d0c52b5108 Parents: 7f63e5e Author: AndyGee <[email protected]> Authored: Fri Mar 3 21:23:28 2017 +0100 Committer: AndyGee <[email protected]> Committed: Fri Mar 3 21:23:28 2017 +0100 ---------------------------------------------------------------------- .../main/java/org/apache/openejb/util/Pool.java | 53 ++++++++------------ .../java/org/apache/openejb/util/PoolTest.java | 1 + 2 files changed, 21 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/de09ee0b/container/openejb-core/src/main/java/org/apache/openejb/util/Pool.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/util/Pool.java b/container/openejb-core/src/main/java/org/apache/openejb/util/Pool.java index b2bc9b2..877598b 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/util/Pool.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/util/Pool.java @@ -152,15 +152,15 @@ public class Pool<T> { } public void stop() { - final ScheduledFuture<?> future = this.future.get(); - if (future != null && this.future.compareAndSet(future, null) + final ScheduledFuture<?> future = this.future.getAndSet(null); + if (future != null && !future.isDone() && !future.isCancelled() && !future.cancel(false)) { Logger.getLogger(Pool.class.getName()).log(Level.WARNING, "Pool scheduler task termination timeout expired"); } - final ScheduledExecutorService scheduler = this.scheduler.get(); - if (scheduler != null && this.scheduler.compareAndSet(scheduler, null)) { + final ScheduledExecutorService scheduler = this.scheduler.getAndSet(null); + if (scheduler != null) { scheduler.shutdown(); try { if (!scheduler.awaitTermination(10, SECONDS)) { // should last something like 0s max since we killed the task @@ -370,7 +370,7 @@ public class Pool<T> { try { if (entry == null) { - return added; + return false; } if (!sweeper) { @@ -467,18 +467,8 @@ public class Pool<T> { public boolean close(final long timeout, final TimeUnit unit) throws InterruptedException { - final ScheduledExecutorService ses = this.scheduler.getAndSet(null); - - if (null != ses) { - try { - ses.shutdown(); - if(!ses.awaitTermination(timeout, unit)){ - Logger.getLogger(Pool.class.getName()).log(Level.WARNING, "Pool scheduler termination timeout expired"); - } - } catch (final Exception e) { - //no-op - } - } + // Stop the sweeper thread + stop(); // drain all keys so no new instances will be accepted into the pool while (instances.tryAcquire()) { @@ -498,9 +488,6 @@ public class Pool<T> { //Ignore } - // Stop the sweeper thread - stop(); - // Drain all leases if (!(available instanceof Overdraft)) { while (available.tryAcquire()) { @@ -544,12 +531,12 @@ public class Pool<T> { private long used; private final int version; private final SoftReference<Instance> soft; - private final AtomicReference<Instance> hard = new AtomicReference<Instance>(); + private final AtomicReference<Instance> hard = new AtomicReference<>(); // Added this so the soft reference isn't collected // after the Entry instance is returned from a "pop" method // Also acts as an "inUse" boolean - private final AtomicReference<Instance> active = new AtomicReference<Instance>(); + private final AtomicReference<Instance> active = new AtomicReference<>(); /** * Constructor is private so that it is impossible for an Entry object @@ -569,8 +556,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) : + new HardReference<>(instance); this.version = poolVersion.get(); this.active.set(instance); this.created = now() + offset; @@ -685,14 +672,14 @@ public class Pool<T> { final long now = now(); - final List<Entry> entries = new ArrayList<Entry>(max); + final List<Entry> entries = new ArrayList<>(max); // Pull all the entries from the pool try { while (true) { final Entry entry = pop(0, MILLISECONDS, false); if (entry == null) { - push(entry, true); + push(null, true); break; } entries.add(entry); @@ -703,7 +690,7 @@ public class Pool<T> { // pool has been drained } - final List<Expired> expiredList = new ArrayList<Expired>(max); + final List<Expired> expiredList = new ArrayList<>(max); { // Expire aged instances, enforce pool "versioning" @@ -793,7 +780,7 @@ public class Pool<T> { // If there are any "min" pool instances left over // we need to queue up creation of a replacement - final List<Expired> replace = new ArrayList<Expired>(); + final List<Expired> replace = new ArrayList<>(); for (final Expired expired : expiredList) { executor.execute(expired.entry.active().discard(expired.event)); @@ -804,14 +791,14 @@ public class Pool<T> { } for (int i = 0; i < replace.size(); i++) { - final long offset = maxAge > 0 ? (long) (maxAge / replace.size() * i * maxAgeOffset) % maxAge : 0l; + final long offset = maxAge > 0 ? (long) (maxAge / replace.size() * i * maxAgeOffset) % maxAge : 0L; executor.execute(new Replace(replace.get(i).entry, offset)); } } } - public static enum Event { + public enum Event { FULL, IDLE, AGED, FLUSHED, GC } @@ -1246,11 +1233,11 @@ public class Pool<T> { this.scheduledExecutorService = scheduledExecutorService; } + @SuppressWarnings("unchecked") public Pool<T> build() { - //noinspection unchecked final Pool pool = new Pool(max, min, strict, maxAge.getTime(MILLISECONDS), idleTimeout.getTime(MILLISECONDS), interval.getTime(MILLISECONDS), executor, supplier, replaceAged, maxAgeOffset, this.garbageCollection, replaceFlushed); - if (scheduledExecutorService != null) { - pool.scheduler.set(scheduledExecutorService); + if (this.scheduledExecutorService != null) { + pool.scheduler.set(this.scheduledExecutorService); } return pool; } http://git-wip-us.apache.org/repos/asf/tomee/blob/de09ee0b/container/openejb-core/src/test/java/org/apache/openejb/util/PoolTest.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/java/org/apache/openejb/util/PoolTest.java b/container/openejb-core/src/test/java/org/apache/openejb/util/PoolTest.java index 6dc08cd..fbde94f 100644 --- a/container/openejb-core/src/test/java/org/apache/openejb/util/PoolTest.java +++ b/container/openejb-core/src/test/java/org/apache/openejb/util/PoolTest.java @@ -321,6 +321,7 @@ public class PoolTest extends TestCase { final long start = System.currentTimeMillis(); assertTrue(pool.close(10, TimeUnit.SECONDS)); + assertFalse(pool.running()); final long time = System.currentTimeMillis() - start; // All instances should have been removed
