Skip to content

Commit

Permalink
Merge pull request #4086 from eclipse/jetty-9.4.x-1036-SchedulerThreads
Browse files Browse the repository at this point in the history
Issue #1036 Configure Scheduler
  • Loading branch information
sbordet authored Sep 17, 2019
2 parents bb27fa6 + c37c623 commit f85382a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
6 changes: 5 additions & 1 deletion jetty-server/src/main/config/etc/jetty.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
<!-- =========================================================== -->
<Call name="addBean">
<Arg>
<New class="org.eclipse.jetty.util.thread.ScheduledExecutorScheduler"/>
<New class="org.eclipse.jetty.util.thread.ScheduledExecutorScheduler">
<Arg name="name"><Property name="jetty.scheduler.name"/></Arg>
<Arg name="daemon" type="boolean"><Property name="jetty.scheduler.daemon" default="false" /></Arg>
<Arg name="threads" type="int"><Property name="jetty.scheduler.threads" default="-1" /></Arg>
</New>
</Arg>
</Call>

Expand Down
5 changes: 5 additions & 0 deletions jetty-server/src/main/config/modules/server.mod
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,8 @@ patch-module: servlet.api=lib/jetty-schemas-3.1.jar

## Dump the state of the Jetty server, components, and webapps before shutdown
# jetty.server.dumpBeforeStop=false

## Scheduler Configuration
# jetty.scheduler.name=
# jetty.scheduler.deamon=false
# jetty.scheduler.threads=-1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.annotation.Name;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.component.Dumpable;

Expand All @@ -40,6 +43,8 @@ public class ScheduledExecutorScheduler extends AbstractLifeCycle implements Sch
private final boolean daemon;
private final ClassLoader classloader;
private final ThreadGroup threadGroup;
private final int threads;
private final AtomicInteger count = new AtomicInteger();
private volatile ScheduledThreadPoolExecutor scheduler;
private volatile Thread thread;

Expand All @@ -50,28 +55,48 @@ public ScheduledExecutorScheduler()

public ScheduledExecutorScheduler(String name, boolean daemon)
{
this(name, daemon, Thread.currentThread().getContextClassLoader());
this(name, daemon, null);
}

public ScheduledExecutorScheduler(String name, boolean daemon, ClassLoader threadFactoryClassLoader)
public ScheduledExecutorScheduler(@Name("name") String name, @Name("daemon") boolean daemon, @Name("threads") int threads)
{
this(name, daemon, threadFactoryClassLoader, null);
this(name, daemon, null, null, threads);
}

public ScheduledExecutorScheduler(String name, boolean daemon, ClassLoader threadFactoryClassLoader, ThreadGroup threadGroup)
public ScheduledExecutorScheduler(String name, boolean daemon, ClassLoader classLoader)
{
this.name = name == null ? "Scheduler-" + hashCode() : name;
this(name, daemon, classLoader, null);
}

public ScheduledExecutorScheduler(String name, boolean daemon, ClassLoader classLoader, ThreadGroup threadGroup)
{
this(name, daemon, classLoader, threadGroup, -1);
}

/**
* @param name The name of the scheduler threads or null for automatic name
* @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 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
* 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)
{
this.name = StringUtil.isBlank(name) ? "Scheduler-" + hashCode() : name;
this.daemon = daemon;
this.classloader = threadFactoryClassLoader == null ? Thread.currentThread().getContextClassLoader() : threadFactoryClassLoader;
this.classloader = classLoader == null ? Thread.currentThread().getContextClassLoader() : classLoader;
this.threadGroup = threadGroup;
this.threads = threads;
}

@Override
protected void doStart() throws Exception
{
scheduler = new ScheduledThreadPoolExecutor(1, r ->
int size = threads > 0 ? threads : 1;
scheduler = new ScheduledThreadPoolExecutor(size, r ->
{
Thread thread = ScheduledExecutorScheduler.this.thread = new Thread(threadGroup, r, name);
Thread thread = ScheduledExecutorScheduler.this.thread = new Thread(threadGroup, r, name + "-" + count.incrementAndGet());
thread.setDaemon(daemon);
thread.setContextClassLoader(classloader);
return thread;
Expand Down

0 comments on commit f85382a

Please sign in to comment.