java.lang.Object
org.miaixz.bus.http.plugin.httpv.CoverTasks.Executor
- Enclosing class:
CoverTasks
Manages the execution of asynchronous tasks, callbacks, and data conversions. This class orchestrates the use of
I/O and main-thread executors, global listeners, and a chain of data converters.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA functional interface for applying a conversion operation.static classA container for holding converted data along with its content type. -
Constructor Summary
ConstructorsConstructorDescriptionExecutor(Executor ioExecutor, Executor mainExecutor, Downloads.Listener downloadListener, CoverTasks.Listener<CoverResult> responseListener, CoverTasks.Listener<IOException> exceptionListener, CoverTasks.Listener<CoverResult.State> completeListener, Convertor[] convertors) Constructs a new Executor. -
Method Summary
Modifier and TypeMethodDescription<V> CoverTasks.Executor.Data<V> doMsgConvert(String type, CoverTasks.Executor.ConvertFunc<V> callable) Performs a data conversion by applying the given function to a suitable converter, optionally filtered by type.<V> VdoMsgConvert(CoverTasks.Executor.ConvertFunc<V> callable) Performs a data conversion by applying the given function to a suitable converter.download(CoverHttp<?> coverHttp, File file, InputStream input, long skipBytes) Creates a new download task handler.voidExecutes a command on the appropriate thread pool.voidexecuteOnComplete(CoverHttp<?> task, Callback<CoverResult.State> onComplete, CoverResult.State state, boolean onIo) Executes the completion callback for a task, respecting the global completion listener.booleanexecuteOnException(CoverHttp<?> task, Callback<IOException> onException, IOException error, boolean onIo) Executes the exception callback for a task, respecting the global exception listener.voidexecuteOnResponse(CoverHttp<?> task, Callback<CoverResult> onResponse, CoverResult result, boolean onIo) Executes the response callback for a task, respecting the global response listener.getExecutor(boolean onIo) Gets the appropriate executor based on theonIoflag.voidshutdown()Shuts down the I/O and main thread pools if they are instances ofExecutorService.
-
Constructor Details
-
Executor
public Executor(Executor ioExecutor, Executor mainExecutor, Downloads.Listener downloadListener, CoverTasks.Listener<CoverResult> responseListener, CoverTasks.Listener<IOException> exceptionListener, CoverTasks.Listener<CoverResult.State> completeListener, Convertor[] convertors) Constructs a new Executor.- Parameters:
ioExecutor- The executor for I/O tasks.mainExecutor- The executor for main-thread tasks.downloadListener- The global download listener.responseListener- The global response listener.exceptionListener- The global exception listener.completeListener- The global completion listener.convertors- The array of data converters.
-
-
Method Details
-
getExecutor
Gets the appropriate executor based on theonIoflag.- Parameters:
onIo- Iftrue, returns the I/O executor; otherwise, returns the main executor.- Returns:
- The selected executor.
-
download
Creates a new download task handler.- Parameters:
coverHttp- The HTTP task associated with the download.file- The destination file.input- The input stream of the data to be downloaded.skipBytes- The number of bytes to skip from the beginning.- Returns:
- A new
Downloadshandler.
-
execute
Executes a command on the appropriate thread pool.- Parameters:
command- The command to execute.onIo- Iftrue, the command is executed on the I/O thread pool; otherwise, on the main thread pool.
-
executeOnResponse
public void executeOnResponse(CoverHttp<?> task, Callback<CoverResult> onResponse, CoverResult result, boolean onIo) Executes the response callback for a task, respecting the global response listener.- Parameters:
task- The HTTP task.onResponse- The specific callback for this task.result- The successful result.onIo- Iftrue, the callback is executed on the I/O thread pool.
-
executeOnException
public boolean executeOnException(CoverHttp<?> task, Callback<IOException> onException, IOException error, boolean onIo) Executes the exception callback for a task, respecting the global exception listener.- Parameters:
task- The HTTP task.onException- The specific callback for this task.error- The exception that occurred.onIo- Iftrue, the callback is executed on the I/O thread pool.- Returns:
trueif an exception handler (either global or specific) was invoked.
-
executeOnComplete
public void executeOnComplete(CoverHttp<?> task, Callback<CoverResult.State> onComplete, CoverResult.State state, boolean onIo) Executes the completion callback for a task, respecting the global completion listener.- Parameters:
task- The HTTP task.onComplete- The specific callback for this task.state- The final state of the task.onIo- Iftrue, the callback is executed on the I/O thread pool.
-
doMsgConvert
Performs a data conversion by applying the given function to a suitable converter.- Type Parameters:
V- The type of the converted data.- Parameters:
callable- The conversion function to apply.- Returns:
- The converted data.
-
doMsgConvert
public <V> CoverTasks.Executor.Data<V> doMsgConvert(String type, CoverTasks.Executor.ConvertFunc<V> callable) Performs a data conversion by applying the given function to a suitable converter, optionally filtered by type.- Type Parameters:
V- The type of the converted data.- Parameters:
type- The content type to match (e.g., "json"). Can be null.callable- The conversion function to apply.- Returns:
- A
CoverTasks.Executor.Dataobject containing the converted data and its content type. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if no matching converter is found or if conversion fails.
-
shutdown
public void shutdown()Shuts down the I/O and main thread pools if they are instances ofExecutorService. -
getIoExecutor
- Returns:
- The I/O executor.
-
getMainExecutor
- Returns:
- The main-thread executor.
-
getDownloadListener
- Returns:
- The global download listener.
-
getResponseListener
- Returns:
- The global response listener.
-
getExceptionListener
- Returns:
- The global exception listener.
-
getCompleteListener
- Returns:
- The global completion listener.
-
getConvertors
- Returns:
- The array of configured data converters.
-