public class KeyedSubmitterSchedulerLimiter
extends java.lang.Object
KeyDistributedScheduler
and
a SubmitterSchedulerLimiter
. This is designed to limit concurrency for a given
thread, but permit more than one thread to run at a time for a given key. If the desired
effect is to have a single thread per key,
KeyDistributedScheduler
is a much better option.
The easiest way to use this class would be to have it distribute out schedulers through
getSubmitterSchedulerForKey(Object)
.
Constructor and Description |
---|
KeyedSubmitterSchedulerLimiter(SubmitterScheduler scheduler,
int maxConcurrency)
Construct a new
KeyedSubmitterSchedulerLimiter providing only the backing scheduler
and the maximum concurrency per unique key. |
KeyedSubmitterSchedulerLimiter(SubmitterScheduler scheduler,
int maxConcurrency,
boolean limitFutureListenersExecution)
Construct a new
KeyedSubmitterSchedulerLimiter providing only the backing scheduler
and the maximum concurrency per unique key. |
KeyedSubmitterSchedulerLimiter(SubmitterScheduler scheduler,
int maxConcurrency,
java.lang.String subPoolName,
boolean addKeyToThreadName)
Construct a new
KeyedSubmitterSchedulerLimiter providing the backing scheduler, the maximum
concurrency per unique key, and how keyed limiter threads should be named. |
KeyedSubmitterSchedulerLimiter(SubmitterScheduler scheduler,
int maxConcurrency,
java.lang.String subPoolName,
boolean addKeyToThreadName,
boolean limitFutureListenersExecution)
Construct a new
KeyedSubmitterSchedulerLimiter providing the backing scheduler, the maximum
concurrency per unique key, and how keyed limiter threads should be named. |
KeyedSubmitterSchedulerLimiter(SubmitterScheduler scheduler,
int maxConcurrency,
java.lang.String subPoolName,
boolean addKeyToThreadName,
boolean limitFutureListenersExecution,
int expectedParallism)
Deprecated.
|
KeyedSubmitterSchedulerLimiter(SubmitterScheduler scheduler,
int maxConcurrency,
java.lang.String subPoolName,
boolean addKeyToThreadName,
int expectedParallism)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
void |
execute(java.lang.Object taskKey,
java.lang.Runnable task)
Provide a task to be run with a given thread key.
|
int |
getMaxConcurrencyPerKey()
Check how many threads may run in parallel for a single unique key.
|
SubmitterExecutor |
getSubmitterExecutorForKey(java.lang.Object taskKey)
Returns an executor implementation where all tasks submitted on this executor will run on the
provided key.
|
SubmitterScheduler |
getSubmitterSchedulerForKey(java.lang.Object taskKey)
Returns a scheduler implementation where all tasks submitted on this scheduler will run on
the provided key.
|
int |
getTrackedKeyCount()
Check how many keys are currently being restricted or monitored.
|
int |
getUnsubmittedTaskCount(java.lang.Object taskKey)
Check how many tasks are currently being limited, and not submitted yet for a given key.
|
java.util.Map<java.lang.Object,java.lang.Integer> |
getUnsubmittedTaskCountMap()
Get a map of all the keys and how many tasks are held back (queued) in each limiter per key.
|
void |
schedule(java.lang.Object taskKey,
java.lang.Runnable task,
long delayInMs)
Schedule a one time task with a given delay.
|
void |
scheduleAtFixedRate(java.lang.Object taskKey,
java.lang.Runnable task,
long initialDelay,
long period)
Schedule a fixed rate recurring task to run.
|
void |
scheduleWithFixedDelay(java.lang.Object taskKey,
java.lang.Runnable task,
long initialDelay,
long recurringDelay)
Schedule a fixed delay recurring task to run.
|
void |
setMaxConcurrencyPerKey(int maxConcurrency)
Updates the concurrency limit for each key.
|
<TT> ListenableFuture<TT> |
submit(java.lang.Object taskKey,
java.util.concurrent.Callable<TT> task)
Submit a callable to be run with a given thread key.
|
ListenableFuture<?> |
submit(java.lang.Object taskKey,
java.lang.Runnable task)
Submit a task to be run with a given thread key.
|
<TT> ListenableFuture<TT> |
submit(java.lang.Object taskKey,
java.lang.Runnable task,
TT result)
Submit a task to be run with a given thread key.
|
<TT> ListenableFuture<TT> |
submitScheduled(java.lang.Object taskKey,
java.util.concurrent.Callable<TT> task,
long delayInMs)
Schedule a
Callable with a given delay. |
ListenableFuture<?> |
submitScheduled(java.lang.Object taskKey,
java.lang.Runnable task,
long delayInMs)
Schedule a task with a given delay.
|
<TT> ListenableFuture<TT> |
submitScheduled(java.lang.Object taskKey,
java.lang.Runnable task,
TT result,
long delayInMs)
Schedule a task with a given delay.
|
public KeyedSubmitterSchedulerLimiter(SubmitterScheduler scheduler, int maxConcurrency)
KeyedSubmitterSchedulerLimiter
providing only the backing scheduler
and the maximum concurrency per unique key. By default this will not rename threads for
tasks executing.scheduler
- Scheduler to execute and schedule tasks onmaxConcurrency
- Maximum concurrency allowed per task keypublic KeyedSubmitterSchedulerLimiter(SubmitterScheduler scheduler, int maxConcurrency, boolean limitFutureListenersExecution)
KeyedSubmitterSchedulerLimiter
providing only the backing scheduler
and the maximum concurrency per unique key. By default this will not rename threads for
tasks executing.
This constructor allows you to specify if listeners /
FutureCallback
's / functions in
ListenableFuture.map(java.util.function.Function)
or
ListenableFuture.flatMap(java.util.function.Function)
should be counted towards the
concurrency limit. Specifying false
will release the limit as soon as the original
task completes. Specifying true
will continue to enforce the limit until all listeners
(without an executor) complete.
scheduler
- Scheduler to execute and schedule tasks onmaxConcurrency
- Maximum concurrency allowed per task keylimitFutureListenersExecution
- true
to include listener / mapped functions towards execution limitpublic KeyedSubmitterSchedulerLimiter(SubmitterScheduler scheduler, int maxConcurrency, java.lang.String subPoolName, boolean addKeyToThreadName)
KeyedSubmitterSchedulerLimiter
providing the backing scheduler, the maximum
concurrency per unique key, and how keyed limiter threads should be named.scheduler
- Scheduler to execute and schedule tasks onmaxConcurrency
- Maximum concurrency allowed per task keysubPoolName
- Name prefix for sub pools, null
to not change thread namesaddKeyToThreadName
- If true
the key's .toString() will be added in the thread namepublic KeyedSubmitterSchedulerLimiter(SubmitterScheduler scheduler, int maxConcurrency, java.lang.String subPoolName, boolean addKeyToThreadName, boolean limitFutureListenersExecution)
KeyedSubmitterSchedulerLimiter
providing the backing scheduler, the maximum
concurrency per unique key, and how keyed limiter threads should be named.
This constructor allows you to specify if listeners /
FutureCallback
's / functions in
ListenableFuture.map(java.util.function.Function)
or
ListenableFuture.flatMap(java.util.function.Function)
should be counted towards the
concurrency limit. Specifying false
will release the limit as soon as the original
task completes. Specifying true
will continue to enforce the limit until all listeners
(without an executor) complete.
scheduler
- Scheduler to execute and schedule tasks onmaxConcurrency
- Maximum concurrency allowed per task keysubPoolName
- Name prefix for sub pools, null
to not change thread namesaddKeyToThreadName
- If true
the key's .toString() will be added in the thread namelimitFutureListenersExecution
- true
to include listener / mapped functions towards execution limit@Deprecated public KeyedSubmitterSchedulerLimiter(SubmitterScheduler scheduler, int maxConcurrency, java.lang.String subPoolName, boolean addKeyToThreadName, int expectedParallism)
KeyedSubmitterSchedulerLimiter(SubmitterScheduler, int, String, boolean)
KeyedSubmitterSchedulerLimiter
providing the backing scheduler, the
maximum concurrency per unique key, and how keyed limiter threads should be named.
The parallelism value should be a factor of how many keys are submitted to the pool during any given period of time. Depending on task execution duration, and quantity of threads executing tasks this value may be able to be smaller than expected. Higher values result in less lock contention, but more memory usage. Most systems will run fine with this anywhere from 4 to 64.
scheduler
- Scheduler to execute and schedule tasks onmaxConcurrency
- Maximum concurrency allowed per task keysubPoolName
- Name prefix for sub pools, null
to not change thread namesaddKeyToThreadName
- If true
the key's .toString() will be added in the thread nameexpectedParallism
- IGNORED AND DEPRECATED@Deprecated public KeyedSubmitterSchedulerLimiter(SubmitterScheduler scheduler, int maxConcurrency, java.lang.String subPoolName, boolean addKeyToThreadName, boolean limitFutureListenersExecution, int expectedParallism)
KeyedSubmitterSchedulerLimiter(SubmitterScheduler, int, String, boolean, boolean)
KeyedSubmitterSchedulerLimiter
providing the backing scheduler, the
maximum concurrency per unique key, and how keyed limiter threads should be named.
The parallelism value should be a factor of how many keys are submitted to the pool during any given period of time. Depending on task execution duration, and quantity of threads executing tasks this value may be able to be smaller than expected. Higher values result in less lock contention, but more memory usage. Most systems will run fine with this anywhere from 4 to 64.
This constructor allows you to specify if listeners /
FutureCallback
's / functions in
ListenableFuture.map(java.util.function.Function)
or
ListenableFuture.flatMap(java.util.function.Function)
should be counted towards the
concurrency limit. Specifying false
will release the limit as soon as the original
task completes. Specifying true
will continue to enforce the limit until all listeners
(without an executor) complete.
scheduler
- Scheduler to execute and schedule tasks onmaxConcurrency
- Maximum concurrency allowed per task keysubPoolName
- Name prefix for sub pools, null
to not change thread namesaddKeyToThreadName
- If true
the key's .toString() will be added in the thread namelimitFutureListenersExecution
- true
to include listener / mapped functions towards execution limitexpectedParallism
- IGNORED AND DEPRECATEDpublic ListenableFuture<?> submitScheduled(java.lang.Object taskKey, java.lang.Runnable task, long delayInMs)
submitScheduled(Object, Runnable, long)
over
schedule(Object, Runnable, long)
. So this should only be used when the future is
necessary.
The Future.get()
method will return null
once the runnable has
completed.
The key is used to identify this threads execution limit. Tasks with matching keys will be
limited concurrent execution to the level returned by getMaxConcurrencyPerKey()
.
See also: SubmitterScheduler.submitScheduled(Runnable, long)
taskKey
- Key to use for identifying execution limittask
- runnable to executedelayInMs
- time in milliseconds to wait to execute taskpublic <TT> ListenableFuture<TT> submitScheduled(java.lang.Object taskKey, java.lang.Runnable task, TT result, long delayInMs)
Future.get()
method will return
the provided result once the runnable has completed.
The key is used to identify this threads execution limit. Tasks with matching keys will be
limited concurrent execution to the level returned by getMaxConcurrencyPerKey()
.
See also: SubmitterScheduler.submitScheduled(Runnable, Object, long)
TT
- type of result returned from the futuretaskKey
- Key to use for identifying execution limittask
- runnable to executeresult
- result to be returned from resulting future .get() when runnable completesdelayInMs
- time in milliseconds to wait to execute taskpublic <TT> ListenableFuture<TT> submitScheduled(java.lang.Object taskKey, java.util.concurrent.Callable<TT> task, long delayInMs)
Callable
with a given delay. This is needed when a result needs to be
consumed from the callable.
The key is used to identify this threads execution limit. Tasks with matching keys will be
limited concurrent execution to the level returned by getMaxConcurrencyPerKey()
.
See also: SubmitterScheduler.submitScheduled(Callable, long)
TT
- type of result returned from the futuretaskKey
- Key to use for identifying execution limittask
- callable to be executeddelayInMs
- time in milliseconds to wait to execute taskpublic void schedule(java.lang.Object taskKey, java.lang.Runnable task, long delayInMs)
The key is used to identify this threads execution limit. Tasks with matching keys will be
limited concurrent execution to the level returned by getMaxConcurrencyPerKey()
.
taskKey
- Key to use for identifying execution limittask
- runnable to executedelayInMs
- time in milliseconds to wait to execute taskpublic void scheduleWithFixedDelay(java.lang.Object taskKey, java.lang.Runnable task, long initialDelay, long recurringDelay)
recurringDelay + runtime
for the provided task.
Unlike ScheduledExecutorService
if the task throws an exception,
subsequent executions are NOT suppressed or prevented. So if the task throws an exception on
every run, the task will continue to be executed at the provided recurring delay (possibly
throwing an exception on each execution).
The key is used to identify this threads execution limit. Tasks with matching keys will be
limited concurrent execution to the level returned by getMaxConcurrencyPerKey()
.
See also: SubmitterScheduler.scheduleWithFixedDelay(Runnable, long, long)
taskKey
- Key to use for identifying execution limittask
- runnable to be executedinitialDelay
- delay in milliseconds until first runrecurringDelay
- delay in milliseconds for running task after last finishpublic void scheduleAtFixedRate(java.lang.Object taskKey, java.lang.Runnable task, long initialDelay, long period)
Unlike ScheduledExecutorService
if the task throws an exception,
subsequent executions are NOT suppressed or prevented. So if the task throws an exception on
every run, the task will continue to be executed at the provided recurring delay (possibly
throwing an exception on each execution).
The key is used to identify this threads execution limit. Tasks with matching keys will be
limited concurrent execution to the level returned by getMaxConcurrencyPerKey()
.
See also: SubmitterScheduler.scheduleAtFixedRate(Runnable, long, long)
taskKey
- Key to use for identifying execution limittask
- runnable to be executedinitialDelay
- delay in milliseconds until first runperiod
- amount of time in milliseconds between the start of recurring executionspublic SubmitterScheduler getSubmitterSchedulerForKey(java.lang.Object taskKey)
taskKey
- object key where equals()
will be used to determine execution threadpublic int getMaxConcurrencyPerKey()
public void setMaxConcurrencyPerKey(int maxConcurrency)
maxConcurrency
- maximum quantity of tasks to run in parallel per keypublic int getTrackedKeyCount()
public int getUnsubmittedTaskCount(java.lang.Object taskKey)
taskKey
- Key which would be limitedpublic java.util.Map<java.lang.Object,java.lang.Integer> getUnsubmittedTaskCountMap()
Because this requires an iteration of all limiters, if only a single limiters unsubmitted
count is needed, use getUnsubmittedTaskCount(Object)
as a cheaper alternative.
public void execute(java.lang.Object taskKey, java.lang.Runnable task)
See also: Executor.execute(Runnable)
taskKey
- object key where equals()
will be used to determine execution threadtask
- Task to be executedpublic ListenableFuture<?> submit(java.lang.Object taskKey, java.lang.Runnable task)
See also: SubmitterExecutor.submit(Runnable)
taskKey
- object key where equals()
will be used to determine execution threadtask
- Task to be executedpublic <TT> ListenableFuture<TT> submit(java.lang.Object taskKey, java.lang.Runnable task, TT result)
TT
- type of result returned from the futuretaskKey
- object key where equals()
will be used to determine execution threadtask
- Runnable to be executedresult
- Result to be returned from future when task completespublic <TT> ListenableFuture<TT> submit(java.lang.Object taskKey, java.util.concurrent.Callable<TT> task)
See also: SubmitterExecutor.submit(Callable)
TT
- type of result returned from the futuretaskKey
- object key where equals()
will be used to determine execution threadtask
- Callable to be executedpublic SubmitterExecutor getSubmitterExecutorForKey(java.lang.Object taskKey)
taskKey
- object key where equals()
will be used to determine execution thread