I am working on handling Cart (commercial web app) on backend side with SpringBoot and Spring JMS. JMS producer will send Order message and consumer will pick it up and process.
Since application should be configured for multi-tenancy, I am keeping tenant ID in ThreadLocal. On producer side, everything is fine and tenant ID is available in ThreadLocal, but it's not on consumer side. How I can make it available on consumer side as well?
There is no guarantee that the JMS producer and consumer will be handled by the same thread. It is very unlikely to be honest. Heck they can be in different JVMs which makes it impossible. So you won't be able to pass information from the producer to the consumer using thread locals.
You have to add this information to the body of the message or add this to as a property.
Each message contains a built-in facility for supporting application-defined property values. Properties provide an efficient mechanism for supporting application-defined message filtering.
Source: Message Javadoc
Writing and reader the property is simple:
// Producer side
msg.setStringProperty("tenant", "tenant-1");
// Consumer side
String tenant = msg.getStringProperty("tenant");
As soon as you extract the tenant information on the consumer side you can put it to a thread local variable for further use.