Spring Integration 子流程取决于 .handle 方法

发布于 2025-01-15 01:14:13 字数 1981 浏览 2 评论 0原文

我正在使用 spring 集成在特定重试完成后更改流程。我的 errorResponse 的 IntegrationFlow bean 如下:

@Bean
public IntegrationFlow errorMailResponse(@Qualifier(ERROR_CHANNEL) PollableChannel errorChannel) {
    return IntegrationFlows.from(errorChannel)
            .handle(MessagingException.class, (payload, headers) -> handleMessageException(payload),
                    e -> e.poller(p -> p.fixedDelay(pollerInterval)))
            .channel(NO_OUTPUT_CHANNEL)
            .get();
}

如果方法 handleMessageException 返回一个对象,我希望流程继续到特定通道 - MAIN_EVENTS_CHANNEL 如果 handleMessageException 返回 null,我希望继续到 NO_OUTPUT_CHANNEL 。

通过 Spring 集成可以实现吗?我尝试使用子流,但我不确定这是否是这样。 https://docs.spring.io/ spring-integration/reference/html/dsl.html#java-dsl-subflows

@Bean
public IntegrationFlow errorMailResponse(@Qualifier(ERROR_CHANNEL) PollableChannel errorChannel) {
    return IntegrationFlows.from(errorChannel)
            .handle(MessagingException.class, (payload, headers) -> handleMailMessageException(payload),
                    e -> e.poller(p -> p.fixedDelay(pollerInterval)))
            .publishSubscribeChannel(subscription -> subscription
                    .subscribe(subflow -> subflow
                            .<MailPojo>handle((payload, headers) -> {
                                // if if result handleMessageException  == null
                            })
                            .channel(NO_OUTPUT_CHANNEL))
                    .subscribe(subflow -> subflow
                            .<MailPojo>handle((payload, headers) -> {
                               // if result handleMessageException  !=null
                            })
                            .channel(MAIN_EVENTS_CHANNEL)))
            .get();
}

感谢任何帮助。

I am using spring integration to change a flow after particular retry is completed. My IntegrationFlow bean for errorResponse is as follows:

@Bean
public IntegrationFlow errorMailResponse(@Qualifier(ERROR_CHANNEL) PollableChannel errorChannel) {
    return IntegrationFlows.from(errorChannel)
            .handle(MessagingException.class, (payload, headers) -> handleMessageException(payload),
                    e -> e.poller(p -> p.fixedDelay(pollerInterval)))
            .channel(NO_OUTPUT_CHANNEL)
            .get();
}

If method handleMessageException returns an object I want the flow to continue to particular channel - MAIN_EVENTS_CHANNEL if handleMessageException returns null I want to continue to NO_OUTPUT_CHANNEL.

Is that possible to achieve with Spring integration? I tried to use subflow but I am not sure if it is the way. https://docs.spring.io/spring-integration/reference/html/dsl.html#java-dsl-subflows

@Bean
public IntegrationFlow errorMailResponse(@Qualifier(ERROR_CHANNEL) PollableChannel errorChannel) {
    return IntegrationFlows.from(errorChannel)
            .handle(MessagingException.class, (payload, headers) -> handleMailMessageException(payload),
                    e -> e.poller(p -> p.fixedDelay(pollerInterval)))
            .publishSubscribeChannel(subscription -> subscription
                    .subscribe(subflow -> subflow
                            .<MailPojo>handle((payload, headers) -> {
                                // if if result handleMessageException  == null
                            })
                            .channel(NO_OUTPUT_CHANNEL))
                    .subscribe(subflow -> subflow
                            .<MailPojo>handle((payload, headers) -> {
                               // if result handleMessageException  !=null
                            })
                            .channel(MAIN_EVENTS_CHANNEL)))
            .get();
}

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光匆匆的小流年 2025-01-22 01:14:13

首先,null 不是payload。因此,消息传递在大多数情况下不支持 null。集成流程仅在返回 null 时停止:https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#service-activator-namespace< /a>

服务激活器是不需要生成回复消息的组件之一。如果您的方法返回 null 或具有 void 返回类型,则服务激活器将在方法调用后退出,而不产生任何信号。

因此,您做出逻辑决定的假设对于 Spring Integration 来说是不正确的。您需要考虑什么可以用作此类 NO_OUTPUT_CHANNEL 的信号。您可以创建一个人工 NullType 并使用 PayloadTypeRouter 来根据 handleMailMessageException() 返回的有效负载类型确定下一步何时进行:

.<Object, Class<?>>route(Object::getClass, m -> m
                        .channelMapping(MailPojo.class, MAIN_EVENTS_CHANNEL)
                        .channelMapping(NullType.class, NO_OUTPUT_CHANNEL))

另一种方法就是使用一个Optional并在router函数中检查它的内容。无论哪种方式,您都需要使用路由器。

First of all the null is not a payload. Therefore messaging does not support null in most cases. The integration flow just stops at the point where you return null: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#service-activator-namespace

The service activator is one of those components that is not required to produce a reply message. If your method returns null or has a void return type, the service activator exits after the method invocation, without any signals.

So, your assumption to make a logical decision is not correct with Spring Integration. You need to think about something what could be used as a signal for such a NO_OUTPUT_CHANNEL. You can create an artificial NullType and use a PayloadTypeRouter to determine when to go next according the payload type returned from your handleMailMessageException():

.<Object, Class<?>>route(Object::getClass, m -> m
                        .channelMapping(MailPojo.class, MAIN_EVENTS_CHANNEL)
                        .channelMapping(NullType.class, NO_OUTPUT_CHANNEL))

Another way is to use an Optional and check its content in the router function. Either way you need to use a router.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文