changeset 351:2f7681265356

Make agent background-able. Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-June/001678.html PR 965
author Roman Kennke <rkennke@redhat.com>
date Fri, 01 Jun 2012 14:35:21 +0200
parents 786f968c3d20
children fb1f9081c11e
files agent/src/main/java/com/redhat/thermostat/agent/AgentApplication.java distribution/config/osgi-export.properties distribution/scripts/thermostat
diffstat 3 files changed, 22 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/agent/src/main/java/com/redhat/thermostat/agent/AgentApplication.java	Thu May 31 23:28:41 2012 +0200
+++ b/agent/src/main/java/com/redhat/thermostat/agent/AgentApplication.java	Fri Jun 01 14:35:21 2012 +0200
@@ -36,8 +36,8 @@
 
 package com.redhat.thermostat.agent;
 
-import java.io.IOException;
 import java.util.Collection;
+import java.util.concurrent.CountDownLatch;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -66,6 +66,10 @@
 import com.redhat.thermostat.common.tools.BasicCommand;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 
+import sun.misc.Signal;
+import sun.misc.SignalHandler;
+
+@SuppressWarnings("restriction")
 public final class AgentApplication extends BasicCommand {
 
     private static final String NAME = "agent";
@@ -139,7 +143,7 @@
             System.exit(Constants.EXIT_BACKEND_LOAD_ERROR);
         }
 
-        Agent agent = new Agent(backendRegistry, configuration, daoFactory);
+        final Agent agent = new Agent(backendRegistry, configuration, daoFactory);
         try {
             logger.fine("Starting agent.");
             agent.start();
@@ -154,15 +158,21 @@
         ctx.getConsole().getOutput().println("Agent id: " + agent.getId());
         ctx.getConsole().getOutput().println("agent started.");
         logger.fine("Agent id: " + agent.getId());
-        
+
+        final CountDownLatch shutdownLatch = new CountDownLatch(1);
+        Signal.handle(new Signal("INT"), new SignalHandler() {
+            public void handle(sun.misc.Signal sig) {
+                agent.stop();
+                logger.fine("Agent stopped.");       
+                shutdownLatch.countDown();
+            }
+        });
         try {
-            System.in.read();
-        } catch (IOException e) {
-            e.printStackTrace();
+            shutdownLatch.await();
+            logger.fine("terimating agent cmd");
+        } catch (InterruptedException e) {
+            return;
         }
-
-        agent.stop();
-        logger.fine("Agent stopped.");       
     }
     
     @Override
--- a/distribution/config/osgi-export.properties	Thu May 31 23:28:41 2012 +0200
+++ b/distribution/config/osgi-export.properties	Fri Jun 01 14:35:21 2012 +0200
@@ -46,9 +46,10 @@
 org.jfree.data.xy
 org.jfree.ui
 sun.swing
+sun.misc
 
 jline=2.5.0
 jline.console=2.5.0
 sun.jvmstat
 sun.jvmstat.monitor
-sun.jvmstat.monitor.event
\ No newline at end of file
+sun.jvmstat.monitor.event
--- a/distribution/scripts/thermostat	Thu May 31 23:28:41 2012 +0200
+++ b/distribution/scripts/thermostat	Fri Jun 01 14:35:21 2012 +0200
@@ -57,7 +57,7 @@
 
 THERM_DIR=${THERMOSTAT_LIBS}
 
-SERVICE_CLASSPATH="${SERVICE_CLASSPATH}:${THERM_DIR}/thermostat-agent-@project.version@.jar:${THERM_DIR}/thermostat-common-@project.version@.jar:${THERM_DIR}/thermostat-tools-@project.version@.jar"
+SERVICE_CLASSPATH="${TOOLS_JAR}:${SERVICE_CLASSPATH}:${THERM_DIR}/thermostat-agent-@project.version@.jar:${THERM_DIR}/thermostat-common-@project.version@.jar:${THERM_DIR}/thermostat-tools-@project.version@.jar"
 THERMOSTAT_MAIN="com.redhat.thermostat.launcher.Thermostat"
 
 ${JAVA} -cp ${SERVICE_CLASSPATH} ${THERMOSTAT_MAIN} "$@"