Aug 22nd, 2010, 12:46 AM
Cloning payloads across subscribers when using publish-subscribe channel
I've noticed that when messages are dispatched across several subscribers (on a publish-subscribe channel), the same payload instance is being shared across all messages. This means that if the payload is a collection, modifying the collection on one subscriber might affect a different subscriber working on the same collection.
What is the best practice for cloning the payload when using a publish-subscribe channel?
Aug 25th, 2010, 10:35 PM
There is nothing specific in SI nor there is any recommendation that we can give. I think its on the per use case basis and is up to you - the developer.
The main issue as I am sure you'd agree is concurrency but based on the load and other criteria it could be handled differently (synchronization vs cloning, etc. . .).
The only thing framework handles for you is what is in our control and that is a message. Every time a message makes a hop its a new instance of the message built form the combination of values form old message plus new stuff.
Aug 26th, 2010, 01:29 AM
I've kind of figured that out. I was hoping that, considering the fact that my use case is not that uncommon, there is already some "magic" solution somewhere. For example, a pub-subscribe channel could be configured to be a "cloning-channel" which clones the payload of its messages (allowing the developer to only write the cloning logic and defining the name of the cloning-method).
But I understand that this is not in the scope of the spring-integration framework, so as you said - it's a concurrency problem and can be dealt with in the traditional ways.
Aug 26th, 2010, 03:19 PM
If you're going to share payloads with multiple threads, make them immutable. There are other ways out, but they're harder.