Class PythonAsyncProcessService

All Implemented Interfaces:
GenericMultiTypeService, ParameterConfigurerProvider, Service, ServiceBase
Direct Known Subclasses:
PythonWsProcessService

public class PythonAsyncProcessService extends AbstractPythonProcessService
Generic command-line-based Python integration for asynchronous processing of multiple data types. Conventions:
  • Python is determined by PythonUtils.getPythonExecutable(). The default is "ServiceEnvironment.py" which must run for this integration with "--mode console".
  • The Python program runs endless until stopped by this class.
  • An asynchronous Python program receives the data via command line input streams based on the input serializer and the symbolic type name.
Author:
Holger Eichelberger, SSE
  • Field Details

  • Constructor Details

    • PythonAsyncProcessService

      public PythonAsyncProcessService(String serviceId, InputStream ymlFile)
      Creates an instance from a service id and a YAML artifact.
      Parameters:
      serviceId - the service id
      ymlFile - the YML file containing the YAML artifact with the service descriptor
    • PythonAsyncProcessService

      public PythonAsyncProcessService(YamlService yaml)
      Creates an abstract service from YAML information.
      Parameters:
      yaml - the service information as read from YAML. By default, the Python executable is "ServiceEnvironment.py", which can be overridden by YamlProcess.getExecutable(). YamlProcess.getHomePath() is set to the home path where the executable was extracted to. Further, YamlProcess.getCmdArg() are taken over if given.
  • Method Details

    • start

      protected ServiceState start() throws ExecutionException
      Description copied from class: AbstractService
      Starts the service and the background process.
      Overrides:
      start in class AbstractPythonProcessService
      Returns:
      the state to transition to, may be null for none
      Throws:
      ExecutionException - if starting the process fails
    • createScanInputThread

      protected void createScanInputThread(Process proc)
      Creates the input scanning thread.
      Parameters:
      proc - the process instance to observe
    • stop

      protected ServiceState stop()
      Description copied from class: AbstractService
      Stops the service and the background process.
      Overrides:
      stop in class AbstractPythonProcessService
      Returns:
      the state to transition to, may be null for none
    • setState

      public void setState(ServiceState state) throws ExecutionException
      Description copied from class: AbstractService
      Changes the state. [R133c]
      Specified by:
      setState in interface ServiceBase
      Overrides:
      setState in class AbstractService
      Parameters:
      state - the new state
      Throws:
      ExecutionException - if changing the state fails for some reason
      See Also:
    • migrate

      public void migrate(String resourceId) throws ExecutionException
      Description copied from interface: Service
      Migrates a service. However, it may be required to migrate/move the containing artifact. [adaptation]
      Parameters:
      resourceId - the target resource id, e.g., a device
      Throws:
      ExecutionException - if migration fails
    • update

      public void update(URI location) throws ExecutionException
      Description copied from interface: Service
      Updates the service by the service in the given location. This operation is responsible for stopping the running service (if needed), replacing it, starting the new service.
      Parameters:
      location - the location of the new service, e.g., an URL
      Throws:
      ExecutionException - if the given service cannot be updated for some reason, e.g., because the replacement service is not an evolved version of the running service
    • switchTo

      public void switchTo(String targetId) throws ExecutionException
      Description copied from interface: Service
      Switches to an interface-compatible service. This method cares for stopping the old service, performing a handover if adequate, starting the target service. [adaptation]
      Parameters:
      targetId - the id of the target service
      Throws:
      ExecutionException - if switching the service cannot be performed for some reason
    • reconfigure

      public void reconfigure(Map<String,String> values) throws ExecutionException
      Description copied from interface: Service
      Reconfigures the service. [adaptation]
      Specified by:
      reconfigure in interface Service
      Overrides:
      reconfigure in class AbstractService
      Parameters:
      values - the (service-specific) name-value mapping that shall lead to a reconfiguration of the service; values come either as primitive values or as JSON structures complying with the parameter descriptor. The service is responsible for correct JSON de-serialization according to the respective descriptor.
      Throws:
      ExecutionException - if reconfiguration fails
    • activate

      public void activate() throws ExecutionException
      Description copied from interface: Service
      Activates the service. [adaptation]
      Specified by:
      activate in interface Service
      Overrides:
      activate in class AbstractService
      Throws:
      ExecutionException - in case that activating fails, e.g., because the service is already active
    • passivate

      public void passivate() throws ExecutionException
      Description copied from interface: Service
      Passivates the service. [adaptation]
      Specified by:
      passivate in interface Service
      Overrides:
      passivate in class AbstractService
      Throws:
      ExecutionException - in case that passivating fails, e.g., because the service is already passive
    • getLogger

      protected static de.iip_ecosphere.platform.support.logging.Logger getLogger()
      Returns the logger.
      Returns:
      the logger
    • sendToService

      protected void sendToService(String type, Object data) throws ExecutionException
      Sends text as input to the service process.
      Parameters:
      type - the type of the data to be sent
      data - the data to be sent
      Throws:
      ExecutionException - if sending fails for some reason
    • process

      public <I> void process(String inType, I data) throws ExecutionException
      Description copied from interface: GenericMultiTypeService
      Requests asynchronous processing a data item.
      Type Parameters:
      I - the input data type
      Parameters:
      inType - the name of inType in the configuration model
      data - the data item to be processed
      Throws:
      ExecutionException - if the execution fails for some reason, e.g., because type translators are not registered (@link #registerInputTypeTranslator(Class, Class, TypeTranslator, TypeTranslator)}
    • processSync

      public <I, O> O processSync(String inTypeName, I data, String outTypeName) throws ExecutionException
      Description copied from interface: GenericMultiTypeService
      Requests synchronous processing a data item.
      Type Parameters:
      I - the input data type
      O - the output data type
      Parameters:
      inTypeName - the name of inType in the configuration model
      data - the data item to be processed
      outTypeName - the name of outType in the configuration model
      Returns:
      the output, always null in case of asynchronous processing as the result is passed to a registered ingestor
      Throws:
      ExecutionException - if the execution fails for some reason, e.g., because type translators are not registered (@link #registerInputTypeTranslator(Class, Class, TypeTranslator, TypeTranslator)}