Class AbstractProcessService<I,SI,SO,O>
- java.lang.Object
-
- de.iip_ecosphere.platform.services.environment.AbstractService
-
- de.iip_ecosphere.platform.services.environment.AbstractRunnablesService
-
- de.iip_ecosphere.platform.services.environment.AbstractProcessService<I,SI,SO,O>
-
- Type Parameters:
I- the input data typeSI- the service input data typeSO- the service output data typeO- the output data type
- All Implemented Interfaces:
MonitoringService,ParameterConfigurerProvider,Service,ServiceBase
- Direct Known Subclasses:
AbstractRestProcessService,AbstractStringProcessService
public abstract class AbstractProcessService<I,SI,SO,O> extends AbstractRunnablesService implements MonitoringService
Implements an abstract asynchronous process-based service for a single pair of input-output types. A created service/process is stopped on JVM shutdown.- Author:
- Holger Eichelberger, SSE
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceAbstractProcessService.RunnableWithStopA runnable that can be stopped.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Class<?>,java.util.List<de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?>>>callbacksprivate static booleaninheritIoprivate de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,java.lang.String>inTransprivate oshi.software.os.OSProcessosProcessprivate de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,O>outTransprivate java.lang.Processprocprivate java.io.PrintWriterserviceInprivate YamlServiceserviceSpec-
Fields inherited from interface de.iip_ecosphere.platform.services.environment.switching.ServiceBase
APPLICATION_SEPARATOR, DEFAULT_APPLICATION_INSTANCE_ID
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractProcessService(de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,java.lang.String> inTrans, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,O> outTrans, de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<O> callback, YamlService serviceSpec)Creates an instance of the service with the required type translators.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private voidaddCallback(de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?> callback)Adds a callback.protected voidaddProcessSpecCmdArg(java.util.List<java.lang.String> args)Adds the command line arguments from#getProcessSpec()toargs.<P> voidattachIngestor(java.lang.Class<P> outCls, DataIngestor<P> ingestor)Attaches an asynchronous result data ingestor as callback.voidattachMetricsProvider(MetricsProvider provider)Attaches the metrics provider.private voidattachProcessInformation()Attaches process information ifprocis available.protected voidconfigure(java.lang.ProcessBuilder builder)Allows to configure a process builder for this service.protected java.lang.ProcesscreateAndConfigureProcess(java.io.File exe, boolean byName, java.io.File dir, java.util.List<java.lang.String> args)Creates, configures and starts a command line process.static java.lang.ProcesscreateProcess(java.io.File exe, boolean byName, java.io.File dir, java.util.List<java.lang.String> args)Creates and starts a command line process.static java.lang.ProcesscreateProcess(java.io.File exe, boolean byName, java.io.File dir, java.util.List<java.lang.String> args, java.util.function.Consumer<java.lang.ProcessBuilder> customizer)Creates and starts a command line process.static java.lang.StringgetExecutableName(java.lang.String program, java.lang.String version)Constructs an executable name not naming 32 bit.static java.lang.StringgetExecutableName(java.lang.String program, java.lang.String version, boolean name32)Constructs an executable name.static java.lang.StringgetExecutablePrefix(java.lang.String program, java.lang.String version)Constructs an executable name without suffix, even on Windows, not naming 32 bit.static java.lang.StringgetExecutablePrefix(java.lang.String program, java.lang.String version, boolean name32)Constructs an executable name without suffix, even on Windows.static java.lang.StringgetExecutableSuffix()Returns the executable file suffix.protected de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,java.lang.String>getInputTranslator()Returns the input translator.static java.lang.StringgetOsArch(boolean name32)Returns the operating system and architecture in typical form, e.g, win32, win, win64 or linux32, linux, linux64.protected de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,O>getOutputTranslator()Returns the output translator.longgetPid()Returns the process id of the process implementing the service (if started).static longgetProcessId(java.lang.Process proc)Returns the process id of a process just started.protected YamlProcessgetProcessSpec()Returns the process specification withingetServiceSpec().protected java.io.PrintWritergetServiceIn()Returns the print writer wrapping the process service input stream created byhandleInputStream(InputStream).protected YamlServicegetServiceSpec()Returns the service specification.protected intgetWaitTimeBeforeDestroy()Returns an optional time to wait before destroying the process.protected voidhandleErrorStream(java.io.InputStream err)Handles the error stream upon process creation.protected abstract voidhandleInputStream(java.io.InputStream in)Handles the input stream upon process creation.protected voidhandleOutputStream(java.io.OutputStream out)Handles the output stream upon process creation.protected <P> voidnotifyCallbacks(P data)Returns the reception callbacks forout.abstract voidprocess(I data)Requests to process the given data item.voidprocessQuiet(I data)Requests asynchronous processing of a data item.static AbstractProcessService.RunnableWithStopredirectIO(java.io.InputStream in, java.io.PrintStream dest)Redirects an input stream to another stream (in parallel).protected static <O,T>
TselectNotNull(O object, java.util.function.Function<O,T> valueFunc, T dflt)Selects between a value ofobjectdetermined byvalueFuncanddflt, ifobjectis null or the result ofvalueFuncis null usedflt, else the value ofvalueFunc.protected static <T> TselectNotNull(T value, T dflt)Selects betweenvalueanddflt, ifvalueis null usedflt, else the value ofvalue.static booleansetInheritIo(boolean inherit)Sets the default value allow or preveting the inheritance of the IO setup for a process being created increateProcess(File, boolean, File, List, Consumer).protected ServiceStatestop()Preliminary: Stops the service and the background process.static voidwaitAndDestroy(java.lang.Process proc)Waits and destroys a process with a default sleep time of 200 ms.static voidwaitAndDestroy(java.lang.Process proc, int sleepTime)Waits and destroys a process.-
Methods inherited from class de.iip_ecosphere.platform.services.environment.AbstractRunnablesService
register
-
Methods inherited from class de.iip_ecosphere.platform.services.environment.AbstractService
activate, addConfigurer, addConfigurer, addConfigurer, createInstance, createInstance, createInstance, getDescription, getId, getKind, getName, getNetMgtKeyAddress, getResourceAsStream, getState, getVersion, initializeFrom, isDeployable, isTopLevel, notifyReconfigured, passivate, reconf, reconfigure, reconfigure, rollbackReconfigurationOnFailure, setLibJars, setState, start
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface de.iip_ecosphere.platform.services.environment.Service
activate, getDescription, getKind, getName, getNetMgtKeyAddress, getParameterConfigurer, getParameterNames, getVersion, isDeployable, isTopLevel, migrate, passivate, reconfigure, switchTo, update
-
Methods inherited from interface de.iip_ecosphere.platform.services.environment.switching.ServiceBase
getId, getState, setState
-
-
-
-
Field Detail
-
inheritIo
private static boolean inheritIo
-
inTrans
private de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,java.lang.String> inTrans
-
outTrans
private de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,O> outTrans
-
callbacks
private java.util.Map<java.lang.Class<?>,java.util.List<de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?>>> callbacks
-
serviceSpec
private YamlService serviceSpec
-
serviceIn
private java.io.PrintWriter serviceIn
-
proc
private java.lang.Process proc
-
osProcess
private oshi.software.os.OSProcess osProcess
-
-
Constructor Detail
-
AbstractProcessService
protected AbstractProcessService(de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,java.lang.String> inTrans, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,O> outTrans, de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<O> callback, YamlService serviceSpec)
Creates an instance of the service with the required type translators.- Parameters:
inTrans- the input translatoroutTrans- the output translatorcallback- called when data from the service is available (may be null ifattachIngestor(Class, DataIngestor)is called before first data processing).serviceSpec- the service description
-
-
Method Detail
-
addCallback
private void addCallback(de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?> callback)
Adds a callback.- Parameters:
callback- the callback, ignored if null
-
processQuiet
public void processQuiet(I data)
Requests asynchronous processing of a data item. Callsprocess(Object)and handles potential exceptions.- Parameters:
data- the data item to be processed
-
attachIngestor
public <P> void attachIngestor(java.lang.Class<P> outCls, DataIngestor<P> ingestor)Attaches an asynchronous result data ingestor as callback.- Type Parameters:
P- the output type- Parameters:
outCls- the class representing the typeingestor- the ingestor instance
-
process
public abstract void process(I data) throws java.io.IOException
Requests to process the given data item.- Parameters:
data- the data to process- Throws:
java.io.IOException- if processing/transferring to the service fails
-
getServiceSpec
protected YamlService getServiceSpec()
Returns the service specification.- Returns:
- the service specification
-
getProcessSpec
protected YamlProcess getProcessSpec()
Returns the process specification withingetServiceSpec().- Returns:
- the process specification, may be null
-
addProcessSpecCmdArg
protected void addProcessSpecCmdArg(java.util.List<java.lang.String> args)
Adds the command line arguments from#getProcessSpec()toargs.- Parameters:
args- the arguments to be modified as a side effect
-
notifyCallbacks
protected <P> void notifyCallbacks(P data)
Returns the reception callbacks forout.- Type Parameters:
P- the output type- Parameters:
data- the output data, may be null
-
getInputTranslator
protected de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,java.lang.String> getInputTranslator()
Returns the input translator.- Returns:
- the translator
-
getOutputTranslator
protected de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,O> getOutputTranslator()
Returns the output translator.- Returns:
- the translatir
-
getOsArch
public static java.lang.String getOsArch(boolean name32)
Returns the operating system and architecture in typical form, e.g, win32, win, win64 or linux32, linux, linux64.- Parameters:
name32- shall the method add 32 in case of a 32 bit operating system (explicit) or be quite (implicit) and add only 64 in case of 64 bit systems- Returns:
- the operating system and architecture name
-
getExecutableSuffix
public static java.lang.String getExecutableSuffix()
Returns the executable file suffix.- Returns:
- the suffix, may be empty
-
getExecutablePrefix
public static java.lang.String getExecutablePrefix(java.lang.String program, java.lang.String version)Constructs an executable name without suffix, even on Windows, not naming 32 bit.- Parameters:
program- the program nameversion- the version of the program- Returns:
- the executable prefix
- See Also:
getExecutablePrefix(String, String, boolean)
-
getExecutablePrefix
public static java.lang.String getExecutablePrefix(java.lang.String program, java.lang.String version, boolean name32)Constructs an executable name without suffix, even on Windows.- Parameters:
program- the program nameversion- the version of the programname32- shall the method add 32 in case of a 32 bit operating system (explicit) or be quite (implicit) and add only 64 in case of 64 bit systems- Returns:
- the executable prefix
- See Also:
getOsArch(boolean)
-
getExecutableName
public static java.lang.String getExecutableName(java.lang.String program, java.lang.String version, boolean name32)Constructs an executable name.- Parameters:
program- the program nameversion- the version of the programname32- shall the method add 32 in case of a 32 bit operating system (explicit) or be quite (implicit) and add only 64 in case of 64 bit systems- Returns:
- the executable name
- See Also:
getExecutablePrefix(String, String, boolean),getExecutableSuffix()
-
getExecutableName
public static java.lang.String getExecutableName(java.lang.String program, java.lang.String version)Constructs an executable name not naming 32 bit.- Parameters:
program- the program nameversion- the version of the program- Returns:
- the executable name
- See Also:
getExecutableName(String, String, boolean)
-
createProcess
public static java.lang.Process createProcess(java.io.File exe, boolean byName, java.io.File dir, java.util.List<java.lang.String> args) throws java.io.IOExceptionCreates and starts a command line process.- Parameters:
exe- the executable to runbyName- add theexeby name or (iffalse) by absolute namedir- the home dir where to execute the process withinargs- the process arguments (may be null for none)- Returns:
- the created process instance
- Throws:
java.io.IOException- if process creation fails- See Also:
createProcess(File, boolean, File, List, Consumer)
-
createProcess
public static java.lang.Process createProcess(java.io.File exe, boolean byName, java.io.File dir, java.util.List<java.lang.String> args, java.util.function.Consumer<java.lang.ProcessBuilder> customizer) throws java.io.IOExceptionCreates and starts a command line process.- Parameters:
exe- the executable to runbyName- add theexeby name or (iffalse) by absolute namedir- the home dir where to execute the process withinargs- the process arguments (may be null for none)customizer- allows for further customization of the process builder created in this method, may be null for none- Returns:
- the created process instance
- Throws:
java.io.IOException- if process creation fails- See Also:
setInheritIo(boolean)
-
setInheritIo
public static boolean setInheritIo(boolean inherit)
Sets the default value allow or preveting the inheritance of the IO setup for a process being created increateProcess(File, boolean, File, List, Consumer). Typically switched off for conflicts with CI/surefire.- Parameters:
inherit- allow/disable inheriting process IO setup- Returns:
- the value of the flag before the call
-
redirectIO
public static AbstractProcessService.RunnableWithStop redirectIO(java.io.InputStream in, java.io.PrintStream dest)
Redirects an input stream to another stream (in parallel).- Parameters:
in- the input stream of the spawned process (e.g., input/error)dest- the destination stream within this class- Returns:
- the runnable performing the redirection
-
waitAndDestroy
public static void waitAndDestroy(java.lang.Process proc)
Waits and destroys a process with a default sleep time of 200 ms.- Parameters:
proc- the process to be destroyed, may be null but nothing happens then- See Also:
waitAndDestroy(Process, int)
-
waitAndDestroy
public static void waitAndDestroy(java.lang.Process proc, int sleepTime)Waits and destroys a process.- Parameters:
proc- the process to be destroyed, may be null but nothing happens thensleepTime- the waiting portion untilProcess.isAlive()is queried again
-
stop
protected ServiceState stop()
Preliminary: Stops the service and the background process.- Overrides:
stopin classAbstractRunnablesService- Returns:
- the state to transition to, may be null for none
-
getWaitTimeBeforeDestroy
protected int getWaitTimeBeforeDestroy()
Returns an optional time to wait before destroying the process.- Returns:
- the wait time in ms, may be 0 for none, default 300 ms
-
handleOutputStream
protected void handleOutputStream(java.io.OutputStream out)
Handles the output stream upon process creation.- Parameters:
out- the process output stream
-
handleInputStream
protected abstract void handleInputStream(java.io.InputStream in)
Handles the input stream upon process creation.- Parameters:
in- the process input stream
-
handleErrorStream
protected void handleErrorStream(java.io.InputStream err)
Handles the error stream upon process creation.- Parameters:
err- the process error stream
-
createAndConfigureProcess
protected java.lang.Process createAndConfigureProcess(java.io.File exe, boolean byName, java.io.File dir, java.util.List<java.lang.String> args) throws java.util.concurrent.ExecutionExceptionCreates, configures and starts a command line process.- Parameters:
exe- the executable to runbyName- add theexeby name or (iffalse) by absolute namedir- the home dir where to execute the process withinargs- the process arguments (may be null for none)- Returns:
- the created process instance
- Throws:
java.util.concurrent.ExecutionException- if process creation fails- See Also:
handleInputStream(InputStream),handleErrorStream(InputStream),handleOutputStream(OutputStream)
-
attachProcessInformation
private void attachProcessInformation()
Attaches process information ifprocis available. Does not attach process information twice.
-
getPid
public long getPid()
Returns the process id of the process implementing the service (if started).- Returns:
- the id, may be negative if there is no process or the id cannot be obtained
-
getProcessId
public static long getProcessId(java.lang.Process proc)
Returns the process id of a process just started.- Parameters:
proc- the process- Returns:
- the id, may be negative if
procis null or the id cannot be obtained
-
configure
protected void configure(java.lang.ProcessBuilder builder)
Allows to configure a process builder for this service. Called fromcreateAndConfigureProcess(File, boolean, File, List)- Parameters:
builder- the process builder to configure
-
getServiceIn
protected java.io.PrintWriter getServiceIn()
Returns the print writer wrapping the process service input stream created byhandleInputStream(InputStream).- Returns:
- the stream
-
attachMetricsProvider
public void attachMetricsProvider(MetricsProvider provider)
Attaches the metrics provider.- Specified by:
attachMetricsProviderin interfaceMonitoringService- Parameters:
provider- the metrics provider instance
-
selectNotNull
protected static <T> T selectNotNull(T value, T dflt)Selects betweenvalueanddflt, ifvalueis null usedflt, else the value ofvalue.- Type Parameters:
T- the value type- Parameters:
value- the primary valuedflt- the default value to use ifvalueis null- Returns:
- either
valueordflt
-
selectNotNull
protected static <O,T> T selectNotNull(O object, java.util.function.Function<O,T> valueFunc, T dflt)Selects between a value ofobjectdetermined byvalueFuncanddflt, ifobjectis null or the result ofvalueFuncis null usedflt, else the value ofvalueFunc.- Type Parameters:
O- the object typeT- the value type- Parameters:
object- the object to take the value fromvalueFunc- the function to apply onobjectdflt- the default value to use ifobjector the result ofvalueFuncis null- Returns:
- either the result of
valueFuncordflt
-
-