org.jppf.process
Class ProcessLauncher

java.lang.Object
  extended by org.jppf.utils.ThreadSynchronization
      extended by org.jppf.process.ProcessLauncher
All Implemented Interfaces:
Runnable, EventListener, IdleStateListener, ProcessWrapperEventListener

public class ProcessLauncher
extends ThreadSynchronization
implements Runnable, ProcessWrapperEventListener, IdleStateListener

This class is intended as a controller for a subprocess, to enable stopping and restarting it when requested.

It performs the following operations:

Author:
Laurent Cohen

Field Summary
 
Fields inherited from class org.jppf.utils.ThreadSynchronization
stopped
 
Constructor Summary
ProcessLauncher(String mainClass)
          Initialize this process launcher.
 
Method Summary
 Process buildProcess()
          Start the JPPF driver subprocess.
protected  void createShutdownHook()
          Create a shutdown hook that is run when this JVM terminates.
 void errorStreamAltered(ProcessWrapperEvent event)
          Notification that the process has written to its error stream.
 String getOutput(Process process, String streamType)
          Get the output of the driver process.
 void idleStateChanged(IdleStateEvent event)
          Called when the idle state of the system has changed.
 void outputStreamAltered(ProcessWrapperEvent event)
          Notification that the process has written to its output stream.
 void run()
          Start the socket listener and the subprocess.
protected  int startDriverSocket()
          Start a server socket that will accept one connection at a time with the JPPF driver, so the server can shtutdown properly, when this driver is killed, by a way other than the API (ie CTRL-C or killing the process through the OS shell).
 void startProcess()
          Start the sub-process.
 
Methods inherited from class org.jppf.utils.ThreadSynchronization
goToSleep, goToSleep, goToSleep, isStopped, setStopped, wakeUp
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProcessLauncher

public ProcessLauncher(String mainClass)
Initialize this process launcher.

Parameters:
mainClass - the fully qualified name of the main class of the sub process to launch.
Method Detail

run

public void run()
Start the socket listener and the subprocess.

Specified by:
run in interface Runnable

startProcess

public void startProcess()
                  throws Exception
Start the sub-process.

Throws:
Exception - if any error occurs.

buildProcess

public Process buildProcess()
                     throws Exception
Start the JPPF driver subprocess.

Returns:
A reference to the Process object representing the JPPF driver suprocess.
Throws:
Exception - if the process failed to start.

createShutdownHook

protected void createShutdownHook()
Create a shutdown hook that is run when this JVM terminates.
This is normally used to ensure the subprocess is terminated as well.


startDriverSocket

protected int startDriverSocket()
Start a server socket that will accept one connection at a time with the JPPF driver, so the server can shtutdown properly, when this driver is killed, by a way other than the API (ie CTRL-C or killing the process through the OS shell).
The port the server socket listens to is dynamically attributed, which is obtained by using the constructor new ServerSocket(0).
The driver will connect and listen to this port, and exit when the connection is broken.
The single connection at a time is obtained by doing the ServerSocket.accept() and the Socket.getInputStream().read() in the same thread.

Returns:
the port number on which the server socket is listening.

getOutput

public String getOutput(Process process,
                        String streamType)
Get the output of the driver process.

Parameters:
process - the process to get the standard or error output from.
streamType - detrmines whether to obtain the standard or error output.
Returns:
the output as a string.

errorStreamAltered

public void errorStreamAltered(ProcessWrapperEvent event)
Notification that the process has written to its error stream.

Specified by:
errorStreamAltered in interface ProcessWrapperEventListener
Parameters:
event - encapsulate the error stream's content.
See Also:
ProcessWrapperEventListener.errorStreamAltered(org.jppf.process.event.ProcessWrapperEvent)

outputStreamAltered

public void outputStreamAltered(ProcessWrapperEvent event)
Notification that the process has written to its output stream.

Specified by:
outputStreamAltered in interface ProcessWrapperEventListener
Parameters:
event - encapsulate the output stream's content.
See Also:
ProcessWrapperEventListener.outputStreamAltered(org.jppf.process.event.ProcessWrapperEvent)

idleStateChanged

public void idleStateChanged(IdleStateEvent event)
Called when the idle state of the system has changed.

Specified by:
idleStateChanged in interface IdleStateListener
Parameters:
event - the event encapsulating the state change.


Copyright © 2005-2010 JPPF Team.