would_like_to_be_anon
would_like_to_be_anon

Reputation: 1727

Which spring integration pattern can I use?

I have a message that comes to my project. I need to persist the message and publish the message in a spring channel for subscriber to pick up the message and process it.

The publisher then sends back HTTP OK to the sender.

The subscriber needs to process the message with some business logic and retrying for a maximum of 3 times if there is an exception/error.

Which spring integration pattern can I use?

I am thinking about using PublishSubscribeChannel, the publisher sends the message to the subscriber channel and subscriber picks up the message and publisher needs to return OK to the sender. Not sure if this is right approach.

I am also trying to use spring annotations, could you give me links on which annotations can I use?

Upvotes: 0

Views: 210

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121552

Your use-case is typical for the Spring Integration and it is fully valid for integration stuff at all.

Looks like you should spend more time for the EIP theory, as you aren't sure what to do.

Well, PublishSubscribeChannel is for cases, when you need send the same message to several subscribers, however, of course, it works with the single subscriber as well. It is like topic in terms of JMS.

I'd implement your use-case like:

<int-http:ibound-gateway path="/foo" request-channel="input"/>

<int:channel id="input"/>

<int:service-activator input-channel="input" ref="myService" method="serviceMethod">
    <int:request-hadnler-advice-chain>
         <beans:bean class="org.springframework.integration.handler.advice.RequestHandlerRetryAdvice"/>
    </int:request-hadnler-advice-chain>
</int:service-activator>

Where the myService just should return OK String after business logic.

Since you implement request/reply case (HTTP) there is no reason to persist message, since HTTP client waits for reply immediately and your persistence for messages might break something in logic in the future. However it will work, of course, in this case, too - with message-store on <queue> of <channel>.

To make the same with annotations, it might look like this:

@Configuration
@EnableIntegration
@EnableRetry
@ComponentScan
public class HttpFlowSample {

    @Bean
    public MessageChannel input() {
        return new DirectChannel();
    }

    @Bean
    public MessagingGatewaySupport httpInboundGateway() {
        HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway();
        RequestMapping mapping = new RequestMapping();
        mapping.setPathPatterns("/foo");
        gateway.setRequestMapping(mapping);
        gateway.setRequestChannel(this.input());
        return gateway;
    }


    @MessageEndpoint
    public static class MyService {

        @ServiceActivator(inputChannel = "input")
        @Retryable
        public String serviceMethod(Object payload) {
            //Do business logic
            return "OK";
        }

    }

}

The annotation sample will work only with Spring Integration 4.0.

Let us know, if this is appropriate for you and I understood you fully.

Upvotes: 2

Related Questions