Class CloudWatchEventDispatcher
- java.lang.Object
-
- com.perihelios.aws.lambda.cloudwatch.dispatcher.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 Summary
Constructors Constructor Description CloudWatchEventDispatcher(InputStream message, Context context)
Creates a dispatcher for a CloudWatch event, ready for further configuration.
-
Method Summary
Modifier and Type Method Description void
dispatch()
Dispatches the event to registered handlers.CloudWatchEventDispatcher
logMessage()
Instructs the dispatcher to log the incoming message, before it is parsed as JSON.<T extends CloudWatchEvent>
CloudWatchEventDispatcherwithEventHandler(Class<T> eventType, BiConsumer<? super T,Context> handler)
Registers an event handler for a particular CloudWatch event type.
-
-
-
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 JSONcontext
- AWS Lambda context, to be passed to handlers as they are invoked
-
-
Method Detail
-
withEventHandler
public <T extends CloudWatchEvent> CloudWatchEventDispatcher 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 withDetailType
. See the general description of that annotation, and ofCloudWatchEvent
, for details.- Type Parameters:
T
- type of event, with type bounds ensuring compatibility betweeneventType
andhandler
- Parameters:
eventType
- class to which events will be unmarshalledhandler
- consumer of events ofeventType
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 fromContext.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 missingdetail-type
ordetail
properties, or if the message'sdetail-type
does not correspond to any event types registered viawithEventHandler()
-
-