Package org.rribbit
Class DefaultRequestResponseBus
java.lang.Object
org.rribbit.DefaultRequestResponseBus
- All Implemented Interfaces:
RequestResponseBus
Default implementation of
RequestResponseBus. Provides all behaviour specified in that interface.
The DefaultRequestResponseBus supports multiple RequestDispatchers and every request will be sent to ALL of them. The responses will then be aggregated. Please keep
performance considerations in mind when adding multiple RequestDispatchers to a DefaultRequestResponseBus, especially when using RRiBbit Remoting. In that case,
every request will be sent to ALL remote machines and the DefaultRequestResponseBus will have to wait until all of them have returned.
If this is undesirable, an alternative is to create multiple DefaultRequestResponseBuses, each with its own RequestDispatcher. This does however, require the sender
of the request to have knowledge about where the receiver is located (i.e. which RequestResponseBus to call). Although this contradicts the principle behind event buses (in that
the sender does not have to know anything about the receiver), it could be a worthwile sacrifice for performance considerations.- Author:
- G.J. Schouten
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionWhenever you use this constructor, be sure to set theRequestDispatcherwith the setter provided by this class.DefaultRequestResponseBus(RequestDispatcher... requestDispatchers) This constructor is recommended, since it forces you to specify aRequestDispatcher. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddRequestDispatcher(RequestDispatcher requestDispatcher) Adds aRequestDispatcherto thisDefaultRequestResponseBus.protected <T> List<T> dispatchRequestAndProcessResponse(Request request) Returns allRequestDispatchers that are used by thisDefaultRequestResponseBus.<T> TEquivalent toRequestResponseBus.sendForSingleWithHint(String, Object...), since that is probably the most widely used method, because generally, when you know the hint, you know the returntype and you don't have to pass it specifically.<T> Collection<T> sendForMultipleOfClass(Class<T> returnType, Object... parameters) Send a request to allListeners that satisfy the following requirements.<T> Collection<T> sendForMultipleOfClassWithHint(Class<T> returnType, String hint, Object... parameters) Send a request to allListeners that satisfy the following requirements.<T> Collection<T> sendForMultipleWithHint(String hint, Object... parameters) Send a request to allListeners that satisfy the following requirements.voidsendForNothing(Object... parameters) Send a request to allListeners that satisfy the following requirements.voidsendForNothingWithHint(String hint, Object... parameters) Send a request to allListeners that satisfy the following requirements.<T> TsendForSingleOfClass(Class<T> returnType, Object... parameters) Send a request to allListeners that satisfy the following requirements.<T> TsendForSingleOfClassWithHint(Class<T> returnType, String hint, Object... parameters) Send a request to allListeners that satisfy the following requirements.<T> TsendForSingleWithHint(String hint, Object... parameters) Send a request to allListeners that satisfy the following requirements.voidsetRequestDispatcher(RequestDispatcher requestDispatcher) Removes allRequestDispatchers from thisDefaultRequestResponseBusand adds only the given one.
-
Field Details
-
requestDispatchers
-
-
Constructor Details
-
DefaultRequestResponseBus
public DefaultRequestResponseBus()Whenever you use this constructor, be sure to set theRequestDispatcherwith the setter provided by this class. If you don't, runtimeNullPointerExceptions will occur. -
DefaultRequestResponseBus
This constructor is recommended, since it forces you to specify aRequestDispatcher. Passing a null value will result in a runtimeNullPointerExceptionwhenever theDefaultRequestResponseBusis used.- Parameters:
requestDispatchers- TheRequestDispatchers to be used by thisDefaultRequestResponseBus
-
-
Method Details
-
sendForSingleOfClass
Description copied from interface:RequestResponseBusSend a request to allListeners that satisfy the following requirements.- They return an
Objectthat the parameter 'returnType' is assignable from and - they belong to a method with parameters that match the given 'parameters' objects
- Specified by:
sendForSingleOfClassin interfaceRequestResponseBus- Parameters:
returnType- the return type that has to be assignable from the return type of theListenersparameters- the parameters to give to theListeners- Returns:
- a single return value from one of the listeners that executed (which one is unspecified) or 'null' if no matching
Listeners were found
- They return an
-
sendForMultipleOfClass
Description copied from interface:RequestResponseBusSend a request to allListeners that satisfy the following requirements.- They return an
Objectthat the parameter 'returnType' is assignable from and - they belong to a method with parameters that match the given 'parameters' objects
Listeners that are executed may be larger than the number of return values that is returned, because someListeners may return nothing (void).- Specified by:
sendForMultipleOfClassin interfaceRequestResponseBus- Parameters:
returnType- the return type that has to be assignable from the return type of theListenersparameters- the parameters to give to theListeners- Returns:
- all return values from the listeners that executed or an empty
Collectionif no matchingListeners were found. This method never returns null.
- They return an
-
sendForNothing
Description copied from interface:RequestResponseBusSend a request to allListeners that satisfy the following requirements. Ignore all return values and return nothing.- They belong to a method with parameters that match the given 'parameters' objects
- Specified by:
sendForNothingin interfaceRequestResponseBus- Parameters:
parameters- the parameters to give to theListeners
-
sendForSingleWithHint
Description copied from interface:RequestResponseBusSend a request to allListeners that satisfy the following requirements.- They have a hint that is equals to the 'hint' parameter and
- they belong to a method with parameters that match the given 'parameters' objects
- Specified by:
sendForSingleWithHintin interfaceRequestResponseBus- Parameters:
hint- the hint that has to be equal to the hint of theListenersparameters- the parameters to give to theListeners- Returns:
- a single return value from one of the listeners that executed (which one is unspecified) or 'null' if no matching
Listeners were found or none of them returned anything
-
sendForMultipleWithHint
Description copied from interface:RequestResponseBusSend a request to allListeners that satisfy the following requirements.- They have a hint that is equals to the 'hint' parameter and
- they belong to a method with parameters that match the given 'parameters' objects
Listeners that are executed may be larger than the number of return values that is returned, because someListeners may return nothing (void).- Specified by:
sendForMultipleWithHintin interfaceRequestResponseBus- Parameters:
hint- the hint that has to be equal to the hint of theListenersparameters- the parameters to give to theListeners- Returns:
- all return values from the listeners that executed or an empty
Collectionif no matchingListeners were found or none of them returned anything. This method never returns null.
-
sendForNothingWithHint
Description copied from interface:RequestResponseBusSend a request to allListeners that satisfy the following requirements. Ignore all return values and return nothing.- They have a hint that is equals to the 'hint' parameter and
- they belong to a method with parameters that match the given 'parameters' objects
- Specified by:
sendForNothingWithHintin interfaceRequestResponseBus- Parameters:
hint- the hint that has to be equal to the hint of theListenersparameters- the parameters to give to theListeners
-
sendForSingleOfClassWithHint
Description copied from interface:RequestResponseBusSend a request to allListeners that satisfy the following requirements.- They return an
Objectthat the parameter 'returnType' is assignable from and - they have a hint that is equals to the 'hint' parameter and
- they belong to a method with parameters that match the given 'parameters' objects
- Specified by:
sendForSingleOfClassWithHintin interfaceRequestResponseBus- Parameters:
returnType- the return type that has to be assignable from the return type of theListenershint- the hint that has to be equal to the hint of theListenersparameters- the parameters to give to theListeners- Returns:
- a single return value from one of the listeners that executed (which one is unspecified) or 'null' if no matching
Listeners were found
- They return an
-
sendForMultipleOfClassWithHint
public <T> Collection<T> sendForMultipleOfClassWithHint(Class<T> returnType, String hint, Object... parameters) Description copied from interface:RequestResponseBusSend a request to allListeners that satisfy the following requirements.- They return an
Objectthat the parameter 'returnType' is assignable from and - they have a hint that is equals to the 'hint' parameter and
- they belong to a method with parameters that match the given 'parameters' objects
Listeners that are executed may be larger than the number of return values that is returned, because someListeners may return nothing (void).- Specified by:
sendForMultipleOfClassWithHintin interfaceRequestResponseBus- Parameters:
returnType- the return type that has to be assignable from the return type of theListenershint- the hint that has to be equal to the hint of theListenersparameters- the parameters to give to theListeners- Returns:
- all return values from the listeners that executed or an empty
Collectionif no matchingListeners were found. This method never returns null.
- They return an
-
send
Description copied from interface:RequestResponseBusEquivalent toRequestResponseBus.sendForSingleWithHint(String, Object...), since that is probably the most widely used method, because generally, when you know the hint, you know the returntype and you don't have to pass it specifically. This method is just a convenience shorthand method with a shorter name.- Specified by:
sendin interfaceRequestResponseBus- Parameters:
hint-parameters-- Returns:
- same as sendForSingleWithHint
-
dispatchRequestAndProcessResponse
Sends the givenRequestto all availableRequestDispatchers and aggregates theResponses. If there are anyThrowables, then anExceptionis thrown, otherwise, the aggregated return values are returned.- Parameters:
request- TheRequestto be dispatched to theRequestDispatchers- Returns:
- All return values returned by all
RequestDispatchers
-
getRequestDispatchers
Returns allRequestDispatchers that are used by thisDefaultRequestResponseBus.- Returns:
- All
RequestDispatchers that are used by thisDefaultRequestResponseBus
-
addRequestDispatcher
Adds aRequestDispatcherto thisDefaultRequestResponseBus.- Parameters:
requestDispatcher- TheRequestDispatcherthat needs to be added to thisDefaultRequestResponseBus
-
setRequestDispatcher
Removes allRequestDispatchers from thisDefaultRequestResponseBusand adds only the given one. After invoking this method, only the givenRequestDispatcherwill be used by thisDefaultRequestResponseBus.- Parameters:
requestDispatcher- TheRequestDispatcherthat must be the soleRequestDispatcherused by thisDefaultRequestResponseBus
-