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 ClassesModifier and TypeClassDescriptionstatic interfaceA runnable that can be stopped. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static booleanprivate de.iip_ecosphere.platform.support.processInfo.ProcessInfoFactory.ProcessInfoprivate Processprivate PrintWriterprivate YamlServiceFields inherited from interface de.iip_ecosphere.platform.services.environment.switching.ServiceBase
APPLICATION_SEPARATOR, DEFAULT_APPLICATION_INSTANCE_ID -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractProcessService(de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I, String> inTrans, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<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
Modifier and TypeMethodDescriptionprivate voidaddCallback(de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?> callback) Adds a callback.protected voidaddProcessSpecCmdArg(List<String> args) Adds the command line arguments from#getProcessSpec()toargs.<P> voidattachIngestor(Class<P> outCls, DataIngestor<P> ingestor) Attaches an asynchronous result data ingestor as callback.voidattachMetricsProvider(MetricsProvider provider) Attaches the metrics provider.private voidAttaches process information ifprocis available.protected voidconfigure(ProcessBuilder builder) Allows to configure a process builder for this service.protected ProcesscreateAndConfigureProcess(File exe, boolean byName, File dir, List<String> args) Creates, configures and starts a command line process.static ProcesscreateProcess(File exe, boolean byName, File dir, List<String> args) Creates and starts a command line process.static ProcesscreateProcess(File exe, boolean byName, File dir, List<String> args, Consumer<ProcessBuilder> customizer) Creates and starts a command line process.protected Iterable<de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?>> getCallbacks(Class<?> cls) Returns the callbacks for a given type.static StringgetExecutableName(String program, String version) Constructs an executable name not naming 32 bit.static StringgetExecutableName(String program, String version, boolean name32) Constructs an executable name.static StringgetExecutablePrefix(String program, String version) Constructs an executable name without suffix, even on Windows, not naming 32 bit.static StringgetExecutablePrefix(String program, String version, boolean name32) Constructs an executable name without suffix, even on Windows.static StringReturns the executable file suffix.Returns the input translator.static StringgetOsArch(boolean name32) Returns the operating system and architecture in typical form, e.g, win32, win, win64 or linux32, linux, linux64.Returns the output translator.longgetPid()Returns the process id of the process implementing the service (if started).static longgetProcessId(Process proc) Returns the process id of a process just started.protected YamlProcessReturns the process specification withingetServiceSpec().protected PrintWriterReturns the print writer wrapping the process service input stream created byhandleInputStream(InputStream).protected YamlServiceReturns the service specification.protected intReturns an optional time to wait before destroying the process.protected voidHandles the error stream upon process creation.protected abstract voidHandles the input stream upon process creation.protected voidHandles the output stream upon process creation.protected <P> voidnotifyCallbacks(P data) Returns the reception callbacks forout.abstract voidRequests to process the given data item.voidprocessQuiet(I data) Requests asynchronous processing of a data item.redirectIO(InputStream in, PrintStream dest) Redirects an input stream to another stream (in parallel).protected static <O,T> T selectNotNull(O object, 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(Process proc) Waits and destroys a process with a default sleep time of 200 ms.static voidwaitAndDestroy(Process proc, int sleepTime) Waits and destroys a process.Methods inherited from class de.iip_ecosphere.platform.services.environment.AbstractRunnablesService
registerMethods inherited from class de.iip_ecosphere.platform.services.environment.AbstractService
activate, addConfigurer, addConfigurer, addConfigurer, addTypeSubstitution, createGenericInstance, createGenericInstance, createGenericInstanceByPlugin, createGenericMultiInstance, createGenericMultiInstance, createGenericMultiInstanceByPlugin, createInstance, createInstance, createInstance, createInstanceByPlugin, getArg, getDescription, getId, getKind, getName, getNetMgtKeyAddress, getResourceAsStream, getState, getStringArg, getSubstitutedType, getTypeCreator, getTypeSubstitutions, getVersion, initializeFrom, isDeployable, isTopLevel, notifyReconfigured, passivate, reconf, reconfigure, reconfigure, rollbackReconfigurationOnFailure, setLibJars, setState, start, transferDataMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface de.iip_ecosphere.platform.services.environment.Service
activate, addTypeSubstitution, createType, createType, getDescription, getKind, getName, getNetMgtKeyAddress, getParameterConfigurer, getParameterNames, getSubstitutedType, getTypeCreator, getTypeSubstitutions, getVersion, isDeployable, isTopLevel, migrate, passivate, reconfigure, switchTo, updateMethods inherited from interface de.iip_ecosphere.platform.services.environment.switching.ServiceBase
getId, getState, setState
-
Field Details
-
inheritIo
private static boolean inheritIo -
inTrans
-
outTrans
-
callbacks
-
serviceSpec
-
serviceIn
-
proc
-
osProcess
private de.iip_ecosphere.platform.support.processInfo.ProcessInfoFactory.ProcessInfo osProcess
-
-
Constructor Details
-
AbstractProcessService
protected AbstractProcessService(de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I, String> inTrans, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<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 Details
-
addCallback
private void addCallback(de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?> callback) Adds a callback.- Parameters:
callback- the callback, ignored if null
-
processQuiet
Requests asynchronous processing of a data item. Callsprocess(Object)and handles potential exceptions.- Parameters:
data- the data item to be processed
-
attachIngestor
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
Requests to process the given data item.- Parameters:
data- the data to process- Throws:
IOException- if processing/transferring to the service fails
-
getServiceSpec
Returns the service specification.- Returns:
- the service specification
-
getProcessSpec
Returns the process specification withingetServiceSpec().- Returns:
- the process specification, may be null
-
addProcessSpecCmdArg
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
-
getCallbacks
protected Iterable<de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?>> getCallbacks(Class<?> cls) Returns the callbacks for a given type.- Parameters:
cls- the type- Returns:
- the callbacks, may be null if there are none
-
getInputTranslator
protected de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,String> getInputTranslator()Returns the input translator.- Returns:
- the translator
-
getOutputTranslator
protected de.iip_ecosphere.platform.transport.serialization.TypeTranslator<String,O> getOutputTranslator()Returns the output translator.- Returns:
- the translator
-
getOsArch
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
Returns the executable file suffix.- Returns:
- the suffix, may be empty
-
getExecutablePrefix
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
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:
-
getExecutableName
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:
-
getExecutableName
Constructs an executable name not naming 32 bit.- Parameters:
program- the program nameversion- the version of the program- Returns:
- the executable name
- See Also:
-
createProcess
public static Process createProcess(File exe, boolean byName, File dir, List<String> args) throws IOException Creates 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:
IOException- if process creation fails- See Also:
-
createProcess
public static Process createProcess(File exe, boolean byName, File dir, List<String> args, Consumer<ProcessBuilder> customizer) throws IOException Creates 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:
IOException- if process creation fails- See Also:
-
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
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
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
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
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
Handles the output stream upon process creation.- Parameters:
out- the process output stream
-
handleInputStream
Handles the input stream upon process creation.- Parameters:
in- the process input stream
-
handleErrorStream
Handles the error stream upon process creation.- Parameters:
err- the process error stream
-
createAndConfigureProcess
protected Process createAndConfigureProcess(File exe, boolean byName, File dir, List<String> args) throws ExecutionException Creates, 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:
ExecutionException- if process creation fails- See Also:
-
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
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
Allows to configure a process builder for this service. Called fromcreateAndConfigureProcess(File, boolean, File, List)- Parameters:
builder- the process builder to configure
-
getServiceIn
Returns the print writer wrapping the process service input stream created byhandleInputStream(InputStream).- Returns:
- the stream
-
attachMetricsProvider
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
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
-