aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-application/src/main/java/org/sonar/application
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2014-07-30 13:34:50 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2014-07-30 13:34:50 +0200
commit26bfa823f22237d7d40cb927f84c840a2d72b37e (patch)
treed1ce365d317e8f6de83ab1236c54a680fddabf96 /sonar-application/src/main/java/org/sonar/application
parent41e12aea73c6a4917d8d877c36f3421e03064e46 (diff)
downloadsonarqube-26bfa823f22237d7d40cb927f84c840a2d72b37e.tar.gz
sonarqube-26bfa823f22237d7d40cb927f84c840a2d72b37e.zip
SONAR-4898 improve reliability
Diffstat (limited to 'sonar-application/src/main/java/org/sonar/application')
-rw-r--r--sonar-application/src/main/java/org/sonar/application/App.java155
1 files changed, 60 insertions, 95 deletions
diff --git a/sonar-application/src/main/java/org/sonar/application/App.java b/sonar-application/src/main/java/org/sonar/application/App.java
index fea8916ccbe..85b674157b1 100644
--- a/sonar-application/src/main/java/org/sonar/application/App.java
+++ b/sonar-application/src/main/java/org/sonar/application/App.java
@@ -21,125 +21,78 @@ package org.sonar.application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.sonar.process.JmxUtils;
import org.sonar.process.Monitor;
-import org.sonar.process.Process;
import org.sonar.process.ProcessMXBean;
+import org.sonar.process.ProcessUtils;
import org.sonar.process.ProcessWrapper;
-import javax.management.InstanceAlreadyExistsException;
-import javax.management.MBeanRegistrationException;
-import javax.management.MBeanServer;
-import javax.management.NotCompliantMBeanException;
-import java.lang.management.ManagementFactory;
-
public class App implements ProcessMXBean {
- static final String PROCESS_NAME = "SonarQube";
-
static final String SONAR_WEB_PROCESS = "web";
static final String SONAR_SEARCH_PROCESS = "search";
-
private final Installation installation;
- private Monitor monitor;
+
+ private final Monitor monitor = new Monitor();
private ProcessWrapper elasticsearch;
private ProcessWrapper server;
public App(Installation installation) throws Exception {
this.installation = installation;
-
- Thread shutdownHook = new Thread(new Runnable() {
- @Override
- public void run() {
- terminate();
- }
- });
- Runtime.getRuntime().addShutdownHook(shutdownHook);
-
- MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
- try {
- mbeanServer.registerMBean(this, Process.objectNameFor(PROCESS_NAME));
- } catch (InstanceAlreadyExistsException e) {
- throw new IllegalStateException("Process already exists in current JVM", e);
- } catch (MBeanRegistrationException e) {
- throw new IllegalStateException("Could not register process as MBean", e);
- } catch (NotCompliantMBeanException e) {
- throw new IllegalStateException("Process is not a compliant MBean", e);
- }
-
- monitor = new Monitor();
+ JmxUtils.registerMBean(this, "SonarQube");
+ ProcessUtils.addSelfShutdownHook(this);
}
- public void start() {
-
- Logger logger = LoggerFactory.getLogger(getClass());
-
- logger.info("Starting search server");
- elasticsearch = new ProcessWrapper(SONAR_SEARCH_PROCESS)
- .setWorkDir(installation.homeDir())
- .setJmxPort(Integer.parseInt(installation.prop(DefaultSettings.ES_JMX_PORT_KEY)))
- .addJavaOpts(installation.prop(DefaultSettings.ES_JAVA_OPTS_KEY))
- .addJavaOpts(String.format("-Djava.io.tmpdir=%s", installation.tempDir().getAbsolutePath()))
- .addJavaOpts(String.format("-D%s=%s", DefaultSettings.PATH_LOGS_KEY, installation.logsDir().getAbsolutePath()))
- .setClassName("org.sonar.search.ElasticSearch")
- .setProperties(installation.props().encryptedProperties())
- .addClasspath(installation.starPath("lib/common"))
- .addClasspath(installation.starPath("lib/search"))
- .execute();
- monitor.registerProcess(elasticsearch);
- logger.info("Search server is ready");
-
- logger.info("Starting web server");
- server = new ProcessWrapper(SONAR_WEB_PROCESS)
- .setWorkDir(installation.homeDir())
- .setJmxPort(Integer.parseInt(installation.prop(DefaultSettings.WEB_JMX_PORT_KEY)))
- .addJavaOpts(installation.prop(DefaultSettings.WEB_JAVA_OPTS_KEY))
- .addJavaOpts(DefaultSettings.WEB_JAVA_OPTS_APPENDED_VAL)
- .addJavaOpts(String.format("-Djava.io.tmpdir=%s", installation.tempDir().getAbsolutePath()))
- .addJavaOpts(String.format("-D%s=%s", DefaultSettings.PATH_LOGS_KEY, installation.logsDir().getAbsolutePath()))
- .setClassName("org.sonar.server.app.ServerProcess")
- .setProperties(installation.props().encryptedProperties())
- .addClasspath(installation.starPath("extensions/jdbc-driver/mysql"))
- .addClasspath(installation.starPath("extensions/jdbc-driver/mssql"))
- .addClasspath(installation.starPath("extensions/jdbc-driver/oracle"))
- .addClasspath(installation.starPath("extensions/jdbc-driver/postgresql"))
- .addClasspath(installation.starPath("lib/common"))
- .addClasspath(installation.starPath("lib/server"))
- .execute();
- monitor.registerProcess(server);
- logger.info("Web server is ready");
-
- monitor.start();
-
+ public void start() throws InterruptedException {
try {
- monitor.join();
- } catch (InterruptedException e) {
- // TODO ignore ?
-
+ Logger logger = LoggerFactory.getLogger(getClass());
+ monitor.start();
+
+ elasticsearch = new ProcessWrapper(SONAR_SEARCH_PROCESS)
+ .setWorkDir(installation.homeDir())
+ .setJmxPort(Integer.parseInt(installation.prop(DefaultSettings.ES_JMX_PORT_KEY)))
+ .addJavaOpts(installation.prop(DefaultSettings.ES_JAVA_OPTS_KEY))
+ .addJavaOpts(String.format("-Djava.io.tmpdir=%s", installation.tempDir().getAbsolutePath()))
+ .addJavaOpts(String.format("-Dsonar.path.logs=%s", installation.logsDir().getAbsolutePath()))
+ .setClassName("org.sonar.search.SearchServer")
+ .setProperties(installation.props().encryptedProperties())
+ .addClasspath(installation.starPath("lib/common"))
+ .addClasspath(installation.starPath("lib/search"));
+ if (elasticsearch.execute()) {
+ monitor.registerProcess(elasticsearch);
+ if (elasticsearch.waitForReady()) {
+ logger.info("Search server is ready");
+
+ server = new ProcessWrapper(SONAR_WEB_PROCESS)
+ .setWorkDir(installation.homeDir())
+ .setJmxPort(Integer.parseInt(installation.prop(DefaultSettings.WEB_JMX_PORT_KEY)))
+ .addJavaOpts(installation.prop(DefaultSettings.WEB_JAVA_OPTS_KEY))
+ .addJavaOpts(DefaultSettings.WEB_JAVA_OPTS_APPENDED_VAL)
+ .addJavaOpts(String.format("-Djava.io.tmpdir=%s", installation.tempDir().getAbsolutePath()))
+ .setClassName("org.sonar.server.app.WebServer")
+ .setProperties(installation.props().encryptedProperties())
+ .addClasspath(installation.starPath("extensions/jdbc-driver/mysql"))
+ .addClasspath(installation.starPath("extensions/jdbc-driver/mssql"))
+ .addClasspath(installation.starPath("extensions/jdbc-driver/oracle"))
+ .addClasspath(installation.starPath("extensions/jdbc-driver/postgresql"))
+ .addClasspath(installation.starPath("lib/common"))
+ .addClasspath(installation.starPath("lib/server"));
+ if (server.execute()) {
+ monitor.registerProcess(server);
+ if (server.waitForReady()) {
+ logger.info("Web server is ready");
+ monitor.join();
+ }
+ }
+ }
+ }
} finally {
- logger.debug("Closing App because monitor is gone.");
terminate();
}
}
@Override
- public void terminate() {
- Logger logger = LoggerFactory.getLogger(getClass());
-
- if (monitor != null) {
- monitor.interrupt();
- monitor = null;
- if (elasticsearch != null) {
- elasticsearch.terminate();
- }
- if (server != null) {
- server.terminate();
- }
- logger.info("Stopping SonarQube main process");
- }
- }
-
- @Override
public boolean isReady() {
return monitor.isAlive();
}
@@ -149,6 +102,18 @@ public class App implements ProcessMXBean {
return System.currentTimeMillis();
}
+ @Override
+ public void terminate() {
+ monitor.terminate();
+ monitor.interrupt();
+ if (server != null) {
+ server.terminate();
+ }
+ if (elasticsearch != null) {
+ elasticsearch.terminate();
+ }
+ }
+
public static void main(String[] args) throws Exception {
Installation installation = new Installation();
new AppLogging().configure(installation);