Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #1036 Configure Scheduler #4086

Merged
merged 3 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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