Message Tracking
Tracking messages in your system can be useful to analyze what the cause of a message was. There are several ways to track messages in your system.
A clear solution to this, is tracing, albeit slightly involved. Other options you have through Axon Framework, are correlation data providers or straight out logging in interceptors You can correlate messages to each other with a Correlation Data Provider or log the messages being handled and dispatched.
Correlation data
The framework provides the CorrelationDataProvider, as described here.
This interface and its implementations provide you the means to populate the meta-data of a message based on the message that is currently being handled.
For instance, you could use this to store the command that triggered the event in the metadata of the event itself, allowing you to track the cause of it.
Axon Framework provides a MessageOriginProvider provider out of the box, which adds a correlationId and causationId to all messages.
The correlationId-id is the same over all messages that are triggered because of the same origin, while causationId contains the message identifier of the previous message.
We can see the effect of this in the following table:
Message identifier |
Type |
|
|
1 |
Command |
- |
- |
2 |
Event |
8231323 |
1 |
3 |
Command |
8231323 |
2 |
4 |
Event |
8231323 |
3 |
Check out the message correlation page to find out more about the MessageOriginProvider.
Interceptor logging
You can track the flow of messages through your application using logging.
To that end, you can leverage Axon’s handler- and dispatch interceptors.
Although a custom interceptor can be constructed, Axon Framework provides the LoggingInterceptor out of the box for this purpose.
The LoggingInterceptor acts as both a dispatch and handler interceptor, logging any type of message to SLF4J.