- All Implemented Interfaces:
RequestHandler,AutoCloseable
RequestHandler interface.
This handler supports both single and batch request dispatching, tracking responses using an internal
ConcurrentHashMap keyed by requestId. When a request is sent, the handler
subscribes to a corresponding result log (e.g., result or web response) via a
TrackingClient, which listens for responses targeted at this client only.
Each request is assigned a unique requestId and tagged with the client's source identifier.
When a response with a matching requestId is received, the corresponding CompletableFuture is
completed.
If no response is received within the configured timeout (default: 200 seconds), the future is completed exceptionally.
This request handle supports chunked responses. Request senders that can deal with chunked responses should use
sendRequest(SerializedMessage, Consumer, Duration, Consumer)}. If a chunked response is received, but the
request sender expected a single response, the intermediate responses are aggregated before completing the request.
Features:
- Supports both single and batch request dispatching.
- Tracks responses via the configured
MessageTypeand filters usingfilterMessageTarget = true. - Ensures startup of the underlying result tracker on first request dispatch.
- Cleans up subscriptions and pending futures on
close().
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final recordEncapsulates a callback mechanism to handle both intermediate and final responses when processing requests. -
Constructor Summary
ConstructorsConstructorDescriptionDefaultRequestHandler(Client client, io.fluxzero.common.MessageType resultType) Constructs a DefaultRequestHandler with the specified client and message type, and a default timeout of 200 seconds.DefaultRequestHandler(Client client, io.fluxzero.common.MessageType resultType, Duration timeout, String responseConsumerName) Constructs a DefaultRequestHandler instance, which manages request dispatching and response handling for a specified client and message type. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Releases all resources associated with this handler.protected voidprotected voidhandleResults(List<io.fluxzero.common.api.SerializedMessage> messages) protected CompletableFuture<io.fluxzero.common.api.SerializedMessage> prepareRequest(io.fluxzero.common.api.SerializedMessage request, Duration timeout, Consumer<io.fluxzero.common.api.SerializedMessage> intermediateCallback) CompletableFuture<io.fluxzero.common.api.SerializedMessage> sendRequest(io.fluxzero.common.api.SerializedMessage request, Consumer<io.fluxzero.common.api.SerializedMessage> requestSender, Duration timeout) Sends a request and processes the response, combining intermediate responses (if any) with the final response data.CompletableFuture<io.fluxzero.common.api.SerializedMessage> sendRequest(io.fluxzero.common.api.SerializedMessage request, Consumer<io.fluxzero.common.api.SerializedMessage> requestSender, Duration timeout, Consumer<io.fluxzero.common.api.SerializedMessage> intermediateCallback) List<CompletableFuture<io.fluxzero.common.api.SerializedMessage>> sendRequests(List<io.fluxzero.common.api.SerializedMessage> requests, Consumer<List<io.fluxzero.common.api.SerializedMessage>> requestSender) Sends multiple requests and returns a list of futures for their corresponding responses.List<CompletableFuture<io.fluxzero.common.api.SerializedMessage>> sendRequests(List<io.fluxzero.common.api.SerializedMessage> requests, Consumer<List<io.fluxzero.common.api.SerializedMessage>> requestSender, Duration timeout) Sends multiple requests with a custom timeout and returns a list of futures for their responses.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.fluxzero.sdk.publishing.RequestHandler
sendRequest, sendRequest
-
Constructor Details
-
DefaultRequestHandler
Constructs a DefaultRequestHandler with the specified client and message type, and a default timeout of 200 seconds.Uses a default name for the result consumer based on the application name.
Uses an unbounded virtual thread pool to complete requests.
- Parameters:
client- the client responsible for sending and receiving messagesresultType- the type of message expected as a result
-
DefaultRequestHandler
public DefaultRequestHandler(Client client, io.fluxzero.common.MessageType resultType, Duration timeout, String responseConsumerName) Constructs a DefaultRequestHandler instance, which manages request dispatching and response handling for a specified client and message type. Allows specifying a custom timeout duration and a response consumer name.Uses an unbounded virtual thread pool to complete requests.
- Parameters:
client- the client responsible for sending and receiving messagesresultType- the type of message expected as a resulttimeout- the duration to wait before the request times outresponseConsumerName- the name of the response consumer for managing response processing
-
-
Method Details
-
sendRequest
public CompletableFuture<io.fluxzero.common.api.SerializedMessage> sendRequest(io.fluxzero.common.api.SerializedMessage request, Consumer<io.fluxzero.common.api.SerializedMessage> requestSender, Duration timeout) Sends a request and processes the response, combining intermediate responses (if any) with the final response data. This method ensures intermediate results are aggregated and included in the final output.- Specified by:
sendRequestin interfaceRequestHandler- Parameters:
request- The request message to be sent.requestSender- A callback used to dispatch the request.timeout- The timeout for this request. A negative value indicates no timeout.- Returns:
- A
CompletableFuturethat completes with the response or fails on timeout.
-
sendRequest
public CompletableFuture<io.fluxzero.common.api.SerializedMessage> sendRequest(io.fluxzero.common.api.SerializedMessage request, Consumer<io.fluxzero.common.api.SerializedMessage> requestSender, Duration timeout, Consumer<io.fluxzero.common.api.SerializedMessage> intermediateCallback) - Specified by:
sendRequestin interfaceRequestHandler
-
sendRequests
public List<CompletableFuture<io.fluxzero.common.api.SerializedMessage>> sendRequests(List<io.fluxzero.common.api.SerializedMessage> requests, Consumer<List<io.fluxzero.common.api.SerializedMessage>> requestSender) Description copied from interface:RequestHandlerSends multiple requests and returns a list of futures for their corresponding responses.Each request is assigned a unique
requestIdand dispatched using the given sender. The returned list preserves the order of the input requests.- Specified by:
sendRequestsin interfaceRequestHandler- Parameters:
requests- The requests to send.requestSender- A callback used to dispatch the requests (e.g. batch publisher).- Returns:
- A list of
CompletableFutureinstances, one for each request.
-
sendRequests
public List<CompletableFuture<io.fluxzero.common.api.SerializedMessage>> sendRequests(List<io.fluxzero.common.api.SerializedMessage> requests, Consumer<List<io.fluxzero.common.api.SerializedMessage>> requestSender, Duration timeout) Description copied from interface:RequestHandlerSends multiple requests with a custom timeout and returns a list of futures for their responses.- Specified by:
sendRequestsin interfaceRequestHandler- Parameters:
requests- The requests to send.requestSender- A callback used to dispatch the requests.timeout- The timeout to apply per request. A negative value disables the timeout.- Returns:
- A list of
CompletableFutureinstances, one for each request.
-
prepareRequest
protected CompletableFuture<io.fluxzero.common.api.SerializedMessage> prepareRequest(io.fluxzero.common.api.SerializedMessage request, Duration timeout, Consumer<io.fluxzero.common.api.SerializedMessage> intermediateCallback) -
ensureStarted
protected void ensureStarted() -
handleResults
-
close
public void close()Description copied from interface:RequestHandlerReleases all resources associated with this handler.This typically shuts down any underlying
TrackingClientsubscriptions, and may cancel or complete any outstanding requests.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceRequestHandler
-