Class AbstractCommandReceivingComponent

java.lang.Object
org.hobbit.core.components.AbstractComponent
org.hobbit.core.components.AbstractCommandReceivingComponent
All Implemented Interfaces:
Closeable, AutoCloseable, CommandReceivingComponent, Component
Direct Known Subclasses:
AbstractPlatformConnectorComponent

public abstract class AbstractCommandReceivingComponent extends AbstractComponent implements CommandReceivingComponent
  • Field Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • DEFAULT_CMD_RESPONSE_TIMEOUT

      public static final long DEFAULT_CMD_RESPONSE_TIMEOUT
      See Also:
    • containerName

      private String containerName
      Name of this Docker container.
    • responseQueueName

      private String responseQueueName
      Name of the queue that is used to receive responses for messages that are sent via the command queue and for which an answer is expected.
    • responseFutures

      private Map<String,com.google.common.util.concurrent.SettableFuture<String>> responseFutures
      Mapping of RabbitMQ's correlationIDs to Future objects corresponding to that RPC call.
    • responseConsumer

      private com.rabbitmq.client.Consumer responseConsumer
      Consumer of the queue that is used to receive responses for messages that are sent via the command queue and for which an answer is expected.
    • cmdQueueFactory

      protected RabbitQueueFactory cmdQueueFactory
      Factory for generating queues with which the commands are sent and received. It is separated from the data connections since otherwise the component can get stuck waiting for a command while the connection is busy handling incoming or outgoing data.
    • cmdChannel

      protected com.rabbitmq.client.Channel cmdChannel
      Channel that is used for the command queue.
    • defaultContainerType

      protected String defaultContainerType
      Default type of containers created by this container
    • acceptedCmdHeaderIds

      private Set<String> acceptedCmdHeaderIds
      Set of command headers that are expected by this component.
    • gson

      protected com.google.gson.Gson gson
      Threadsafe JSON parser.
    • cmdResponseTimeout

      protected long cmdResponseTimeout
      Time the component waits for a response of the platform controller.
    • cmdThreadPool

      private ExecutorService cmdThreadPool
    • errorLogged

      private boolean errorLogged
    • reportUnhandledExceptions

      protected boolean reportUnhandledExceptions
      Flag that is used to control whether the reportUnhandledExceptionSavely(Exception) method should report exceptions or not. It can be set to false to provide better, more detailed reports while skipping the general reporting of unhandled exceptions.
  • Constructor Details

    • AbstractCommandReceivingComponent

      public AbstractCommandReceivingComponent()
      Constructor.
    • AbstractCommandReceivingComponent

      public AbstractCommandReceivingComponent(boolean execCommandsInParallel)
      Constructor.
      Parameters:
      execCommandsInParallel - flag allowing the processing of commands in parallel
  • Method Details

    • init

      public void init() throws Exception
      Description copied from interface: Component
      This method initializes the component.
      Specified by:
      init in interface Component
      Overrides:
      init in class AbstractComponent
      Throws:
      Exception - if an error occurs during the initialization
    • sendToCmdQueue

      protected void sendToCmdQueue(byte command) throws IOException
      Sends the given command to the command queue.
      Parameters:
      command - the command that should be sent
      Throws:
      IOException - if a communication problem occurs
    • sendToCmdQueue

      protected void sendToCmdQueue(byte command, byte[] data) throws IOException
      Sends the given command to the command queue with the given data appended.
      Parameters:
      command - the command that should be sent
      data - data that should be appended to the command
      Throws:
      IOException - if a communication problem occurs
    • sendToCmdQueue

      protected void sendToCmdQueue(byte command, byte[] data, com.rabbitmq.client.AMQP.BasicProperties props) throws IOException
      Sends the given command to the command queue with the given data appended and using the given properties.
      Parameters:
      command - the command that should be sent
      data - data that should be appended to the command
      props - properties that should be used for the message
      Throws:
      IOException - if a communication problem occurs
    • addCommandHeaderId

      protected void addCommandHeaderId(String sessionId)
      Adds the given session id to the set of ids this component is reacting to.
      Parameters:
      sessionId - session id that should be added to the set of accepted ids.
    • handleCmd

      protected void handleCmd(byte[] bytes, com.rabbitmq.client.AMQP.BasicProperties props)
      This method is called if a message is received from the command queue.
      Parameters:
      bytes - data from the RabbitMQ message
      props - properties of the RabbitMQ message
    • handleCmd

      protected void handleCmd(byte[] bytes, String replyTo)
      This method is called if a message is received from the command queue.
      Parameters:
      bytes - data from the RabbitMQ message
      replyTo - name of the queue in which response is expected
    • createContainer

      protected String createContainer(String imageName, String[] envVariables)
      This method sends a Commands.DOCKER_CONTAINER_START command to create and start an instance of the given image using the given environment variables.
      Parameters:
      imageName - the name of the image of the docker container
      envVariables - environment variables that should be added to the created container
      Returns:
      the name of the container instance or null if an error occurred
    • extendContainerEnvVariables

      protected String[] extendContainerEnvVariables(String[] envVariables)
      This method extends (if needed) the array of environment variables for the container with HOBBIT specific variables.
      Parameters:
      envVariables - user-provided array of environment variables
      Returns:
      the extended array of environment variables
    • createContainer

      protected String createContainer(String imageName, String containerType, String[] envVariables)
      This method sends a Commands.DOCKER_CONTAINER_START command to create and start an instance of the given image using the given environment variables.

      Note that the containerType parameter should have one of the following values.

      Parameters:
      imageName - the name of the image of the docker container
      containerType - the type of the container
      envVariables - environment variables that should be added to the created container
      Returns:
      the name of the container instance or null if an error occurred
    • createContainer

      protected String createContainer(String imageName, String containerType, String[] envVariables, String[] netAliases)
      This method sends a Commands.DOCKER_CONTAINER_START command to create and start an instance of the given image using the given environment variables.

      Note that the containerType parameter should have one of the following values.

      Parameters:
      imageName - the name of the image of the docker container
      containerType - the type of the container
      envVariables - environment variables that should be added to the created container
      netAliases - network aliases that should be added to the created container
      Returns:
      the name of the container instance or null if an error occurred
    • createContainerAsync

      protected Future<String> createContainerAsync(String imageName, String containerType, String[] envVariables)
      This method sends a Commands.DOCKER_CONTAINER_START command to create and start an instance of the given image using the given environment variables.

      Note that the containerType parameter should have one of the following values.

      Parameters:
      imageName - the name of the image of the docker container
      containerType - the type of the container
      envVariables - environment variables that should be added to the created container
      Returns:
      the Future object with the name of the container instance or null if an error occurred
    • createContainerAsync

      protected Future<String> createContainerAsync(String imageName, String containerType, String[] envVariables, String[] netAliases)
      This method sends a Commands.DOCKER_CONTAINER_START command to create and start an instance of the given image using the given environment variables.

      Note that the containerType parameter should have one of the following values.

      Parameters:
      imageName - the name of the image of the docker container
      containerType - the type of the container
      envVariables - environment variables that should be added to the created container
      netAliases - network aliases that should be added to the created container
      Returns:
      the Future object with the name of the container instance or null if an error occurred
    • stopContainer

      protected void stopContainer(String containerName)
      This method sends a Commands.DOCKER_CONTAINER_STOP command to stop the container with the given id.
      Parameters:
      containerName - the name of the container instance that should be stopped
    • initResponseQueue

      private void initResponseQueue() throws IOException
      Internal method for initializing the responseQueueName and the responseConsumer if they haven't been initialized before.
      Throws:
      IOException - if a communication problem occurs
    • getCmdResponseTimeout

      public long getCmdResponseTimeout()
      Returns:
      the cmdResponseTimeout
    • setCmdResponseTimeout

      public void setCmdResponseTimeout(long cmdResponseTimeout)
      Parameters:
      cmdResponseTimeout - the cmdResponseTimeout to set
    • reportError

      public void reportError(ErrorData error) throws IOException
      This method sends the given error report on the command queue.
      Parameters:
      error - the data of the error that should be reported
      Throws:
      IOException - if sending the report fails
    • reportErrorSavely

      public void reportErrorSavely(ErrorData error)
      This method sends the given error report on the command queue without throwing an exception. This can be helpful in situations, in which the reported error may lead to a crash of the system and the reporting mechanism itself may not work anymore.
      Parameters:
      error - the data of the error that should be reported
    • reportExceptionSavely

      public void reportExceptionSavely(Exception e)
      This method sends a report about the given exception. The container name is derived from the configuration and the error report is generated based on the data provided by the exception. Note that instances of ReportedException are not reported.
      Parameters:
      e - the severe exception that should be reported as error
    • reportUnhandledExceptionSavely

      public void reportUnhandledExceptionSavely(Exception e)
      This method sends a report about the given unhandled exception using the reportExceptionSavely(Exception) method unless the flag reportUnhandledExceptions is set to false.
      Parameters:
      e - the severe exception that should be reported as error
    • reportAndWrap

      public Exception reportAndWrap(Exception e)
      Report the given unhandled exception using reportUnhandledExceptionSavely(Exception) and return a new instance of ReportedException that can be thrown.
      Parameters:
      e - the severe exception that should be reported as error
      Returns:
      the new exception instance that can be thrown
    • isReportUnhandledExceptions

      public boolean isReportUnhandledExceptions()
      Returns:
      the reportUnhandledExceptions
    • setReportUnhandledExceptions

      public void setReportUnhandledExceptions(boolean reportUnhandledExceptions)
      Parameters:
      reportUnhandledExceptions - the reportUnhandledExceptions to set
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class AbstractComponent
      Throws:
      IOException