【问题标题】:Which spring integration pattern can I use?我可以使用哪种弹簧集成模式?
【发布时间】:2014-07-13 14:52:03
【问题描述】:

我的项目有一条消息。我需要将消息持久化并在 spring 通道中发布消息,以便订阅者获取消息并进行处理。

然后发布者将 HTTP OK 发送回发送者。

订阅者需要对消息进行一些业务逻辑的处理,如果出现异常/错误,最多重试3次。

我可以使用哪种弹簧集成模式?

我正在考虑使用 PublishSubscribeChannel,发布者将消息发送到订阅者频道,订阅者接收消息,发布者需要向发送者返回 OK。不确定这是否是正确的方法。

我也在尝试使用spring注解,你能给我一些我可以使用哪些注解的链接吗?

【问题讨论】:

    标签: java spring spring-integration


    【解决方案1】:

    您的用例是 Spring 集成的典型用例,它完全适用于集成。

    看来你应该多花点时间学习 EIP 理论,因为你不知道该怎么做。

    好吧,PublishSubscribeChannel 适用于需要向多个订阅者发送相同消息的情况,当然,它也适用于单个订阅者。就 JMS 而言,它就像 topic。

    我会像这样实现你的用例:

    <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>
    

    myService 应该在业务逻辑之后返回 OK 字符串。

    由于您实现了请求/回复案例 (HTTP),因此没有理由保留消息,因为 HTTP 客户端会立即等待回复,并且您对消息的持久性可能会在将来破坏某些逻辑。但是,当然,在这种情况下它也可以工作 - 在 &lt;queue&gt; 的 &lt;channel&gt; 上使用 message-store。

    为了使注释相同,它可能看起来像这样:

    @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";
            }
    
        }
    
    }
    

    注释示例仅适用于 Spring Integration 4.0。

    如果这适合您,请告诉我们,我完全理解您。

    【讨论】:

    • 谢谢!唯一的区别是您的示例是使用网关来获取我们当前使用 REST 的请求。
    • RestTemplate 用于客户端。 &lt;int-http:inbound-gateway&gt; 是服务器端 Spring MVC Controller 的 @RequestMapping 的模拟。
    猜你喜欢
    • 1970-01-01
    • 2020-07-09
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 2015-12-12
    • 2023-03-09
    相关资源
    最近更新 更多