allow configuring a custom ScheduledExecutorService into ScheduledExecutorScheduler
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
This commit is contained in:
parent
842bbe7fcb
commit
0928204db7
|
@ -14,6 +14,7 @@
|
||||||
package org.eclipse.jetty.util.thread;
|
package org.eclipse.jetty.util.thread;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
@ -43,7 +44,7 @@ public class ScheduledExecutorScheduler extends AbstractLifeCycle implements Sch
|
||||||
private final ThreadGroup threadGroup;
|
private final ThreadGroup threadGroup;
|
||||||
private final int threads;
|
private final int threads;
|
||||||
private final AtomicInteger count = new AtomicInteger();
|
private final AtomicInteger count = new AtomicInteger();
|
||||||
private volatile ScheduledThreadPoolExecutor scheduler;
|
private volatile ScheduledExecutorService scheduler;
|
||||||
private volatile Thread thread;
|
private volatile Thread thread;
|
||||||
|
|
||||||
public ScheduledExecutorScheduler()
|
public ScheduledExecutorScheduler()
|
||||||
|
@ -76,7 +77,7 @@ public class ScheduledExecutorScheduler extends AbstractLifeCycle implements Sch
|
||||||
* @param daemon True if scheduler threads should be daemon
|
* @param daemon True if scheduler threads should be daemon
|
||||||
* @param classLoader The classloader to run the threads with or null to use the current thread context classloader
|
* @param classLoader The classloader to run the threads with or null to use the current thread context classloader
|
||||||
* @param threadGroup The threadgroup to use or null for no thread group
|
* @param threadGroup The threadgroup to use or null for no thread group
|
||||||
* @param threads The number of threads to pass to the the core {@link ScheduledThreadPoolExecutor} or -1 for a
|
* @param threads The number of threads to pass to the core {@link ScheduledExecutorService} or -1 for a
|
||||||
* heuristic determined number of threads.
|
* heuristic determined number of threads.
|
||||||
*/
|
*/
|
||||||
public ScheduledExecutorScheduler(@Name("name") String name, @Name("daemon") boolean daemon, @Name("classLoader") ClassLoader classLoader, @Name("threadGroup") ThreadGroup threadGroup, @Name("threads") int threads)
|
public ScheduledExecutorScheduler(@Name("name") String name, @Name("daemon") boolean daemon, @Name("classLoader") ClassLoader classLoader, @Name("threadGroup") ThreadGroup threadGroup, @Name("threads") int threads)
|
||||||
|
@ -88,11 +89,26 @@ public class ScheduledExecutorScheduler extends AbstractLifeCycle implements Sch
|
||||||
this.threads = threads;
|
this.threads = threads;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param scheduledExecutorService the core {@link ScheduledExecutorService} to be used
|
||||||
|
*/
|
||||||
|
public ScheduledExecutorScheduler(ScheduledExecutorService scheduledExecutorService)
|
||||||
|
{
|
||||||
|
this.name = null;
|
||||||
|
this.daemon = false;
|
||||||
|
this.classloader = null;
|
||||||
|
this.threadGroup = null;
|
||||||
|
this.threads = 0;
|
||||||
|
this.scheduler = scheduledExecutorService;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doStart() throws Exception
|
protected void doStart() throws Exception
|
||||||
|
{
|
||||||
|
if (this.scheduler == null)
|
||||||
{
|
{
|
||||||
int size = threads > 0 ? threads : 1;
|
int size = threads > 0 ? threads : 1;
|
||||||
scheduler = new ScheduledThreadPoolExecutor(size, r ->
|
ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(size, r ->
|
||||||
{
|
{
|
||||||
Thread thread = ScheduledExecutorScheduler.this.thread = new Thread(threadGroup, r, name + "-" + count.incrementAndGet());
|
Thread thread = ScheduledExecutorScheduler.this.thread = new Thread(threadGroup, r, name + "-" + count.incrementAndGet());
|
||||||
thread.setDaemon(daemon);
|
thread.setDaemon(daemon);
|
||||||
|
@ -100,6 +116,8 @@ public class ScheduledExecutorScheduler extends AbstractLifeCycle implements Sch
|
||||||
return thread;
|
return thread;
|
||||||
});
|
});
|
||||||
scheduler.setRemoveOnCancelPolicy(true);
|
scheduler.setRemoveOnCancelPolicy(true);
|
||||||
|
this.scheduler = scheduler;
|
||||||
|
}
|
||||||
super.doStart();
|
super.doStart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,13 +126,15 @@ public class ScheduledExecutorScheduler extends AbstractLifeCycle implements Sch
|
||||||
{
|
{
|
||||||
scheduler.shutdownNow();
|
scheduler.shutdownNow();
|
||||||
super.doStop();
|
super.doStop();
|
||||||
|
// If name is set to null, this means we got the scheduler from the constructor.
|
||||||
|
if (name != null)
|
||||||
scheduler = null;
|
scheduler = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Task schedule(Runnable task, long delay, TimeUnit unit)
|
public Task schedule(Runnable task, long delay, TimeUnit unit)
|
||||||
{
|
{
|
||||||
ScheduledThreadPoolExecutor s = scheduler;
|
ScheduledExecutorService s = scheduler;
|
||||||
if (s == null)
|
if (s == null)
|
||||||
return () -> false;
|
return () -> false;
|
||||||
ScheduledFuture<?> result = s.schedule(task, delay, unit);
|
ScheduledFuture<?> result = s.schedule(task, delay, unit);
|
||||||
|
|
Loading…
Reference in New Issue