Class AgentRunner
- All Implemented Interfaces:
AutoCloseable,Runnable
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault retry timeout for closing.static final ThreadIndicates 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()TheAgentwhich is contained.final voidclose()Stop the running Agent and cleanup.final voidStop the running Agent and cleanup.booleanisClosed()Has theAgentbeen closed?voidrun()Run anAgent.static ThreadstartOnThread(AgentRunner runner) Start the given agent runner on a new thread.static ThreadstartOnThread(AgentRunner runner, ThreadFactory threadFactory) Start the given agent runner on a new thread.thread()Get the thread which is running thatAgent.
-
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:
-
-
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 anThrowableis 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
TheAgentwhich is contained.- Returns:
Agentbeing contained.
-
isClosed
public boolean isClosed()Has theAgentbeen closed?- Returns:
- has the
Agentbeen closed?
-
thread
Get the thread which is running thatAgent.If null then the runner has not been started. If
TOMBSTONEthen 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_MSvalue and a null action.- Specified by:
closein 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.
-