Package com.kosdev.kos.commons.util
Class TimedCallback
java.lang.Object
com.kosdev.kos.commons.util.TimedCallback
Wrapper for a callback that handles exceptions and measures the time the
callback took and logs a warning if it takes longer than expected. Since
most callbacks are intended to be completed quickly, this provides an easy
way to warn users when their code is unexpectedly using event threads for
long running tasks.
- Since:
- 10
- Version:
- 10
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcall(String name, Object listener, int warningTimeMs, TimedCallback.Callback callback) Call the specified callback in the current thread.static voidcall(String name, Object listener, TimedCallback.Callback callback) Call the specified callback with the default warning time.static voidexecute(RunnableQueues queues, Object queueKey, String name, Object listener, int warningTimeMs, TimedCallback.Callback callback) Call the specified callback in a thread pool thread using the specifiedRunnableQueuesand queue key.static voidexecute(String name, Object listener, int warningTimeMs, TimedCallback.Callback callback) Call the specified callback in a thread pool thread.static intReturn the current default warning time.
-
Field Details
-
DEFAULT_WARNING_TIME_MS
public static final int DEFAULT_WARNING_TIME_MS- See Also:
-
-
Constructor Details
-
TimedCallback
public TimedCallback()
-
-
Method Details
-
getDefaultWarningTimeMs
public static int getDefaultWarningTimeMs()Return the current default warning time.- Since:
- 10
-
call
Call the specified callback with the default warning time.- Parameters:
name- name of the call for use in log messageslistener- the listener being called, the class name is used in log messagescallback- the callback to run- Since:
- 10
-
call
public static void call(String name, Object listener, int warningTimeMs, TimedCallback.Callback callback) Call the specified callback in the current thread. Log an error if the callback throws an exception or a warning if it takes too long to run.- Parameters:
name- name of the call for use in log messageslistener- the listener being called, the class name is used in log messageswarningTimeMs- how long until a warning is generatedcallback- the callback to run- Since:
- 10
-
execute
public static void execute(String name, Object listener, int warningTimeMs, TimedCallback.Callback callback) Call the specified callback in a thread pool thread. Log an error if the callback throws an exception or a warning if it takes too long to run.- Parameters:
name- name of the call for use in log messageslistener- the listener being called, the class name is used in log messageswarningTimeMs- how long until a warning is generatedcallback- the callback to run- Since:
- 10
-
execute
public static void execute(RunnableQueues queues, Object queueKey, String name, Object listener, int warningTimeMs, TimedCallback.Callback callback) Call the specified callback in a thread pool thread using the specifiedRunnableQueuesand queue key. Log an error if the callback throws an exception or a warning if it takes too long to run. This allows callbacks to be executed in strict order, using concurrency where possible based on the queue key provided.- Parameters:
queues- theRunnableQueuesto execute fromqueueKey- the key for the queue to usename- name of the call for use in log messageslistener- the listener being called, the class name is used in log messageswarningTimeMs- how long until a warning is generatedcallback- the callback to run- Since:
- 10
-