forked from OpenIdentityPlatform/OpenAM
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AME-12693 Name ScheduledExecutorService threads
- Loading branch information
1 parent
8106332
commit bd3e03e
Showing
28 changed files
with
388 additions
and
187 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
...it-context/src/main/java/org/forgerock/openam/audit/context/AMExecutorServiceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* 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: "Portions copyright [year] [name of copyright owner]". | ||
* | ||
* Copyright 2015-2016 ForgeRock AS. | ||
*/ | ||
package org.forgerock.openam.audit.context; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.forgerock.util.thread.ExecutorServiceFactory; | ||
|
||
/** | ||
* Responsible for filtering the API of {@ExecutorServiceFactory} to exclude any | ||
* methods that do not offer a means of setting thread names and to ensure that | ||
* new methods added to {@ExecutorServiceFactory} are always called via | ||
* {@link AuditRequestContextPropagatingExecutorServiceFactory}. | ||
* | ||
* @see ExecutorServiceFactory | ||
* @see AuditRequestContextPropagatingExecutorServiceFactory | ||
* | ||
* @since 14.0.0 | ||
*/ | ||
public interface AMExecutorServiceFactory { | ||
|
||
/** | ||
* Generates a ScheduledExecutorService which has been pre-registered with the | ||
* ShutdownManager. | ||
* | ||
* @see java.util.concurrent.Executors#newScheduledThreadPool(int) | ||
* | ||
* @param poolSize The size of the ScheduledExecutorService thread pool. | ||
* @param threadNamePrefix The thread name prefix to use when generating new threads. | ||
* @return A non null ScheduledExecutorService | ||
*/ | ||
ScheduledExecutorService createScheduledService(int poolSize, String threadNamePrefix); | ||
|
||
/** | ||
* Creates a fixed size Thread Pool ExecutorService which has been pre-registered with | ||
* the {@link org.forgerock.util.thread.listener.ShutdownManager}. | ||
* | ||
* @param pool The size of the pool to create. | ||
* @param factory The {@link java.util.concurrent.ThreadFactory} used to generate new threads. | ||
* @return Non null. | ||
*/ | ||
ExecutorService createFixedThreadPool(int pool, ThreadFactory factory); | ||
|
||
/** | ||
* Create a fixed size Thread Pool ExecutorService using the provided name as the prefix | ||
* of the thread names. | ||
* | ||
* @see #createFixedThreadPool(int, java.util.concurrent.ThreadFactory) | ||
* | ||
* @param pool Size of the fixed pool. | ||
* @param threadNamePrefix The thread name prefix to use when generating new threads. | ||
* @return Non null. | ||
*/ | ||
ExecutorService createFixedThreadPool(int pool, String threadNamePrefix); | ||
|
||
/** | ||
* Generates a Cached Thread Pool ExecutorService which has been pre-registered with the | ||
* ShutdownManager. The provided ThreadFactory is used by the service when creating Threads. | ||
* | ||
* @see java.util.concurrent.Executors#newCachedThreadPool(java.util.concurrent.ThreadFactory) | ||
* | ||
* @param factory The ThreadFactory that will be used when generating threads. May not be null. | ||
* @return A non null ExecutorService. | ||
*/ | ||
ExecutorService createCachedThreadPool(ThreadFactory factory); | ||
|
||
/** | ||
* Generates a Cached Thread Pool ExecutorService using the provided name as a prefix | ||
* of the thread names. | ||
* | ||
* @see #createCachedThreadPool(java.util.concurrent.ThreadFactory) | ||
* | ||
* @param threadNamePrefix The thread name prefix to use when generating new threads. | ||
* @return Non null. | ||
*/ | ||
ExecutorService createCachedThreadPool(String threadNamePrefix); | ||
|
||
/** | ||
* Generates a ThreadPoolExecutor with the provided values, and registers that executor as listening for | ||
* shutdown messages. | ||
* | ||
* @param coreSize the number of threads to keep in the pool, even if they are idle | ||
* @param maxSize Max number of threads in the pool | ||
* @param idleTimeout When the number of threads is greater than core, maximum time that excess idle | ||
* threads will wait before terminating | ||
* @param timeoutTimeunit The time unit for the idleTimeout argument | ||
* @param runnables Queue of threads to be run | ||
* @param threadNamePrefix The thread name prefix to use when generating new threads. | ||
* @return a configured ExecutorService, registered to listen to shutdown messages. | ||
*/ | ||
ExecutorService createThreadPool(int coreSize, int maxSize, long idleTimeout, | ||
TimeUnit timeoutTimeunit, BlockingQueue<Runnable> runnables, String threadNamePrefix); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...text/src/main/java/org/forgerock/openam/audit/context/ExtendedExecutorServiceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* 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: "Portions copyright [year] [name of copyright owner]". | ||
* | ||
* Copyright 2015-2016 ForgeRock AS. | ||
*/ | ||
package org.forgerock.openam.audit.context; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.forgerock.util.Reject; | ||
import org.forgerock.util.thread.ExecutorServiceFactory; | ||
import org.forgerock.util.thread.listener.ShutdownListener; | ||
import org.forgerock.util.thread.listener.ShutdownManager; | ||
|
||
/** | ||
* Subclass of {@link ExecutorServiceFactory} which allows {@link ScheduledExecutorService} threads to be named. | ||
* <p> | ||
* This is a temporary class which is only needed as Commons has reached feature freeze. When changes to Commons | ||
* are permitted, the single public method in this class should be rolled up into {@link ExecutorServiceFactory} | ||
* (COMMONS-137) and this subclass should be deleted (AME-12776). | ||
*/ | ||
class ExtendedExecutorServiceFactory extends ExecutorServiceFactory implements AMExecutorServiceFactory { | ||
|
||
private final ShutdownManager shutdownManager; | ||
|
||
/** | ||
* Create an instance of the factory. | ||
* | ||
* @param shutdownManager Required to ensure each ExecutorService will be shutdown. | ||
*/ | ||
ExtendedExecutorServiceFactory(ShutdownManager shutdownManager) { | ||
super(shutdownManager); | ||
this.shutdownManager = shutdownManager; | ||
} | ||
|
||
@Override | ||
public ScheduledExecutorService createScheduledService(int poolSize, String threadNamePrefix) { | ||
final ScheduledExecutorService service = | ||
Executors.newScheduledThreadPool(poolSize, new NamedThreadFactory(threadNamePrefix)); | ||
registerShutdown(service); | ||
return service; | ||
} | ||
|
||
@Override | ||
public ExecutorService createThreadPool(int coreSize, int maxSize, long idleTimeout, | ||
TimeUnit timeoutTimeunit, BlockingQueue<Runnable> runnables, String threadNamePrefix) { | ||
Reject.ifTrue(coreSize < 0); | ||
Reject.ifTrue(maxSize < coreSize || maxSize <= 0); | ||
Reject.ifTrue(idleTimeout < 0); | ||
|
||
ExecutorService service = new ThreadPoolExecutor(coreSize, maxSize, idleTimeout, timeoutTimeunit, | ||
runnables, new NamedThreadFactory(threadNamePrefix)); | ||
registerShutdown(service); | ||
return service; | ||
} | ||
|
||
/** | ||
* Registers a listener to trigger shutdown of the ExecutorService. | ||
* @param service Non null ExecutorService to register. | ||
*/ | ||
private void registerShutdown(final ExecutorService service) { | ||
shutdownManager.addShutdownListener( | ||
new ShutdownListener() { | ||
public void shutdown() { | ||
service.shutdownNow(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Used to generate threads with a provided name. Each new thread will | ||
* have its generated number appended to the end of it, in the form -X, where | ||
* X is incremented once for each thread created. | ||
*/ | ||
private class NamedThreadFactory implements ThreadFactory { | ||
|
||
private final AtomicInteger count = new AtomicInteger(0); | ||
private final String name; | ||
|
||
public NamedThreadFactory(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Thread newThread(Runnable r) { | ||
return new Thread(r, name + "-" + count.getAndIncrement()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.