Class AgentInvoker

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class AgentInvoker
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Agent container which does not start a thread. It instead allows the duty cycle Agent.doWork() to be invoked directly. start() should be called to allow the Agent.onStart() to fire before any calls to invoke() of the agent duty cycle.

    Exceptions which occur during the Agent.doWork() invocation will be caught and passed to the provided ErrorHandler.

    Note: This class is not threadsafe.

    • Field Detail

      • isClosed

        private boolean isClosed
      • isStarted

        private boolean isStarted
      • isRunning

        private boolean isRunning
      • agent

        private final Agent agent
    • Constructor Detail

      • AgentInvoker

        public AgentInvoker​(ErrorHandler errorHandler,
                            AtomicCounter errorCounter,
                            Agent agent)
        Create an agent and initialise it.
        Parameters:
        errorHandler - to be called if an Throwable is encountered
        errorCounter - to be incremented each time an exception is encountered. This may be null.
        agent - to be run in this thread.
    • Method Detail

      • isStarted

        public boolean isStarted()
        Has the Agent been started?
        Returns:
        has the Agent been started?
      • isRunning

        public boolean isRunning()
        Has the Agent been running?
        Returns:
        has the Agent been started successfully and not closed?
      • isClosed

        public boolean isClosed()
        Has the Agent been closed?
        Returns:
        has the Agent been closed?
      • agent

        public Agent agent()
        The Agent which is contained.
        Returns:
        Agent being contained.
      • start

        public void start()
        Mark the invoker as started and call the Agent.onStart() method.

        Startup logic will only be performed once.

      • close

        public final void close()
        Mark the invoker as closed and call the Agent.onClose() logic for clean up.

        The cleanup logic will only be performed once.

        Specified by:
        close in interface java.lang.AutoCloseable
      • handleError

        private void handleError​(java.lang.Throwable throwable)