]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3830 Configure embedded Jetty's thread pool
authorDavid Gageot <david@gageot.net>
Mon, 5 Nov 2012 11:10:00 +0000 (12:10 +0100)
committerDavid Gageot <david@gageot.net>
Tue, 6 Nov 2012 08:21:00 +0000 (09:21 +0100)
sonar-application/src/main/assembly/conf/sonar.properties
sonar-application/src/main/java/org/sonar/application/JettyEmbedder.java
sonar-application/src/main/java/org/sonar/application/StartServer.java
sonar-application/src/test/java/org/sonar/application/JettyEmbedderTest.java

index c392d500b58d5c2691733fb77a13b62704aa7cdd..381505f5b8fcb1805f5ae69763f8db0e0366c466 100644 (file)
@@ -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
index 53cdc2aadba8451ecfb488de476a0a1337c63a96..55c6639a85e409b95e113833a4348cf5a286611b 100644 (file)
@@ -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<String> paths = new ArrayList<String>();
-      paths.add(pluginsDir.getCanonicalPath() + System.getProperty("file.separator"));
-
-      Collection<File> 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<String> paths = new ArrayList<String>();
+
+    File pluginsDir = new File(resource.toURI());
+    paths.add(pluginsDir.getCanonicalPath() + System.getProperty("file.separator"));
+
+    Collection<File> files = FileUtils.listFiles(pluginsDir, new String[] {"jar"}, false);
+    for (File file : files) {
+      paths.add(file.getCanonicalPath());
+    }
+
+    return join(paths, ",");
   }
 
   private String join(List<String> 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();
   }
index 8c2f48181b0a52e0db87deedaf328f806f50ae21..6d241327c42141a284866dbbc0c0052febe160d6 100644 (file)
@@ -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();
index 9e79ce86f194c68dafcdcfc360e22377e63bcb13..1804d4cd4fff7bfd9a472cfb64eaaac0f001f896 100644 (file)
@@ -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/,");
   }
 }