From: David Gageot Date: Mon, 5 Nov 2012 11:10:00 +0000 (+0100) Subject: SONAR-3830 Configure embedded Jetty's thread pool X-Git-Tag: 3.4~396 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=597f22fd86971529077f5e854e72c09be1dc7b73;p=sonarqube.git SONAR-3830 Configure embedded Jetty's thread pool --- diff --git a/sonar-application/src/main/assembly/conf/sonar.properties b/sonar-application/src/main/assembly/conf/sonar.properties index c392d500b58..381505f5b8f 100644 --- a/sonar-application/src/main/assembly/conf/sonar.properties +++ b/sonar-application/src/main/assembly/conf/sonar.properties @@ -22,6 +22,9 @@ # Log HTTP requests. Deactivated by default. #sonar.web.jettyRequestLogs: ../../logs/jetty-yyyy_mm_dd.request.log +#sonar.web.jetty.threads.min: 5 +#sonar.web.jetty.threads.max: 50 +#sonar.web.jetty.threads.low: 10 #----------------------------------------------------------------------- # DATABASE diff --git a/sonar-application/src/main/java/org/sonar/application/JettyEmbedder.java b/sonar-application/src/main/java/org/sonar/application/JettyEmbedder.java index 53cdc2aadba..55c6639a85e 100644 --- a/sonar-application/src/main/java/org/sonar/application/JettyEmbedder.java +++ b/sonar-application/src/main/java/org/sonar/application/JettyEmbedder.java @@ -35,6 +35,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Properties; public class JettyEmbedder { @@ -42,22 +43,23 @@ public class JettyEmbedder { private final String host; private final int port; private final String contextPath; + private final Properties configuration; - public JettyEmbedder(String host, int port, String contextPath, URL configurationURL) throws Exception { + public JettyEmbedder(String host, int port, String contextPath, URL configurationURL, Properties configuration) throws Exception { this.host = host.trim(); this.port = port; this.contextPath = contextPath; + this.configuration = configuration; server = new Server(); if (configurationURL == null) { configureProgrammatically(); - } else { System.setProperty("jetty.host", this.host); System.setProperty("jetty.port", String.valueOf(port)); System.setProperty("jetty.context", contextPath); - XmlConfiguration configuration = new XmlConfiguration(configurationURL); - configuration.configure(server); + XmlConfiguration xmlConfiguration = new XmlConfiguration(configurationURL); + xmlConfiguration.configure(server); } } @@ -65,7 +67,7 @@ public class JettyEmbedder { * for tests */ JettyEmbedder(String host, int port) throws Exception { - this(host, port, null, null); + this(host, port, null, null, new Properties()); } public void start() throws Exception { @@ -104,9 +106,9 @@ public class JettyEmbedder { private void configureServer() { QueuedThreadPool threadPool = new QueuedThreadPool(); - threadPool.setMinThreads(5); - threadPool.setMaxThreads(50); - threadPool.setLowThreads(10); + threadPool.setMinThreads(getIntProperty("sonar.web.jetty.threads.min", 5)); + threadPool.setMaxThreads(getIntProperty("sonar.web.jetty.threads.max", 50)); + threadPool.setLowThreads(getIntProperty("sonar.web.jetty.threads.low", 10)); server.setThreadPool(threadPool); SelectChannelConnector connector = new SelectChannelConnector(); connector.setHost(host); @@ -121,33 +123,41 @@ public class JettyEmbedder { server.setGracefulShutdown(1000); } + private int getIntProperty(String name, int defaultValue) { + String value = configuration.getProperty(name); + if (null == value) { + return defaultValue; + } + + return Integer.parseInt(value); + } + final String getPluginsClasspath(String pluginsPathFromClassloader) throws URISyntaxException, IOException { - final URL resource = getClass().getResource(pluginsPathFromClassloader); - if (resource != null) { - File pluginsDir = new File(resource.toURI()); - List paths = new ArrayList(); - paths.add(pluginsDir.getCanonicalPath() + System.getProperty("file.separator")); - - Collection files = FileUtils.listFiles(pluginsDir, new String[] {"jar"}, false); - if (files != null) { - for (File file : files) { - paths.add(file.getCanonicalPath()); - } - } - return join(paths, ","); + URL resource = getClass().getResource(pluginsPathFromClassloader); + if (resource == null) { + return null; } - return null; + + List paths = new ArrayList(); + + File pluginsDir = new File(resource.toURI()); + paths.add(pluginsDir.getCanonicalPath() + System.getProperty("file.separator")); + + Collection files = FileUtils.listFiles(pluginsDir, new String[] {"jar"}, false); + for (File file : files) { + paths.add(file.getCanonicalPath()); + } + + return join(paths, ","); } private String join(List paths, String separator) { StringBuilder sb = new StringBuilder(); - boolean first = true; for (String path : paths) { - if (!first) { + if (sb.length() > 0) { sb.append(separator); } sb.append(path); - first = false; } return sb.toString(); } diff --git a/sonar-application/src/main/java/org/sonar/application/StartServer.java b/sonar-application/src/main/java/org/sonar/application/StartServer.java index 8c2f48181b0..6d241327c42 100644 --- a/sonar-application/src/main/java/org/sonar/application/StartServer.java +++ b/sonar-application/src/main/java/org/sonar/application/StartServer.java @@ -42,7 +42,7 @@ public final class StartServer { String host = configuration.getProperty("sonar.web.host", DEFAULT_WEB_HOST); int port = Integer.parseInt(configuration.getProperty("sonar.web.port", "" + DEFAULT_WEB_PORT)); String context = configuration.getProperty("sonar.web.context", DEFAULT_WEB_CONTEXT); - JettyEmbedder jetty = new JettyEmbedder(host, port, context, StartServer.class.getResource("/jetty.xml")); + JettyEmbedder jetty = new JettyEmbedder(host, port, context, StartServer.class.getResource("/jetty.xml"), configuration); configureRequestLogs(jetty, configuration); jetty.start(); diff --git a/sonar-application/src/test/java/org/sonar/application/JettyEmbedderTest.java b/sonar-application/src/test/java/org/sonar/application/JettyEmbedderTest.java index 9e79ce86f19..1804d4cd4ff 100644 --- a/sonar-application/src/test/java/org/sonar/application/JettyEmbedderTest.java +++ b/sonar-application/src/test/java/org/sonar/application/JettyEmbedderTest.java @@ -22,38 +22,42 @@ package org.sonar.application; import org.apache.commons.lang.StringUtils; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import java.util.Properties; -public class JettyEmbedderTest { +import static org.fest.assertions.Assertions.assertThat; +public class JettyEmbedderTest { @Test public void xmlConfigurationShouldAccessToSomeSystemProperties() throws Exception { // useful to set the port into the XML file - new JettyEmbedder("127.0.0.1", 9999, "/", JettyEmbedderTest.class.getResource("/org/sonar/application/jetty-test.xml")); - assertEquals("127.0.0.1", System.getProperty("jetty.host")); - assertEquals("9999", System.getProperty("jetty.port")); - assertEquals("/", System.getProperty("jetty.context")); + new JettyEmbedder("127.0.0.1", 9999, "/", JettyEmbedderTest.class.getResource("/org/sonar/application/jetty-test.xml"), new Properties()); + + assertThat(System.getProperty("jetty.host")).isEqualTo("127.0.0.1"); + assertThat(System.getProperty("jetty.port")).isEqualTo("9999"); + assertThat(System.getProperty("jetty.context")).isEqualTo("/"); } @Test public void shouldUseDefaultConfigurationIfNoXml() throws Exception { JettyEmbedder jetty = new JettyEmbedder("1.2.3.4", 9999); - assertEquals(1, jetty.getServer().getConnectors().length); - assertEquals(9999, jetty.getServer().getConnectors()[0].getPort()); - assertEquals("1.2.3.4", jetty.getServer().getConnectors()[0].getHost()); + + assertThat(jetty.getServer().getConnectors()).hasSize(1); + assertThat(jetty.getServer().getConnectors()[0].getPort()).isEqualTo(9999); + assertThat(jetty.getServer().getConnectors()[0].getHost()).isEqualTo("1.2.3.4"); } @Test public void shouldLoadPluginsClasspath() throws Exception { JettyEmbedder jetty = new JettyEmbedder("127.0.0.1", 9999); + String classpath = jetty.getPluginsClasspath("/org/sonar/application/JettyEmbedderTest/shouldLoadPluginsClasspath"); classpath = StringUtils.replaceChars(classpath, "\\", "/"); - assertTrue(classpath, classpath.contains("org/sonar/application/JettyEmbedderTest/shouldLoadPluginsClasspath/plugin1.jar")); - assertTrue(classpath, classpath.contains("org/sonar/application/JettyEmbedderTest/shouldLoadPluginsClasspath/plugin2.jar")); + assertThat(classpath).contains("org/sonar/application/JettyEmbedderTest/shouldLoadPluginsClasspath/plugin1.jar"); + assertThat(classpath).contains("org/sonar/application/JettyEmbedderTest/shouldLoadPluginsClasspath/plugin1.jar"); + assertThat(classpath).contains("org/sonar/application/JettyEmbedderTest/shouldLoadPluginsClasspath/plugin2.jar"); // important : directories end with / - assertTrue(classpath, classpath.contains("org/sonar/application/JettyEmbedderTest/shouldLoadPluginsClasspath/,")); + assertThat(classpath).contains("org/sonar/application/JettyEmbedderTest/shouldLoadPluginsClasspath/,"); } }