Module bus.http

Class Dispatcher

java.lang.Object
org.miaixz.bus.http.metric.Dispatcher

public class Dispatcher extends Object
Policy on when async requests are executed.

Each dispatcher uses an ExecutorService to run calls internally. If you supply your own executor, it should be able to run the configured maximum calls concurrently.

Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • Dispatcher

      public Dispatcher(ExecutorService executorService)
      Constructor that takes an ExecutorService.
      Parameters:
      executorService - The executor service to use for running calls.
    • Dispatcher

      public Dispatcher()
      Default constructor.
  • Method Details

    • executorService

      public ExecutorService executorService()
      Returns the executor service that runs calls. Creates one if none exists.
      Returns:
      The executor service.
    • getMaxRequests

      public int getMaxRequests()
      Returns the maximum number of simultaneous requests.
      Returns:
      The maximum number of requests.
    • setMaxRequests

      public void setMaxRequests(int maxRequests)
      Set the maximum number of requests to execute concurrently. Above this requests stay in memory, waiting for naming calls to finish. If more than maxRequests requests are in flight when this is called, they'll remain in flight.
      Parameters:
      maxRequests - The maximum number of requests.
    • getMaxRequestsPerHost

      public int getMaxRequestsPerHost()
      Returns the maximum number of simultaneous requests per host.
      Returns:
      The maximum number of requests per host.
    • setMaxRequestsPerHost

      public void setMaxRequestsPerHost(int maxRequestsPerHost)
      Set the maximum number of requests for each host to execute concurrently. This limits requests by the URL's host name. Note that concurrent requests to a single IP address may still exceed this limit: multiple hostnames may share an IP address or be routed through the same HTTP proxy. If more than maxRequestsPerHost requests are in flight when this is called, they'll remain in flight.
      Parameters:
      maxRequestsPerHost - The maximum number of requests per host.
    • setIdleCallback

      public void setIdleCallback(Runnable idleCallback)
      Set a callback to be invoked each time the dispatcher becomes idle (when the number of running calls returns to zero).
      Parameters:
      idleCallback - The callback.
    • enqueue

      public void enqueue(RealCall.AsyncCall call)
      Schedules call to be executed at some point in the future.
      Parameters:
      call - The asynchronous call to enqueue.
    • findExistingCallWithHost

      public RealCall.AsyncCall findExistingCallWithHost(String host)
      Finds an existing call (either running or ready) with the same host.
      Parameters:
      host - The host to search for.
      Returns:
      An existing async call, or null if none is found.
    • cancelAll

      public void cancelAll()
      Cancel all queued and running calls. Includes calls executed synchronously NewCall.execute() and asynchronously NewCall.enqueue(org.miaixz.bus.http.Callback).
    • promoteAndExecute

      public boolean promoteAndExecute()
      Promotes eligible calls from readyAsyncCalls to runningAsyncCalls and runs them on the executor service. Must not be called with synchronization because executing calls can call into user code.
      Returns:
      true if the dispatcher is currently running calls.
    • executed

      public void executed(RealCall call)
      Used by Call#execute to signal it is in-flight.
      Parameters:
      call - the call that has been executed.
    • finished

      public void finished(RealCall.AsyncCall call)
      Used by AsyncCall#run to signal completion.
      Parameters:
      call - the async call that has finished.
    • finished

      public void finished(RealCall call)
      Used by Call#execute to signal completion.
      Parameters:
      call - the call that has finished.
    • finished

      public <T> void finished(Deque<T> calls, T call)
      A general-purpose function to finish a call, either synchronous or asynchronous.
      Type Parameters:
      T - The type of the call.
      Parameters:
      calls - The deque of calls to remove from.
      call - The call to remove.
    • queuedCalls

      public List<NewCall> queuedCalls()
      Returns a snapshot of the calls currently awaiting execution.
      Returns:
      A list of queued calls.
    • runningCalls

      public List<NewCall> runningCalls()
      Returns a snapshot of the calls currently being executed.
      Returns:
      A list of running calls.
    • queuedCallsCount

      public int queuedCallsCount()
      Returns the number of calls that are waiting to be executed.
      Returns:
      The count of queued calls.
    • runningCallsCount

      public int runningCallsCount()
      Returns the number of calls that are currently being executed.
      Returns:
      The count of running calls.