Module bus.http

Class CoverTasks.Executor

java.lang.Object
org.miaixz.bus.http.plugin.httpv.CoverTasks.Executor
Enclosing class:
CoverTasks

public static class CoverTasks.Executor extends Object
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
  • Constructor Details

  • Method Details

    • getExecutor

      public Executor getExecutor(boolean onIo)
      Gets the appropriate executor based on the onIo flag.
      Parameters:
      onIo - If true, returns the I/O executor; otherwise, returns the main executor.
      Returns:
      The selected executor.
    • download

      public Downloads download(CoverHttp<?> coverHttp, File file, InputStream input, long skipBytes)
      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 Downloads handler.
    • execute

      public void execute(Runnable command, boolean onIo)
      Executes a command on the appropriate thread pool.
      Parameters:
      command - The command to execute.
      onIo - If true, 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 - If true, 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 - If true, the callback is executed on the I/O thread pool.
      Returns:
      true if 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 - If true, the callback is executed on the I/O thread pool.
    • doMsgConvert

      public <V> V doMsgConvert(CoverTasks.Executor.ConvertFunc<V> callable)
      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.Data object 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 of ExecutorService.
    • getIoExecutor

      public Executor getIoExecutor()
      Returns:
      The I/O executor.
    • getMainExecutor

      public Executor getMainExecutor()
      Returns:
      The main-thread executor.
    • getDownloadListener

      public Downloads.Listener getDownloadListener()
      Returns:
      The global download listener.
    • getResponseListener

      public CoverTasks.Listener<CoverResult> getResponseListener()
      Returns:
      The global response listener.
    • getExceptionListener

      public CoverTasks.Listener<IOException> getExceptionListener()
      Returns:
      The global exception listener.
    • getCompleteListener

      public CoverTasks.Listener<CoverResult.State> getCompleteListener()
      Returns:
      The global completion listener.
    • getConvertors

      public Convertor[] getConvertors()
      Returns:
      The array of configured data converters.