I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications.
[App A] -> [App B] -> [App C]
We set a global id in the message header so we can trace each message lifecycle through the system.
I would like to be able to prepend any log message in the system with the message global id.
Has anyone else done this? Is there any way to associate this variable to the Thread so I can access it in future methods? I’d rather not pass the variable around in methods of the system.
I think ThreadLocal may be what you want here, though some may find this approach an abuse of ThreadLocal’s purpose, or good design. Something like:
The magic here is that myID’s value is actually specific to a Thread, and will be different when accessed from different threads.
You can then do what you like with the ID, including logging it.