Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2164 from heinrichcohn/enable_jetty_server_thread…
Browse files Browse the repository at this point in the history
…pool_configuration

Enable modification of JettyServer thread pool configuration (for ver 2.x)
  • Loading branch information
apanicker-nflx committed Apr 12, 2021
2 parents dabbacc + 6abe7ab commit 596b7ec
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
import javax.servlet.DispatcherType;
import javax.ws.rs.core.MediaType;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -47,15 +50,25 @@ public class JettyServer implements Lifecycle {

private final int port;
private final boolean join;
private final int maxThreads;
private final int minThreads;

private Server server;


public JettyServer(int port, boolean join) {
this.port = port;
this.join = join;
maxThreads = JettyServerConfiguration.THREAD_POOL_MAX_THREADS_DEFAULT_VALUE;
minThreads = JettyServerConfiguration.THREAD_POOL_MIN_THREADS_DEFAULT_VALUE;
}

public JettyServer(int port, boolean join, int maxThreads, int minThreads) {
this.port = port;
this.join = join;
this.maxThreads = maxThreads;
this.minThreads = minThreads;
}

@Override
public synchronized void start() throws Exception {
Expand All @@ -64,7 +77,10 @@ public synchronized void start() throws Exception {
throw new IllegalStateException("Server is already running");
}

this.server = new Server(port);
this.server = new Server(new QueuedThreadPool(maxThreads,minThreads));
ServerConnector connector = new ServerConnector(this.server);
connector.setPort(port);
this.server.setConnectors(new Connector[]{connector});

ServletContextHandler context = new ServletContextHandler();
context.addFilter(GuiceFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public interface JettyServerConfiguration extends Configuration {
String JOIN_PROPERTY_NAME = "conductor.jetty.server.join";
boolean JOIN_DEFAULT_VALUE = true;

String THREAD_POOL_MIN_THREADS_PROPERTY_NAME="conductor.jetty.server.threadpool.minThreads";
int THREAD_POOL_MIN_THREADS_DEFAULT_VALUE = 8;

String THREAD_POOL_MAX_THREADS_PROPERTY_NAME="conductor.jetty.server.threadpool.maxThreads";
int THREAD_POOL_MAX_THREADS_DEFAULT_VALUE = 200;

default boolean isEnabled(){
return getBooleanProperty(ENABLED_PROPERTY_NAME, ENABLED_DEFAULT_VALUE);
}
Expand All @@ -23,4 +29,12 @@ default int getPort() {
default boolean isJoin(){
return getBooleanProperty(JOIN_PROPERTY_NAME, JOIN_DEFAULT_VALUE);
}

default int getThreadPoolMinThreads() {
return getIntProperty(THREAD_POOL_MIN_THREADS_PROPERTY_NAME,THREAD_POOL_MIN_THREADS_DEFAULT_VALUE);
}

default int getThreadPoolMaxThreads() {
return getIntProperty(THREAD_POOL_MAX_THREADS_PROPERTY_NAME,THREAD_POOL_MAX_THREADS_DEFAULT_VALUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public Optional<JettyServer> get() {
Optional.of(
new JettyServer(
configuration.getPort(),
configuration.isJoin()
configuration.isJoin(),
configuration.getThreadPoolMaxThreads(),
configuration.getThreadPoolMinThreads()
))
: Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.netflix.conductor.jetty.server;

import org.junit.Test;

public class JettyServerTest {
@Test
public void testCreateJettyServerWithDefaultThreadPoolConfiguration() throws Exception {
JettyServer jettyServer = new JettyServer(8083, false);
jettyServer.start();
jettyServer.stop();
}

@Test
public void testCreateJettyServerWithValidThreadPoolConfiguration() throws Exception {
JettyServer jettyServer = new JettyServer(8083, false,20,8);
jettyServer.start();
jettyServer.stop();

}
@Test(expected = IllegalArgumentException.class)
public void testCreateJettyServerWithInvalidThreadPoolConfiguration() throws Exception {
JettyServer jettyServer = new JettyServer(8083, false,8,20);
jettyServer.start();
jettyServer.stop();
}
}

0 comments on commit 596b7ec

Please sign in to comment.