[prev in list] [next in list] [prev in thread] [next in thread] 

List:       forgerock-openam-commit
Subject:    [CommitOpenAM] [14906] branches/AME-7692_noRestartsAuth/openam: AME-7689 Making scripting thread poo
From:       noreply () forgerock ! org
Date:       2015-07-31 14:21:28
Message-ID: 20150731142128.CBD273F8E4 () sources ! internal ! forgerock ! com
[Download RAW message or body]

[Attachment #2 (text/html)]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14906] branches/AME-7692_noRestartsAuth/openam: AME-7689 Making scripting \
thread pool queue size hot-swappable.</title> </head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: \
verdana,arial,helvetica,sans-serif; font-size: 10pt;  } #msg dl a { font-weight: \
bold} #msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: \
bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: \
6px; } #logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em \
0; } #logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg \
h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; } \
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; \
} #logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: \
-1.5em; padding-left: 1.5em; } #logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em \
1em 0 1em; background: white;} #logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid \
#fa0; border-bottom: 1px solid #fa0; background: #fff; } #logmsg table th { \
text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted \
#fa0; } #logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: \
0.2em 0.5em; } #logmsg table thead th { text-align: center; border-bottom: 1px solid \
#fa0; } #logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: \
6px; } #patch { width: 100%; }
#patch h4 {font-family: \
verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
 #patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, \
#patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins \
{background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del \
{background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, \
                .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a \
href="http://sources.forgerock.org/changelog/openam/?cs=14906">14906</a></dd> \
<dt>Author</dt> <dd>BrianB</dd> <dt>Date</dt> <dd>2015-07-31 15:21:28 +0100 (Fri, 31 \
Jul 2015)</dd> </dl>

<h3>Log Message</h3>
<pre>AME-7689 Making scripting thread pool queue size hot-swappable.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branchesAME7692_noRestartsAuthopenamopenamauditopenamauditcontextsrcmain \
javaorgforgerockopenamauditcontextAuditRequestContextPropagatingExecutorServicejava">b \
ranches/AME-7692_noRestartsAuth/openam/openam-audit/openam-audit-context/src/main/java \
/org/forgerock/openam/audit/context/AuditRequestContextPropagatingExecutorService.java</a></li>
 <li><a href="#branchesAME7692_noRestartsAuthopenamopenamscriptingsrcmainjavaorgforger \
ockopenamscriptingThreadPoolScriptEvaluatorjava">branches/AME-7692_noRestartsAuth/open \
am/openam-scripting/src/main/java/org/forgerock/openam/scripting/ThreadPoolScriptEvaluator.java</a></li>
 <li><a href="#branchesAME7692_noRestartsAuthopenamopenamscriptingsrcmainjavaorgforger \
ockopenamscriptingguiceScriptingGuiceModulejava">branches/AME-7692_noRestartsAuth/open \
am/openam-scripting/src/main/java/org/forgerock/openam/scripting/guice/ScriptingGuiceModule.java</a></li>
 </ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#branchesAME7692_noRestartsAuthopenamopenamsharedsrcmainjavaorgforgerocko \
penamsharedconcurrencyResizableLinkedBlockingQueuejava">branches/AME-7692_noRestartsAu \
th/openam/openam-shared/src/main/java/org/forgerock/openam/shared/concurrency/ResizableLinkedBlockingQueue.java</a></li>
 <li><a href="#branchesAME7692_noRestartsAuthopenamopenamsharedsrcmainjavaorgforgerock \
openamsharedconcurrencyResizableSemaphorejava">branches/AME-7692_noRestartsAuth/openam \
/openam-shared/src/main/java/org/forgerock/openam/shared/concurrency/ResizableSemaphore.java</a></li>
 <li><a href="#branchesAME7692_noRestartsAuthopenamopenamsharedsrctestjavaorgforgerock \
openamsharedconcurrencyResizableLinkedBlockingQueueTestjava">branches/AME-7692_noResta \
rtsAuth/openam/openam-shared/src/test/java/org/forgerock/openam/shared/concurrency/ResizableLinkedBlockingQueueTest.java</a></li>
 <li><a href="#branchesAME7692_noRestartsAuthopenamopenamsharedsrctestjavaorgforgerock \
openamsharedconcurrencyResizableSemaphoreTestjava">branches/AME-7692_noRestartsAuth/op \
enam/openam-shared/src/test/java/org/forgerock/openam/shared/concurrency/ResizableSemaphoreTest.java</a></li>
 </ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branchesAME7692_noRestartsAuthopenamopenamauditopenamauditcontextsrcmainjavaorg \
forgerockopenamauditcontextAuditRequestContextPropagatingExecutorServicejava"></a> \
<div class="modfile"><h4>Modified: \
branches/AME-7692_noRestartsAuth/openam/openam-audit/openam-audit-context/src/main/jav \
a/org/forgerock/openam/audit/context/AuditRequestContextPropagatingExecutorService.java \
(14905 => 14906)</h4> <pre class="diff"><span>
<span class="info">--- \
branches/AME-7692_noRestartsAuth/openam/openam-audit/openam-audit-context/src/main/jav \
a/org/forgerock/openam/audit/context/AuditRequestContextPropagatingExecutorService.java	2015-07-31 \
                13:34:25 UTC (rev 14905)
+++ branches/AME-7692_noRestartsAuth/openam/openam-audit/openam-audit-context/src/main \
/java/org/forgerock/openam/audit/context/AuditRequestContextPropagatingExecutorService.java	2015-07-31 \
14:21:28 UTC (rev 14906) </span><span class="lines">@@ -41,6 +41,15 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     /**
</span><ins>+     * Gets the {@link ExecutorService} delegate assigned to this \
instance. +     *
+     * @return The {@link ExecutorService} delegate.
+     */
+    public ExecutorService getDelegate() {
+        return delegate;
+    }
+
+    /**
</ins><span class="cx">      * {@inheritDoc}
</span><span class="cx">      */
</span><span class="cx">     @Override
</span></span></pre></div>
<a id="branchesAME7692_noRestartsAuthopenamopenamscriptingsrcmainjavaorgforgerockopenamscriptingThreadPoolScriptEvaluatorjava"></a>
 <div class="modfile"><h4>Modified: \
branches/AME-7692_noRestartsAuth/openam/openam-scripting/src/main/java/org/forgerock/openam/scripting/ThreadPoolScriptEvaluator.java \
(14905 => 14906)</h4> <pre class="diff"><span>
<span class="info">--- \
branches/AME-7692_noRestartsAuth/openam/openam-scripting/src/main/java/org/forgerock/openam/scripting/ThreadPoolScriptEvaluator.java	2015-07-31 \
                13:34:25 UTC (rev 14905)
+++ branches/AME-7692_noRestartsAuth/openam/openam-scripting/src/main/java/org/forgerock/openam/scripting/ThreadPoolScriptEvaluator.java	2015-07-31 \
14:21:28 UTC (rev 14906) </span><span class="lines">@@ -16,8 +16,10 @@
</span><span class="cx"> 
</span><span class="cx"> package org.forgerock.openam.scripting;
</span><span class="cx"> 
</span><ins>+import org.forgerock.openam.audit.context.AuditRequestContextPropagatingExecutorService;
 </ins><span class="cx"> import \
org.forgerock.openam.audit.context.ConfigurableExecutorService; </span><span \
class="cx"> import org.forgerock.openam.audit.context.ExecutorServiceConfigurator; \
</span><ins>+import org.forgerock.openam.shared.concurrency.ResizableLinkedBlockingQueue;
 </ins><span class="cx"> import org.forgerock.util.Reject;
</span><span class="cx"> import org.slf4j.Logger;
</span><span class="cx"> import org.slf4j.LoggerFactory;
</span><span class="lines">@@ -121,8 +123,7 @@
</span><span class="cx">      * configuration parameters are not valid, then an error \
is logged and the pool is left in its original </span><span class="cx">      * \
configuration. </span><span class="cx">      * &lt;p/&gt;
</span><del>-     * NB: The queue size is not reconfigurable, so changes will take \
                effect only on server restart. All other settings
-     * can be changed without a restart and the pool will adjust over time to the \
new settings. </del><ins>+     * NB: All settings can be changed without a restart \
and the pool will adjust over time to the new settings. </ins><span class="cx">      \
*/ </span><span class="cx">     private static final class ThreadPoolConfigurator \
implements StandardScriptEngineManager.ConfigurationListener { </span><span \
class="cx">         private final ExecutorService executorService; </span><span \
class="lines">@@ -143,6 +144,19 @@ </span><span class="cx"> 
</span><span class="cx">             try {
</span><span class="cx"> 
</span><ins>+                int newThreadPoolQueueSize = \
newConfiguration.getThreadPoolQueueSize(); +                \
ResizableLinkedBlockingQueue currentQueue = +                        \
((ResizableLinkedBlockingQueue) +                        ((ThreadPoolExecutor)
+                        ((AuditRequestContextPropagatingExecutorService)
+                                executorService)
+                                .getDelegate())
+                                .getQueue());
+                int currentThreadPoolQueueSize = currentQueue.getMaximumQueueSize();
+                if (newThreadPoolQueueSize != currentThreadPoolQueueSize) {
+                    currentQueue.resizeQueue(newThreadPoolQueueSize);
+                }
+
</ins><span class="cx">                 if (delegateConfigurator.getCorePoolSize() != \
newConfiguration.getThreadPoolCoreSize() || </span><span class="cx">                  \
delegateConfigurator.getMaximumPoolSize() != newConfiguration.getThreadPoolMaxSize() \
|| </span><span class="cx">                     \
delegateConfigurator.getKeepAliveTime(TimeUnit.SECONDS) != \
newConfiguration.getThreadPoolIdleTimeoutSeconds()) </span></span></pre></div>
<a id="branchesAME7692_noRestartsAuthopenamopenamscriptingsrcmainjavaorgforgerockopenamscriptingguiceScriptingGuiceModulejava"></a>
 <div class="modfile"><h4>Modified: \
branches/AME-7692_noRestartsAuth/openam/openam-scripting/src/main/java/org/forgerock/openam/scripting/guice/ScriptingGuiceModule.java \
(14905 => 14906)</h4> <pre class="diff"><span>
<span class="info">--- \
branches/AME-7692_noRestartsAuth/openam/openam-scripting/src/main/java/org/forgerock/openam/scripting/guice/ScriptingGuiceModule.java	2015-07-31 \
                13:34:25 UTC (rev 14905)
+++ branches/AME-7692_noRestartsAuth/openam/openam-scripting/src/main/java/org/forgerock/openam/scripting/guice/ScriptingGuiceModule.java	2015-07-31 \
14:21:28 UTC (rev 14906) </span><span class="lines">@@ -25,7 +25,6 @@
</span><span class="cx"> import \
com.google.inject.assistedinject.FactoryModuleBuilder; </span><span class="cx"> \
import com.google.inject.name.Names; </span><span class="cx"> import \
java.util.concurrent.BlockingQueue; </span><del>-import \
java.util.concurrent.LinkedBlockingQueue; </del><span class="cx"> import \
java.util.concurrent.TimeUnit; </span><span class="cx"> import javax.inject.Inject;
</span><span class="cx"> import javax.inject.Named;
</span><span class="lines">@@ -49,6 +48,7 @@
</span><span class="cx"> import \
org.forgerock.openam.scripting.service.ScriptConfigurationService; </span><span \
class="cx"> import org.forgerock.openam.scripting.service.ScriptingService; \
</span><span class="cx"> import \
org.forgerock.openam.scripting.service.ScriptingServiceFactory; </span><ins>+import \
org.forgerock.openam.shared.concurrency.ResizableLinkedBlockingQueue; </ins><span \
class="cx"> import org.forgerock.util.thread.ExecutorServiceFactory; </span><span \
class="cx"> import org.slf4j.Logger; </span><span class="cx"> import \
org.slf4j.LoggerFactory; </span><span class="lines">@@ -204,8 +204,8 @@
</span><span class="cx"> 
</span><span class="cx">     private BlockingQueue&lt;Runnable&gt; \
getThreadPoolQueue(int size) { </span><span class="cx">         return size == \
ScriptEngineConfiguration.UNBOUNDED_QUEUE_SIZE </span><del>-                ? new \
                LinkedBlockingQueue&lt;Runnable&gt;()
-                : new LinkedBlockingQueue&lt;Runnable&gt;(size);
</del><ins>+                ? new ResizableLinkedBlockingQueue&lt;Runnable&gt;()
+                : new ResizableLinkedBlockingQueue&lt;Runnable&gt;(size);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx"> }
</span></span></pre></div>
<a id="branchesAME7692_noRestartsAuthopenamopenamsharedsrcmainjavaorgforgerockopenamsharedconcurrencyResizableLinkedBlockingQueuejava"></a>
 <div class="addfile"><h4>Added: \
branches/AME-7692_noRestartsAuth/openam/openam-shared/src/main/java/org/forgerock/openam/shared/concurrency/ResizableLinkedBlockingQueue.java \
(0 => 14906)</h4> <pre class="diff"><span>
<span class="info">--- \
branches/AME-7692_noRestartsAuth/openam/openam-shared/src/main/java/org/forgerock/openam/shared/concurrency/ResizableLinkedBlockingQueue.java	 \
                (rev 0)
+++ branches/AME-7692_noRestartsAuth/openam/openam-shared/src/main/java/org/forgerock/openam/shared/concurrency/ResizableLinkedBlockingQueue.java	2015-07-31 \
14:21:28 UTC (rev 14906) </span><span class="lines">@@ -0,0 +1,215 @@
</span><ins>+/*
+* The contents of this file are subject to the terms of the Common Development and
+* Distribution License (the License). You may not use this file except in compliance \
with the +* License.
+*
+* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for \
the +* specific language governing permission and limitations under the License.
+*
+* When distributing Covered Software, include this CDDL Header Notice in each file \
and include +* the License file at legal/CDDLv1.0.txt. If applicable, add the \
following below the CDDL +* Header, with the fields enclosed by brackets [] replaced \
by your own identifying +* information: &quot;Portions copyright [year] [name of \
copyright owner]&quot;. +*
+* Copyright 2015 ForgeRock AS.
+*/
+package org.forgerock.openam.shared.concurrency;
+
+import org.forgerock.util.Reject;
+
+import java.util.Collection;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * A {@link LinkedBlockingQueue} implementation which performs exactly as a standard \
{@link LinkedBlockingQueue}, but + * which supports 'resizing' of the queue too.
+ *
+ * @param &lt;E&gt; The type of elements held in this collection.
+ *
+ * @since 13.0.0
+ */
+public class ResizableLinkedBlockingQueue&lt;E&gt; extends \
LinkedBlockingQueue&lt;E&gt; { +
+    private int queueSize;
+    private final ResizableSemaphore availablePlaces;
+
+    /**
+     * {@inheritdoc}
+     */
+    public ResizableLinkedBlockingQueue() {
+        super();
+        queueSize = Integer.MAX_VALUE;
+        availablePlaces = new ResizableSemaphore(queueSize, true);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public ResizableLinkedBlockingQueue(Collection&lt;? extends E&gt; c) {
+        super(c);
+        queueSize = Integer.MAX_VALUE;
+        availablePlaces = new ResizableSemaphore(queueSize, true);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public ResizableLinkedBlockingQueue(int initialCapacity) {
+        super();
+        queueSize = initialCapacity;
+        availablePlaces = new ResizableSemaphore(initialCapacity, true);
+    }
+
+    /**
+     * Set the queue to a size between 0 and Integer.MAX_VALUE. If the newSize \
supplied is a negative integer, then +     * a {@link IllegalArgumentException} will \
be thrown instead. +     *
+     * @param newSize The new size of the queue.
+     * @throws IllegalArgumentException thrown when the given newSize is a minus \
number. +     */
+    public synchronized void resizeQueue(int newSize) throws \
IllegalArgumentException { +        if (newSize &lt; 0) {
+            throw new IllegalArgumentException(&quot;Cannot set queue size to a \
value below zero.&quot;); +        }
+
+        int difference;
+
+        if (newSize &lt; queueSize) {
+            difference = queueSize - newSize;
+            availablePlaces.reducePermits(difference);
+        } else if (newSize &gt; queueSize) {
+            difference = newSize - queueSize;
+            availablePlaces.increasePermits(difference);
+        }
+        queueSize = newSize;
+
+        //Nothing to do if newSize == queueSize.
+    }
+
+    /**
+     * Get the current maximum capacity of the queue. This is not how many elements \
are currently in it, but how +     * many would be in it if it were full.
+     *
+     * @return The current maximum size of the queue.
+     */
+    public int getMaximumQueueSize() {
+        return queueSize;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    @Override
+    public boolean offer(E e) {
+        Reject.ifNull(e, &quot;Element to offer cannot be null.&quot;);
+
+        boolean returnValue;
+
+        if (availablePlaces.tryAcquire()) {
+            if (super.offer(e)) {
+                returnValue = true;
+            } else {
+                returnValue = false;
+                //If the queue.offer(e) fails, the availablePlaces.tryAcquire() will \
still have occurred, so we'll have +                //to release the acquired permit.
+                availablePlaces.release();
+            }
+        } else {
+            returnValue = false;
+        }
+
+        return returnValue;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    @Override
+    public boolean offer(E e, long timeout, TimeUnit unit) throws \
InterruptedException { +        Reject.ifNull(e, &quot;Element to offer cannot be \
null.&quot;); +
+        boolean returnValue;
+
+        if (availablePlaces.tryAcquire(timeout, unit)) {
+            if (super.offer(e)) {
+                returnValue = true;
+            } else {
+                returnValue = false;
+                //If the queue.offer(e) fails, the \
availablePlaces.tryAcquire(timeout, unit) will still have occurred, +                \
//so we'll have to release the acquired permit. +                \
availablePlaces.release(); +            }
+        } else {
+            returnValue = false;
+        }
+
+        return returnValue;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    @Override
+    public E poll() {
+        E returnValue = super.poll();
+        if (returnValue != null) {
+            availablePlaces.release();
+        }
+        return returnValue;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    @Override
+    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
+        E resultValue = super.poll(timeout, unit);
+
+        if (resultValue != null) {
+            availablePlaces.release();
+        }
+        return resultValue;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    @Override
+    public void put(E e) throws InterruptedException {
+        availablePlaces.acquire();
+        super.put(e);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    @Override
+    public int remainingCapacity() {
+        return availablePlaces.availablePermits();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    @Override
+    public boolean remove(Object o) {
+        if (super.remove(o)) {
+            availablePlaces.release();
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    @Override
+    public E take() throws InterruptedException {
+        E result = super.take();
+        availablePlaces.release();
+        return result;
+    }
+
+}
</ins></span></pre></div>
<a id="branchesAME7692_noRestartsAuthopenamopenamsharedsrcmainjavaorgforgerockopenamsharedconcurrencyResizableSemaphorejava"></a>
 <div class="addfile"><h4>Added: \
branches/AME-7692_noRestartsAuth/openam/openam-shared/src/main/java/org/forgerock/openam/shared/concurrency/ResizableSemaphore.java \
(0 => 14906)</h4> <pre class="diff"><span>
<span class="info">--- \
branches/AME-7692_noRestartsAuth/openam/openam-shared/src/main/java/org/forgerock/openam/shared/concurrency/ResizableSemaphore.java	 \
                (rev 0)
+++ branches/AME-7692_noRestartsAuth/openam/openam-shared/src/main/java/org/forgerock/openam/shared/concurrency/ResizableSemaphore.java	2015-07-31 \
14:21:28 UTC (rev 14906) </span><span class="lines">@@ -0,0 +1,61 @@
</span><ins>+/*
+* The contents of this file are subject to the terms of the Common Development and
+* Distribution License (the License). You may not use this file except in compliance \
with the +* License.
+*
+* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for \
the +* specific language governing permission and limitations under the License.
+*
+* When distributing Covered Software, include this CDDL Header Notice in each file \
and include +* the License file at legal/CDDLv1.0.txt. If applicable, add the \
following below the CDDL +* Header, with the fields enclosed by brackets [] replaced \
by your own identifying +* information: &quot;Portions copyright [year] [name of \
copyright owner]&quot;. +*
+* Copyright 2015 ForgeRock AS.
+*/
+package org.forgerock.openam.shared.concurrency;
+
+import java.util.concurrent.Semaphore;
+
+/**
+ * A {@link Semaphore} implementation which performs exactly as a standard {@link \
Semaphore}, but supports 'resizing' + * of the {@link Semaphore} too.
+ *
+ * @since 13.0.0
+ */
+public class ResizableSemaphore extends Semaphore {
+
+    /**
+     * {@inheritdoc}
+     */
+    public ResizableSemaphore(int permits, boolean fair) {
+        super(permits, fair);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public ResizableSemaphore(int permits) {
+        super(permits);
+    }
+
+    /**
+     * Overridden method to stop it being protected.
+     * Decrease the number of available permits by the amount in the supplied \
argument. +     *
+     * {@inheritdoc}
+     */
+    @Override
+    protected void reducePermits(int reduction) {
+        super.reducePermits(reduction);
+    }
+
+    /**
+     * Increase the number of available permits by the amount in the supplied \
argument. +     *
+     * @param increase The number of permits to add.
+     */
+    public void increasePermits(int increase) {
+        this.release(increase);
+    }
+}
</ins></span></pre></div>
<a id="branchesAME7692_noRestartsAuthopenamopenamsharedsrctestjavaorgforgerockopenamsharedconcurrencyResizableLinkedBlockingQueueTestjava"></a>
 <div class="addfile"><h4>Added: \
branches/AME-7692_noRestartsAuth/openam/openam-shared/src/test/java/org/forgerock/openam/shared/concurrency/ResizableLinkedBlockingQueueTest.java \
(0 => 14906)</h4> <pre class="diff"><span>
<span class="info">--- \
branches/AME-7692_noRestartsAuth/openam/openam-shared/src/test/java/org/forgerock/openam/shared/concurrency/ResizableLinkedBlockingQueueTest.java	 \
                (rev 0)
+++ branches/AME-7692_noRestartsAuth/openam/openam-shared/src/test/java/org/forgerock/openam/shared/concurrency/ResizableLinkedBlockingQueueTest.java	2015-07-31 \
14:21:28 UTC (rev 14906) </span><span class="lines">@@ -0,0 +1,30 @@
</span><ins>+package org.forgerock.openam.shared.concurrency;
+
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.*;
+
+public class ResizableLinkedBlockingQueueTest {
+
+    @Test
+    public void testDecreaseQueueSize() throws Exception {
+        ResizableLinkedBlockingQueue&lt;Object&gt; queue = new \
ResizableLinkedBlockingQueue&lt;&gt;(10); +
+        queue.resizeQueue(6);
+
+        int queueSize = queue.getMaximumQueueSize();
+        assertEquals(queueSize, 6, &quot;Expected a decrease to 6 places \
available.&quot;); +    }
+
+    @Test
+    public void testIncreaseQueueSize() throws Exception {
+        ResizableLinkedBlockingQueue&lt;Object&gt; queue = new \
ResizableLinkedBlockingQueue&lt;&gt;(10); +
+        queue.resizeQueue(14);
+
+        int queueSize = queue.getMaximumQueueSize();
+        assertEquals(queueSize, 14, &quot;Expected an increase to 14 places \
available.&quot;); +    }
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="branchesAME7692_noRestartsAuthopenamopenamsharedsrctestjavaorgforgerockopenamsharedconcurrencyResizableSemaphoreTestjava"></a>
 <div class="addfile"><h4>Added: \
branches/AME-7692_noRestartsAuth/openam/openam-shared/src/test/java/org/forgerock/openam/shared/concurrency/ResizableSemaphoreTest.java \
(0 => 14906)</h4> <pre class="diff"><span>
<span class="info">--- \
branches/AME-7692_noRestartsAuth/openam/openam-shared/src/test/java/org/forgerock/openam/shared/concurrency/ResizableSemaphoreTest.java	 \
                (rev 0)
+++ branches/AME-7692_noRestartsAuth/openam/openam-shared/src/test/java/org/forgerock/openam/shared/concurrency/ResizableSemaphoreTest.java	2015-07-31 \
14:21:28 UTC (rev 14906) </span><span class="lines">@@ -0,0 +1,30 @@
</span><ins>+package org.forgerock.openam.shared.concurrency;
+
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.*;
+
+public class ResizableSemaphoreTest {
+
+    private ResizableSemaphore semaphore;
+
+    @Test
+    public void testReducePermits() throws Exception {
+        semaphore = new ResizableSemaphore(10);
+
+        semaphore.reducePermits(6);
+
+        int availablePermits = semaphore.availablePermits();
+        assertEquals(availablePermits, 4, &quot;Expected a decrease to 4 permits \
available.&quot;); +    }
+
+    @Test
+    public void testIncreasePermits() throws Exception {
+        semaphore = new ResizableSemaphore(10);
+
+        semaphore.increasePermits(4);
+
+        int availablePermits = semaphore.availablePermits();
+        assertEquals(availablePermits, 14, &quot;Expected an increase to 14 permits \
available.&quot;); +    }
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>
<div id="footer">Copyright (c) by ForgeRock. All rights reserved.</div>

</body>
</html>



_______________________________________________
CommitOpenAM mailing list
CommitOpenAM@forgerock.org
https://lists.forgerock.org/mailman/listinfo/commitopenam


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic