2

We have a requirement where I need the same message(payload) to be processed in two different channels. We are under the impression that using PubliSHSubscribe Channel will help us deal with that by making a copy of message to both the channels. However we figured that each channel was getting executed one after the other and if e make any changes in the payload in one channel, its effecting the payload of other channel as well.

@Bean
public IntegrationFlow bean1() {
    return IntegrationFlows
            .from("Channel1")
            .handle(MyMessage.class, (payload, header) -> obj1.method1(payload))
            .channel(MessageChannels.publishSubscribe("subscribableChannel").get())
            .get();
}


@Bean
public IntegrationFlow bean21() {
    return IntegrationFlows
            .from("subscribableChannel")
            .handle(MyMessage.class, (payload, header) -> obj2.method2(payload,header))
            .channel("nullChannel")
            .get();
}


@Bean
public IntegrationFlow bean22() {
    return IntegrationFlows
            .from("subscribableChannel")
            .handle(MyMessage.class, (payload, header) -> obj3.method3(payload))
            .channel("nullChannel")
            .get();
}

IN the above example, if i make changes to payload in bean21, its effecting the input payload passed to bean 22.

My requirement is to pass the same payload to bean21 and bean22 and to execute them parallely? Can you please advise how to accomplish that?

Sasi
  • 89
  • 8

1 Answers1

0

That's correct. Spring Integration is just Java and there is no any magic in copying payload between different messages. It is really just the same object in the memory. Now imagine you have a pure Java and would like to call two different method with the same Foo object. And then you modify that object in one method. What happens in another one? Right, it will see a modifications on the object.

To achieve your goal with definitely copying object to a new instance, you have to ensure it yourself. For example implement Cloneable interface on your class or provide copy constructor or any other possible solution to create a new object.

In the beginning of the flow of one of the subscriber you should perform that clone operation and you will have a new object without impacting another subscriber.

See more info in this JIRA: https://jira.spring.io/browse/INT-2979

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118