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 RequestDispatcher
s 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 RequestDispatcher
s 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 DefaultRequestResponseBus
es, 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
-
Constructor Summary
ConstructorDescriptionWhenever you use this constructor, be sure to set theRequestDispatcher
with the setter provided by this class.DefaultRequestResponseBus
(RequestDispatcher... requestDispatchers) This constructor is recommended, since it forces you to specify aRequestDispatcher
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addRequestDispatcher
(RequestDispatcher requestDispatcher) Adds aRequestDispatcher
to thisDefaultRequestResponseBus
.protected <T> List
<T> dispatchRequestAndProcessResponse
(Request request) Returns allRequestDispatcher
s that are used by thisDefaultRequestResponseBus
.<T> T
Equivalent 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 allListener
s that satisfy the following requirements.<T> Collection
<T> sendForMultipleOfClassWithHint
(Class<T> returnType, String hint, Object... parameters) Send a request to allListener
s that satisfy the following requirements.<T> Collection
<T> sendForMultipleWithHint
(String hint, Object... parameters) Send a request to allListener
s that satisfy the following requirements.void
sendForNothing
(Object... parameters) Send a request to allListener
s that satisfy the following requirements.void
sendForNothingWithHint
(String hint, Object... parameters) Send a request to allListener
s that satisfy the following requirements.<T> T
sendForSingleOfClass
(Class<T> returnType, Object... parameters) Send a request to allListener
s that satisfy the following requirements.<T> T
sendForSingleOfClassWithHint
(Class<T> returnType, String hint, Object... parameters) Send a request to allListener
s that satisfy the following requirements.<T> T
sendForSingleWithHint
(String hint, Object... parameters) Send a request to allListener
s that satisfy the following requirements.void
setRequestDispatcher
(RequestDispatcher requestDispatcher) Removes allRequestDispatcher
s from thisDefaultRequestResponseBus
and adds only the given one.
-
Field Details
-
requestDispatchers
-
-
Constructor Details
-
DefaultRequestResponseBus
public DefaultRequestResponseBus()Whenever you use this constructor, be sure to set theRequestDispatcher
with the setter provided by this class. If you don't, runtimeNullPointerException
s will occur. -
DefaultRequestResponseBus
This constructor is recommended, since it forces you to specify aRequestDispatcher
. Passing a null value will result in a runtimeNullPointerException
whenever theDefaultRequestResponseBus
is used.- Parameters:
requestDispatchers
- TheRequestDispatcher
s to be used by thisDefaultRequestResponseBus
-
-
Method Details
-
sendForSingleOfClass
Description copied from interface:RequestResponseBus
Send a request to allListener
s that satisfy the following requirements.- They return an
Object
that the parameter 'returnType' is assignable from and - they belong to a method with parameters that match the given 'parameters' objects
- Specified by:
sendForSingleOfClass
in interfaceRequestResponseBus
- Parameters:
returnType
- the return type that has to be assignable from the return type of theListener
sparameters
- the parameters to give to theListener
s- Returns:
- a single return value from one of the listeners that executed (which one is unspecified) or 'null' if no matching
Listener
s were found
- They return an
-
sendForMultipleOfClass
Description copied from interface:RequestResponseBus
Send a request to allListener
s that satisfy the following requirements.- They return an
Object
that the parameter 'returnType' is assignable from and - they belong to a method with parameters that match the given 'parameters' objects
Listener
s that are executed may be larger than the number of return values that is returned, because someListener
s may return nothing (void).- Specified by:
sendForMultipleOfClass
in interfaceRequestResponseBus
- Parameters:
returnType
- the return type that has to be assignable from the return type of theListener
sparameters
- the parameters to give to theListener
s- Returns:
- all return values from the listeners that executed or an empty
Collection
if no matchingListener
s were found. This method never returns null.
- They return an
-
sendForNothing
Description copied from interface:RequestResponseBus
Send a request to allListener
s 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:
sendForNothing
in interfaceRequestResponseBus
- Parameters:
parameters
- the parameters to give to theListener
s
-
sendForSingleWithHint
Description copied from interface:RequestResponseBus
Send a request to allListener
s 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:
sendForSingleWithHint
in interfaceRequestResponseBus
- Parameters:
hint
- the hint that has to be equal to the hint of theListener
sparameters
- the parameters to give to theListener
s- Returns:
- a single return value from one of the listeners that executed (which one is unspecified) or 'null' if no matching
Listener
s were found or none of them returned anything
-
sendForMultipleWithHint
Description copied from interface:RequestResponseBus
Send a request to allListener
s 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
Listener
s that are executed may be larger than the number of return values that is returned, because someListener
s may return nothing (void).- Specified by:
sendForMultipleWithHint
in interfaceRequestResponseBus
- Parameters:
hint
- the hint that has to be equal to the hint of theListener
sparameters
- the parameters to give to theListener
s- Returns:
- all return values from the listeners that executed or an empty
Collection
if no matchingListener
s were found or none of them returned anything. This method never returns null.
-
sendForNothingWithHint
Description copied from interface:RequestResponseBus
Send a request to allListener
s 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:
sendForNothingWithHint
in interfaceRequestResponseBus
- Parameters:
hint
- the hint that has to be equal to the hint of theListener
sparameters
- the parameters to give to theListener
s
-
sendForSingleOfClassWithHint
Description copied from interface:RequestResponseBus
Send a request to allListener
s that satisfy the following requirements.- They return an
Object
that 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:
sendForSingleOfClassWithHint
in interfaceRequestResponseBus
- Parameters:
returnType
- the return type that has to be assignable from the return type of theListener
shint
- the hint that has to be equal to the hint of theListener
sparameters
- the parameters to give to theListener
s- Returns:
- a single return value from one of the listeners that executed (which one is unspecified) or 'null' if no matching
Listener
s were found
- They return an
-
sendForMultipleOfClassWithHint
public <T> Collection<T> sendForMultipleOfClassWithHint(Class<T> returnType, String hint, Object... parameters) Description copied from interface:RequestResponseBus
Send a request to allListener
s that satisfy the following requirements.- They return an
Object
that 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
Listener
s that are executed may be larger than the number of return values that is returned, because someListener
s may return nothing (void).- Specified by:
sendForMultipleOfClassWithHint
in interfaceRequestResponseBus
- Parameters:
returnType
- the return type that has to be assignable from the return type of theListener
shint
- the hint that has to be equal to the hint of theListener
sparameters
- the parameters to give to theListener
s- Returns:
- all return values from the listeners that executed or an empty
Collection
if no matchingListener
s were found. This method never returns null.
- They return an
-
send
Description copied from interface:RequestResponseBus
Equivalent 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:
send
in interfaceRequestResponseBus
- Parameters:
hint
-parameters
-- Returns:
- same as sendForSingleWithHint
-
dispatchRequestAndProcessResponse
Sends the givenRequest
to all availableRequestDispatcher
s and aggregates theResponse
s. If there are anyThrowable
s, then anException
is thrown, otherwise, the aggregated return values are returned.- Parameters:
request
- TheRequest
to be dispatched to theRequestDispatcher
s- Returns:
- All return values returned by all
RequestDispatcher
s
-
getRequestDispatchers
Returns allRequestDispatcher
s that are used by thisDefaultRequestResponseBus
.- Returns:
- All
RequestDispatcher
s that are used by thisDefaultRequestResponseBus
-
addRequestDispatcher
Adds aRequestDispatcher
to thisDefaultRequestResponseBus
.- Parameters:
requestDispatcher
- TheRequestDispatcher
that needs to be added to thisDefaultRequestResponseBus
-
setRequestDispatcher
Removes allRequestDispatcher
s from thisDefaultRequestResponseBus
and adds only the given one. After invoking this method, only the givenRequestDispatcher
will be used by thisDefaultRequestResponseBus
.- Parameters:
requestDispatcher
- TheRequestDispatcher
that must be the soleRequestDispatcher
used by thisDefaultRequestResponseBus
-