public class AgentRunner extends Object implements Runnable, AutoCloseable
Modifier and Type | Field and Description |
---|---|
static int |
RETRY_CLOSE_TIMEOUT_MS
Default retry timeout for closing.
|
static Thread |
TOMBSTONE
Indicates that the runner is being closed.
|
Constructor and Description |
---|
AgentRunner(IdleStrategy idleStrategy,
ErrorHandler errorHandler,
AtomicCounter errorCounter,
Agent agent)
Create an agent runner and initialise it.
|
Modifier and Type | Method and Description |
---|---|
Agent |
agent()
The
Agent which is contained. |
void |
close()
Stop the running Agent and cleanup.
|
void |
close(int retryCloseTimeoutMs,
Consumer<Thread> closeFailAction)
Stop the running Agent and cleanup.
|
boolean |
isClosed()
Has the
Agent been closed? |
void |
run()
Run an
Agent . |
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 |
thread()
Get the thread which is running that
Agent . |
public static final Thread TOMBSTONE
public static final int RETRY_CLOSE_TIMEOUT_MS
public AgentRunner(IdleStrategy idleStrategy, ErrorHandler errorHandler, AtomicCounter errorCounter, Agent agent)
idleStrategy
- to use for Agent run looperrorHandler
- to be called if an Throwable
is encounterederrorCounter
- to be incremented each time an exception is encountered. This may be null.agent
- to be run in this thread.public static Thread startOnThread(AgentRunner runner)
runner
- the agent runner to start.public static Thread startOnThread(AgentRunner runner, ThreadFactory threadFactory)
runner
- the agent runner to start.threadFactory
- the factory to use to create the thread.public Thread thread()
Agent
.
If null then the runner has not been started. If TOMBSTONE
then the runner is being closed.
Agent
.public final void close()
This is equivalent to calling close(int, Consumer)
using the default RETRY_CLOSE_TIMEOUT_MS
value and a
null action.
close
in interface AutoCloseable
public final void close(int retryCloseTimeoutMs, Consumer<Thread> closeFailAction)
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.
retryCloseTimeoutMs
- how long to wait before retryingcloseFailAction
- function to invoke before retrying after close timeoutCopyright © 2014-2019 Real Logic Ltd. All Rights Reserved.