Class AgentRunner
- java.lang.Object
-
- org.agrona.concurrent.AgentRunner
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,java.lang.Runnable
public class AgentRunner extends java.lang.Object implements java.lang.Runnable, java.lang.AutoCloseable
Agent runner containing anAgent
which is run on aThread
.Note: An instance should only be started once and then discarded, it should not be reused.
-
-
Field Summary
Fields Modifier and Type Field Description private Agent
agent
private AtomicCounter
errorCounter
private ErrorHandler
errorHandler
private IdleStrategy
idleStrategy
private boolean
isClosed
private boolean
isRunning
static int
RETRY_CLOSE_TIMEOUT_MS
Default retry timeout for closing.private java.util.concurrent.atomic.AtomicReference<java.lang.Thread>
thread
static java.lang.Thread
TOMBSTONE
Indicates that the runner is being closed.
-
Constructor Summary
Constructors Constructor Description AgentRunner(IdleStrategy idleStrategy, ErrorHandler errorHandler, AtomicCounter errorCounter, Agent agent)
Create an agent runner and initialise it.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Agent
agent()
TheAgent
which is contained.void
close()
Stop the running Agent and cleanup.void
close(int retryCloseTimeoutMs, java.util.function.Consumer<java.lang.Thread> closeFailAction)
Stop the running Agent and cleanup.private void
doWork(IdleStrategy idleStrategy, Agent agent)
private void
failAction(java.util.function.Consumer<java.lang.Thread> closeFailAction, java.lang.Thread thread, java.lang.String message)
private void
handleError(java.lang.Throwable throwable)
boolean
isClosed()
Has theAgent
been closed?void
run()
Run anAgent
.static java.lang.Thread
startOnThread(AgentRunner runner)
Start the given agent runner on a new thread.static java.lang.Thread
startOnThread(AgentRunner runner, java.util.concurrent.ThreadFactory threadFactory)
Start the given agent runner on a new thread.java.lang.Thread
thread()
Get the thread which is running thatAgent
.private void
workLoop(IdleStrategy idleStrategy, Agent agent)
-
-
-
Field Detail
-
TOMBSTONE
public static final java.lang.Thread TOMBSTONE
Indicates that the runner is being closed.
-
RETRY_CLOSE_TIMEOUT_MS
public static final int RETRY_CLOSE_TIMEOUT_MS
Default retry timeout for closing.- See Also:
- Constant Field Values
-
isRunning
private volatile boolean isRunning
-
isClosed
private volatile boolean isClosed
-
errorCounter
private final AtomicCounter errorCounter
-
errorHandler
private final ErrorHandler errorHandler
-
idleStrategy
private final IdleStrategy idleStrategy
-
agent
private final Agent agent
-
thread
private final java.util.concurrent.atomic.AtomicReference<java.lang.Thread> thread
-
-
Constructor Detail
-
AgentRunner
public AgentRunner(IdleStrategy idleStrategy, ErrorHandler errorHandler, AtomicCounter errorCounter, Agent agent)
Create an agent runner and initialise it.- Parameters:
idleStrategy
- to use for Agent run looperrorHandler
- 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
-
startOnThread
public static java.lang.Thread startOnThread(AgentRunner runner)
Start the given agent runner on a new thread.- Parameters:
runner
- the agent runner to start.- Returns:
- the new thread that has been started.
-
startOnThread
public static java.lang.Thread startOnThread(AgentRunner runner, java.util.concurrent.ThreadFactory threadFactory)
Start the given agent runner on a new thread.- Parameters:
runner
- the agent runner to start.threadFactory
- the factory to use to create the thread.- Returns:
- the new thread that has been started.
-
thread
public java.lang.Thread thread()
Get the thread which is running thatAgent
.If null then the runner has not been started. If
TOMBSTONE
then the runner is being closed.- Returns:
- the thread running the
Agent
.
-
run
public void run()
- Specified by:
run
in interfacejava.lang.Runnable
-
close
public final void close()
Stop the running Agent and cleanup.This is equivalent to calling
close(int, Consumer)
using the defaultRETRY_CLOSE_TIMEOUT_MS
value and a null action.- Specified by:
close
in interfacejava.lang.AutoCloseable
-
close
public final void close(int retryCloseTimeoutMs, java.util.function.Consumer<java.lang.Thread> closeFailAction)
Stop the running Agent and cleanup.This will wait for the work loop to exit. The close timeout parameter controls how long we should wait before retrying to stop the agent by interrupting the thread. If the calling thread has its interrupt flag set then this method can return early before waiting for the running agent to close.
An optional action can be invoked whenever we time out while waiting which accepts the agent runner thread as the parameter (e.g. to obtain and log a stack trace from the thread). If the action is null, a message is written to stderr. Please note that a retry close timeout of zero waits indefinitely, in which case the fail action is only called on interrupt.
- Parameters:
retryCloseTimeoutMs
- how long to wait before retrying.closeFailAction
- function to invoke before retrying after close timeout.
-
failAction
private void failAction(java.util.function.Consumer<java.lang.Thread> closeFailAction, java.lang.Thread thread, java.lang.String message)
-
workLoop
private void workLoop(IdleStrategy idleStrategy, Agent agent)
-
doWork
private void doWork(IdleStrategy idleStrategy, Agent agent)
-
handleError
private void handleError(java.lang.Throwable throwable)
-
-