Results 1 to 4 of 4

Thread: Cloning payloads across subscribers when using publish-subscribe channel

  1. #1

    Default Cloning payloads across subscribers when using publish-subscribe channel

    Hi,

    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?

    Thanks!

  2. #2
    Join Date
    Jan 2008
    Location
    Mohnton, PA USA (that's near Philadelphia)
    Posts
    2,148

    Default

    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.

  3. #3

    Default

    Thanks Oleg,

    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.

    Thanks again.

  4. #4
    Join Date
    May 2007
    Location
    Netherlands
    Posts
    614

    Default

    If you're going to share payloads with multiple threads, make them immutable. There are other ways out, but they're harder.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •