Class CompositeInvocableHandler

java.lang.Object
io.awspring.cloud.sqs.listener.adapter.CompositeInvocableHandler

public class CompositeInvocableHandler extends Object
A composite of InvocableHandlerMethod that delegates message handling to a list of registered handlers, selecting the appropriate one based on the received message payload type.

If no matching handler is found, the defaultHandler is invoked. If a default handler is not configured, an IllegalStateException is thrown.

This class also does caching of resolved handlers and check for ambiguous handler methods.

Author:
José Iêdo
  • Constructor Details

    • CompositeInvocableHandler

      public CompositeInvocableHandler(InvocableHandlerMethod handler)
      Create a new CompositeInvocableHandler instance with the given handler.
      Parameters:
      handler - the handler.
    • CompositeInvocableHandler

      public CompositeInvocableHandler(List<InvocableHandlerMethod> handlers, @Nullable InvocableHandlerMethod defaultHandler)
      Create a new CompositeInvocableHandler instance with the given handlers.
      Parameters:
      handlers - the handlers.
      defaultHandler - the default handler.
  • Method Details

    • invoke

      public @Nullable Object invoke(Message<?> message) throws Exception
      Invoke the appropriate handler for the given message. If no handler is found, the default handler is invoked.
      Parameters:
      message - the message to handle
      Returns:
      the result of the handler invocation
      Throws:
      IllegalStateException - if no handler is found and no default handler is configured.
      Exception
    • findHandlerForPayload

      protected @Nullable InvocableHandlerMethod findHandlerForPayload(Class<?> payloadClass)
      Finds the appropriate handler method for the given payload type. Iterates through the list of handlers to find a match for the payload type. If multiple handlers match and none of them is the default handler, an exception is thrown. If no handler is found, the default handler is returned (if available).
      Parameters:
      payloadClass - the class of the payload to find a handler for
      Returns:
      the matching InvocableHandlerMethod, or the default handler if no match is found
      Throws:
      IllegalArgumentException - if multiple non-default handlers match the payload type