Package org.agrona.concurrent
Class AgentInvoker
java.lang.Object
org.agrona.concurrent.AgentInvoker
- All Implemented Interfaces:
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.
-
Constructor Summary
ConstructorsConstructorDescriptionAgentInvoker(ErrorHandler errorHandler, AtomicCounter errorCounter, Agent agent) Create an agent and initialise it. -
Method Summary
Modifier and TypeMethodDescriptionagent()TheAgentwhich is contained.final voidclose()Mark the invoker as closed and call theAgent.onClose()logic for clean up.intinvoke()Invoke theAgent.doWork()method and return the work count.booleanisClosed()Has theAgentbeen closed?booleanHas theAgentbeen running?booleanHas theAgentbeen started?voidstart()Mark the invoker as started and call theAgent.onStart()method.
-
Constructor Details
-
AgentInvoker
Create an agent and initialise it.- Parameters:
errorHandler- to be called if anThrowableis encounterederrorCounter- to be incremented each time an exception is encountered. This may be null.agent- to be run in this thread.
-
-
Method Details
-
isStarted
public boolean isStarted()Has theAgentbeen started?- Returns:
- has the
Agentbeen started?
-
isRunning
public boolean isRunning()Has theAgentbeen running?- Returns:
- has the
Agentbeen started successfully and not closed?
-
isClosed
public boolean isClosed()Has theAgentbeen closed?- Returns:
- has the
Agentbeen closed?
-
agent
TheAgentwhich is contained.- Returns:
Agentbeing contained.
-
start
public void start()Mark the invoker as started and call theAgent.onStart()method.Startup logic will only be performed once.
-
invoke
public int invoke()Invoke theAgent.doWork()method and return the work count.If an error occurs then the
AtomicCounter.increment()will be called on the errorCounter if not null and theThrowablewill be passed to theErrorHandler.onError(Throwable)method. If the error is anAgentTerminationExceptionthenclose()will be called after the error handler.If not successfully started or after closed then this method will return without invoking the
Agent.- Returns:
- the work count for the
Agent.doWork()method.
-
close
public final void close()Mark the invoker as closed and call theAgent.onClose()logic for clean up.The cleanup logic will only be performed once.
- Specified by:
closein interfaceAutoCloseable
-