Interface Task<T>
- Type Parameters:
T
- The result type
public interface Task<T>
Represents an asynchronous task.
Note: The underlying task may already be started.
Note: The underlying task may already be started.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
cancel()
Cancels the task and will emit aCancellationException
.get()
Blocks the current thread until the result is ready.boolean
Whether this task has started.Provide a callback for exception handling.Provide a callback for success handling.setTimeout
(long timeout, TimeUnit unit) Change the timeout duration for this task.setTimeout
(Duration timeout) Change the timeout duration for this task.
-
Method Details
-
isStarted
boolean isStarted()Whether this task has started.- Returns:
- True, if this task has already started.
-
onError
Provide a callback for exception handling.
This is an asynchronous operation.The error will be logged regardless of your callback, this only exists to handle failures for other purposes.
- Parameters:
callback
- The error callback- Returns:
- The current Task instance for chaining
- Throws:
IllegalArgumentException
- If null is provided
-
onSuccess
Provide a callback for success handling.
This is an asynchronous operation.- Parameters:
callback
- The success callback- Returns:
- The current Task instance for chaining
- Throws:
IllegalArgumentException
- If null is provided
-
setTimeout
Change the timeout duration for this task.
This may be ignored for certain operations.The provided timeout is relative to the start time of the task. If the time has already passed, this will immediately cancel the task.
- Parameters:
timeout
- The new timeout duration- Returns:
- The current Task instance for chaining
- Throws:
IllegalArgumentException
- If null is provided or the timeout is not positive
-
setTimeout
Change the timeout duration for this task.
This may be ignored for certain operations.The provided timeout is relative to the start time of the task. If the time has already passed, this will immediately cancel the task.
- Parameters:
timeout
- The new timeout durationunit
- The time unit of the timeout- Returns:
- The current Task instance for chaining
- Throws:
IllegalArgumentException
- If null is provided or the timeout is not positive
-
get
Blocks the current thread until the result is ready.
This will not work on the default JDA event thread because it might depend on other events to be processed, which could lead to a deadlock.- Returns:
- The result value
- Throws:
UnsupportedOperationException
- If this is called on the default JDA event threadCompletionException
- If some exception occurred (such asTimeoutException
).CancellationException
- If the request was cancelled
-
cancel
void cancel()Cancels the task and will emit aCancellationException
.
-