Package org.rribbit

Class DefaultRequestResponseBus

java.lang.Object
org.rribbit.DefaultRequestResponseBus
All Implemented Interfaces:
RequestResponseBus

public class DefaultRequestResponseBus extends Object implements 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 Details

  • Constructor Details

  • Method Details

    • sendForSingleOfClass

      public <T> T sendForSingleOfClass(Class<T> returnType, Object... parameters)
      Description copied from interface: RequestResponseBus
      Send a request to all Listeners 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 interface RequestResponseBus
      Parameters:
      returnType - the return type that has to be assignable from the return type of the Listeners
      parameters - the parameters to give to the Listeners
      Returns:
      a single return value from one of the listeners that executed (which one is unspecified) or 'null' if no matching Listeners were found
    • sendForMultipleOfClass

      public <T> Collection<T> sendForMultipleOfClass(Class<T> returnType, Object... parameters)
      Description copied from interface: RequestResponseBus
      Send a request to all Listeners 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
      The number of Listeners that are executed may be larger than the number of return values that is returned, because some Listeners may return nothing (void).
      Specified by:
      sendForMultipleOfClass in interface RequestResponseBus
      Parameters:
      returnType - the return type that has to be assignable from the return type of the Listeners
      parameters - the parameters to give to the Listeners
      Returns:
      all return values from the listeners that executed or an empty Collection if no matching Listeners were found. This method never returns null.
    • sendForNothing

      public void sendForNothing(Object... parameters)
      Description copied from interface: RequestResponseBus
      Send a request to all Listeners 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 interface RequestResponseBus
      Parameters:
      parameters - the parameters to give to the Listeners
    • sendForSingleWithHint

      public <T> T sendForSingleWithHint(String hint, Object... parameters)
      Description copied from interface: RequestResponseBus
      Send a request to all Listeners 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 interface RequestResponseBus
      Parameters:
      hint - the hint that has to be equal to the hint of the Listeners
      parameters - the parameters to give to the Listeners
      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

      public <T> Collection<T> sendForMultipleWithHint(String hint, Object... parameters)
      Description copied from interface: RequestResponseBus
      Send a request to all Listeners 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
      The number of Listeners that are executed may be larger than the number of return values that is returned, because some Listeners may return nothing (void).
      Specified by:
      sendForMultipleWithHint in interface RequestResponseBus
      Parameters:
      hint - the hint that has to be equal to the hint of the Listeners
      parameters - the parameters to give to the Listeners
      Returns:
      all return values from the listeners that executed or an empty Collection if no matching Listeners were found or none of them returned anything. This method never returns null.
    • sendForNothingWithHint

      public void sendForNothingWithHint(String hint, Object... parameters)
      Description copied from interface: RequestResponseBus
      Send a request to all Listeners 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 interface RequestResponseBus
      Parameters:
      hint - the hint that has to be equal to the hint of the Listeners
      parameters - the parameters to give to the Listeners
    • sendForSingleOfClassWithHint

      public <T> T sendForSingleOfClassWithHint(Class<T> returnType, String hint, Object... parameters)
      Description copied from interface: RequestResponseBus
      Send a request to all Listeners 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 interface RequestResponseBus
      Parameters:
      returnType - the return type that has to be assignable from the return type of the Listeners
      hint - the hint that has to be equal to the hint of the Listeners
      parameters - the parameters to give to the Listeners
      Returns:
      a single return value from one of the listeners that executed (which one is unspecified) or 'null' if no matching Listeners were found
    • sendForMultipleOfClassWithHint

      public <T> Collection<T> sendForMultipleOfClassWithHint(Class<T> returnType, String hint, Object... parameters)
      Description copied from interface: RequestResponseBus
      Send a request to all Listeners 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
      The number of Listeners that are executed may be larger than the number of return values that is returned, because some Listeners may return nothing (void).
      Specified by:
      sendForMultipleOfClassWithHint in interface RequestResponseBus
      Parameters:
      returnType - the return type that has to be assignable from the return type of the Listeners
      hint - the hint that has to be equal to the hint of the Listeners
      parameters - the parameters to give to the Listeners
      Returns:
      all return values from the listeners that executed or an empty Collection if no matching Listeners were found. This method never returns null.
    • send

      public <T> T send(String hint, Object... parameters)
      Description copied from interface: RequestResponseBus
      Equivalent to RequestResponseBus.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 interface RequestResponseBus
      Parameters:
      hint -
      parameters -
      Returns:
      same as sendForSingleWithHint
    • dispatchRequestAndProcessResponse

      protected <T> List<T> dispatchRequestAndProcessResponse(Request request)
      Sends the given Request to all available RequestDispatchers and aggregates the Responses. If there are any Throwables, then an Exception is thrown, otherwise, the aggregated return values are returned.
      Parameters:
      request - The Request to be dispatched to the RequestDispatchers
      Returns:
      All return values returned by all RequestDispatchers
    • getRequestDispatchers

      public List<RequestDispatcher> getRequestDispatchers()
      Returns all RequestDispatchers that are used by this DefaultRequestResponseBus.
      Returns:
      All RequestDispatchers that are used by this DefaultRequestResponseBus
    • addRequestDispatcher

      public void addRequestDispatcher(RequestDispatcher requestDispatcher)
      Parameters:
      requestDispatcher - The RequestDispatcher that needs to be added to this DefaultRequestResponseBus
    • setRequestDispatcher

      public void setRequestDispatcher(RequestDispatcher requestDispatcher)
      Removes all RequestDispatchers from this DefaultRequestResponseBus and adds only the given one. After invoking this method, only the given RequestDispatcher will be used by this DefaultRequestResponseBus.
      Parameters:
      requestDispatcher - The RequestDispatcher that must be the sole RequestDispatcher used by this DefaultRequestResponseBus