Patrik Mihalčin
Patrik Mihalčin

Reputation: 4061

Multiple IntegrationFlows attached to the same request channel in Gateway method

Given I have application which uses Spring Integration and I define gateway:

@Component
@MessagingGateway
public interface SmsGateway {

    @Gateway(requestChannel = CHANNEL_SEND_SMS)
    void sendSms(SendSmsRequest request);
}

public interface IntegrationChannels {
    String CHANNEL_SEND_SMS = "channelSendSms";
}

I also attach IntegrationFlow to CHANNEL_SEND_SMS channel:

@Bean
public IntegrationFlow sendSmsFlow() {
    return IntegrationFlows.from(CHANNEL_SEND_SMS)
            .transform(...)
            .handle(...)
            .get();
}

Whenever I call sendSms gateway method from business code, sendSmsFlow is executed as expected.

When I want to attach another IntegrationFlow to the same CHANNEL_SEND_SMS channel, e.g.

@Bean
public IntegrationFlow differentFlow() {
    return IntegrationFlows.from(CHANNEL_SEND_SMS)
            .transform(...)
            .handle(...)
            .get();
}

then this differentFlow is not executed.

Why does it behave this way?

Is there any solution to make it work for both flows?

Upvotes: 0

Views: 53

Answers (1)

Gary Russell
Gary Russell

Reputation: 174779

The default channel type is DirectChannel and messages are distributed to multiple subscribed channels in a round robin fashion by default.

Declare CHANNEL_SEND_SMS as a PublishSubscribeChannel if you want each flow to get every message.

This will only work with a void gateway method; if there is a return type, you will get the first (or random if there is any async downstream processing) and the others will be discarded.

Upvotes: 1

Related Questions