java.lang.Object
org.miaixz.bus.http.metric.Dispatcher
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 Summary
ConstructorsConstructorDescriptionDefault constructor.Dispatcher(ExecutorService executorService) Constructor that takes an ExecutorService. -
Method Summary
Modifier and TypeMethodDescriptionvoidCancel all queued and running calls.voidenqueue(RealCall.AsyncCall call) Schedulescallto be executed at some point in the future.voidUsed byCall#executeto signal it is in-flight.Returns the executor service that runs calls.Finds an existing call (either running or ready) with the same host.<T> voidA general-purpose function to finish a call, either synchronous or asynchronous.voidUsed byCall#executeto signal completion.voidfinished(RealCall.AsyncCall call) Used byAsyncCall#runto signal completion.intReturns the maximum number of simultaneous requests.intReturns the maximum number of simultaneous requests per host.booleanPromotes eligible calls fromreadyAsyncCallstorunningAsyncCallsand runs them on the executor service.Returns a snapshot of the calls currently awaiting execution.intReturns the number of calls that are waiting to be executed.Returns a snapshot of the calls currently being executed.intReturns the number of calls that are currently being executed.voidsetIdleCallback(Runnable idleCallback) Set a callback to be invoked each time the dispatcher becomes idle (when the number of running calls returns to zero).voidsetMaxRequests(int maxRequests) Set the maximum number of requests to execute concurrently.voidsetMaxRequestsPerHost(int maxRequestsPerHost) Set the maximum number of requests for each host to execute concurrently.
-
Constructor Details
-
Dispatcher
Constructor that takes an ExecutorService.- Parameters:
executorService- The executor service to use for running calls.
-
Dispatcher
public Dispatcher()Default constructor.
-
-
Method Details
-
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 thanmaxRequestsrequests 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 thanmaxRequestsPerHostrequests are in flight when this is called, they'll remain in flight.- Parameters:
maxRequestsPerHost- The maximum number of requests per host.
-
setIdleCallback
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
Schedulescallto be executed at some point in the future.- Parameters:
call- The asynchronous call to enqueue.
-
findExistingCallWithHost
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 fromreadyAsyncCallstorunningAsyncCallsand 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
Used byCall#executeto signal it is in-flight.- Parameters:
call- the call that has been executed.
-
finished
Used byAsyncCall#runto signal completion.- Parameters:
call- the async call that has finished.
-
finished
Used byCall#executeto signal completion.- Parameters:
call- the call that has finished.
-
finished
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
Returns a snapshot of the calls currently awaiting execution.- Returns:
- A list of queued calls.
-
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.
-