Package org.agrona.concurrent
Class AgentInvoker
- java.lang.Object
-
- org.agrona.concurrent.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 cycleAgent.doWork()
to be invoked directly.start()
should be called to allow theAgent.onStart()
to fire before any calls toinvoke()
of the agent duty cycle.Exceptions which occur during the
Agent.doWork()
invocation will be caught and passed to the providedErrorHandler
.Note: This class is not threadsafe.
-
-
Field Summary
Fields Modifier and Type Field Description private Agent
agent
private AtomicCounter
errorCounter
private ErrorHandler
errorHandler
private boolean
isClosed
private boolean
isRunning
private boolean
isStarted
-
Constructor Summary
Constructors Constructor Description AgentInvoker(ErrorHandler errorHandler, AtomicCounter errorCounter, Agent agent)
Create an agent and initialise it.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Agent
agent()
TheAgent
which is contained.void
close()
Mark the invoker as closed and call theAgent.onClose()
logic for clean up.private void
handleError(java.lang.Throwable throwable)
int
invoke()
Invoke theAgent.doWork()
method and return the work count.boolean
isClosed()
Has theAgent
been closed?boolean
isRunning()
Has theAgent
been running?boolean
isStarted()
Has theAgent
been started?void
start()
Mark the invoker as started and call theAgent.onStart()
method.
-
-
-
Field Detail
-
isClosed
private boolean isClosed
-
isStarted
private boolean isStarted
-
isRunning
private boolean isRunning
-
errorCounter
private final AtomicCounter errorCounter
-
errorHandler
private final ErrorHandler errorHandler
-
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 anThrowable
is encounterederrorCounter
- 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 theAgent
been started?- Returns:
- has the
Agent
been started?
-
isRunning
public boolean isRunning()
Has theAgent
been running?- Returns:
- has the
Agent
been started successfully and not closed?
-
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 theThrowable
will be passed to theErrorHandler.onError(Throwable)
method. If the error is anAgentTerminationException
thenclose()
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:
close
in interfacejava.lang.AutoCloseable
-
handleError
private void handleError(java.lang.Throwable throwable)
-
-