Author: chatellier Date: 2008-10-31 12:13:47 +0000 (Fri, 31 Oct 2008) New Revision: 1582 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SubProcessSimulationLauncher.java Log: Make subprocess to correctly end Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SubProcessSimulationLauncher.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SubProcessSimulationLauncher.java 2008-10-31 12:13:15 UTC (rev 1581) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SubProcessSimulationLauncher.java 2008-10-31 12:13:47 UTC (rev 1582) @@ -21,16 +21,20 @@ import static org.codelutin.i18n.I18nf._; -import fr.ifremer.isisfish.IsisFish; -import fr.ifremer.isisfish.datastore.SimulationStorage; -import fr.ifremer.isisfish.simulator.SimulationControl; -import java.io.BufferedInputStream; +import java.io.BufferedReader; import java.io.File; +import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.rmi.RemoteException; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import fr.ifremer.isisfish.IsisFish; +import fr.ifremer.isisfish.datastore.SimulationStorage; +import fr.ifremer.isisfish.simulator.SimulationControl; + /** * * @author poussin @@ -41,10 +45,10 @@ */ public class SubProcessSimulationLauncher implements SimulatorLauncher { - /** to use log facility, just put in your code: log.info(\"...\"); */ - static private Log log = LogFactory.getLog(SubProcessSimulationLauncher.class); + /** Class logger (protected for inner classes) */ + protected static Log log = LogFactory.getLog(SubProcessSimulationLauncher.class); - protected boolean finished = false; + //protected boolean finished = false; @Override public String toString() { @@ -57,14 +61,20 @@ String simulationId = control.getId(); SimulationStorage simulation = null; try { -// simulation = SimulationStorage.importAndRenameZip( -// simulationZip, simulationId); + // simulation = SimulationStorage.importAndRenameZip( + // simulationZip, simulationId); simulation = subProcessSimulate(control, simulationZip); } catch (Exception eee) { - log.error(_("Can't do simulation %s", simulationId), eee); - simulation.getInformation().setException(eee); + if(log.isErrorEnabled()) { + log.error(_("Can't do simulation %s", simulationId), eee); + } + // FIXME simulation is always null if exception + if(simulation != null) { + simulation.getInformation().setException(eee); + } } + // FIXME comment not valid // on retourne directement le simulation storage passe en argument // car la simulation a ete faite avec return simulation; @@ -83,7 +93,7 @@ String simulationId = control.getId(); // on ferme le SimulationStorage pour ne pas interferer avec le process -// simulation.closeStorage(); + //simulation.closeStorage(); String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; @@ -98,8 +108,11 @@ // demarrage du process Process process = processBuilder.start(); - log.info(_("SubProcess start: %s %s", process, processBuilder.command())); + if(log.isInfoEnabled()) { + log.info(_("SubProcess start: %s %s", process, processBuilder.command())); + } + // FIXME control can't be null here if (control != null) { // prepare de thread de surveillance du process si control n'est pas null Thread monitor = new SimulationCheckpointExternalProcessThread( @@ -109,13 +122,18 @@ // on attend que la simulation soit fini process.waitFor(); - finished = true; + + if(log.isInfoEnabled()) { + log.info("SubProcess finished"); + } + + //finished = true; SimulationStorage result = SimulationStorage.getSimulation(simulationId); return result; } /** - * This thread is responsable to synchronized SimulationControl used localy with + * This thread is responsible to synchronized SimulationControl used locally with * remote simulation control for remote simulation. * * This thread dead when {@link SimulationControl#isRunning()} is false @@ -123,10 +141,7 @@ * @author poussin */ protected class SimulationCheckpointExternalProcessThread extends Thread { // SimulationCheckpointThread - - /** - * Logger for this class - */ + protected SimulationControl control = null; protected String simulationId = null; protected Process process = null; @@ -135,22 +150,36 @@ public SimulationCheckpointExternalProcessThread(SimulationControl control, String simulationId, Process process) { - log.info("Lancement du thread de surveillance des simulations externes"); + if(log.isInfoEnabled()) { + log.info("Lancement du thread de surveillance des simulations externes"); + } this.control = control; this.simulationId = simulationId; this.process = process; - out = process.getInputStream(); + this.out = process.getInputStream(); } @Override public void run() { - int sleepTime = 2000; - int error = 0; - while (!finished) { - try { - out.skip(out.available()); - Thread.sleep(sleepTime); + InputStreamReader osr = new InputStreamReader(out); + BufferedReader br = new BufferedReader(osr); + + //int sleepTime = 2000; + + // use tip available at + // http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4 + // for reading subprocess output + String line=null; + try { + while ((line = br.readLine()) != null) { + // add reading line to logging output + if(log.isInfoEnabled()) { + log.info(SubProcessSimulationLauncher.this.toString() + ">" + line); + } + + //Thread.sleep(sleepTime); + // on ne lit pas le stop, car le stop ne peut-etre appeler // que par l'utilisateur qui est de ce cote de la machine SimulationStorage.readControl(simulationId, control, "stop"); @@ -164,11 +193,16 @@ if (!control.isRunning()) { return; } - } catch (Exception eee) { - log.debug("Can't update control for " + control.getId(), eee); } + //} catch (InterruptedException eee) { + // if(log.isDebugEnabled()) { + // log.debug("Can't update control for " + control.getId(), eee); + // } + } catch (IOException e) { + if(log.isErrorEnabled()) { + log.error(_("isisfish.simulator.subprocess.readoutput.error"), e); + } } - } } }
participants (1)
-
chatellierï¼ users.labs.libre-entreprise.org