Class CloudWatchEventDispatcher


  • public class CloudWatchEventDispatcher
    extends Object
    Main entry point of the API—all users of this library will create and configure an instance of this class.

    A typical usage of this class in a Lambda function is:

         ...
         void myLambdaFunc(InputStream message, Context context) {
             new CloudWatchEventDispatcher(message, context)
                 .withEventHandler(MyEvent1.class, new MyEvent1Handler())
                 .withEventHandler(MyEvent2.class, new MyEvent2Handler())
                 .logMessage()
                 .dispatch();
         }
         ...
     
    • Constructor Detail

      • CloudWatchEventDispatcher

        public CloudWatchEventDispatcher​(InputStream message,
                                         Context context)
        Creates a dispatcher for a CloudWatch event, ready for further configuration.
        Parameters:
        message - raw message stream, presumed to contain CloudWatch event JSON
        context - AWS Lambda context, to be passed to handlers as they are invoked
    • Method Detail

      • withEventHandler

        public <T extends CloudWatchEventCloudWatchEventDispatcher withEventHandler​(Class<T> eventType,
                                                                                      BiConsumer<? super T,​Context> handler)
        Registers an event handler for a particular CloudWatch event type.

        All classes passed in eventType must be annotated with DetailType. See the general description of that annotation, and of CloudWatchEvent, for details.

        Type Parameters:
        T - type of event, with type bounds ensuring compatibility between eventType and handler
        Parameters:
        eventType - class to which events will be unmarshalled
        handler - consumer of events of eventType type
        Returns:
        a reference to this object
      • logMessage

        public CloudWatchEventDispatcher logMessage()
        Instructs the dispatcher to log the incoming message, before it is parsed as JSON.

        Activating logging via this method is primarily useful for troubleshooting errors where an AWS Lambda may be receiving messages other than CloudWatch events. CloudWatch will only send valid JSON documents, but it is possible to (erroneously) connect a Lambda function expecting CloudWatch events to another event source.

        Logging is done via the LambdaLogger obtained from Context.getLogger(). CloudWatch Logs must be enabled for the Lambda, and the Lambda's role must have the necessary permissions to write to CloudWatch Logs.

        Returns:
        a reference to this object
      • dispatch

        public void dispatch()
        Dispatches the event to registered handlers.

        This is the terminal operation of the dispatcher. All settings specified via the other methods of this class are applied at this time.

        Throws:
        IllegalArgumentException - if the message is not valid JSON, or if the message is missing detail-type or detail properties, or if the message's detail-type does not correspond to any event types registered via withEventHandler()