Class AgentRunner
- All Implemented Interfaces:
AutoCloseable
,Runnable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Agent
private final AtomicCounter
private final ErrorHandler
private final IdleStrategy
private boolean
private boolean
static final int
Default retry timeout for closing.private final AtomicReference
<Thread> static final Thread
Indicates that the runner is being closed. -
Constructor Summary
ConstructorsConstructorDescriptionAgentRunner
(IdleStrategy idleStrategy, ErrorHandler errorHandler, AtomicCounter errorCounter, Agent agent) Create an agent runner and initialise it. -
Method Summary
Modifier and TypeMethodDescriptionagent()
TheAgent
which is contained.final void
close()
Stop the running Agent and cleanup.final void
Stop the running Agent and cleanup.private void
doWork
(IdleStrategy idleStrategy, Agent agent) private void
failAction
(Consumer<Thread> closeFailAction, Thread thread, String message) private void
handleError
(Throwable throwable) boolean
isClosed()
Has theAgent
been closed?void
run()
Run anAgent
.static Thread
startOnThread
(AgentRunner runner) Start the given agent runner on a new thread.static Thread
startOnThread
(AgentRunner runner, ThreadFactory threadFactory) Start the given agent runner on a new thread.thread()
Get the thread which is running thatAgent
.private void
workLoop
(IdleStrategy idleStrategy, Agent agent)
-
Field Details
-
TOMBSTONE
Indicates that the runner is being closed. -
RETRY_CLOSE_TIMEOUT_MS
public static final int RETRY_CLOSE_TIMEOUT_MSDefault retry timeout for closing.- See Also:
-
isRunning
private volatile boolean isRunning -
isClosed
private volatile boolean isClosed -
errorCounter
-
errorHandler
-
idleStrategy
-
agent
-
thread
-
-
Constructor Details
-
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 Details
-
startOnThread
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
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.
-
agent
TheAgent
which is contained.- Returns:
Agent
being contained.
-
isClosed
public boolean isClosed()Has theAgent
been closed?- Returns:
- has the
Agent
been closed?
-
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() -
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 interfaceAutoCloseable
-
close
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
-
workLoop
-
doWork
-
handleError
-