我如何在春季集成流中处理旁观的消息

发布于 2025-02-12 20:44:08 字数 1231 浏览 1 评论 0原文

我在春季集成方面遇到了一些问题。

我有一个错误的频道,该通道应该消耗一个错误的队列,以防其他流量有错误,该处理程序允许我们以非常特定的格式记录一些重要数据,然后丢弃该消息。

问题是,该流程被配置为接收特定的消息类型,例如失败的Message类。

每当消耗消息时,我都会得到一个类铸造例外,说[b不能被铸造为失败

因此,在进行了一些研究之后,我实施了以下变压器:

    private FailedMessage parseFailedMessage(byte[] genericMessage){
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(genericMessage);
            ObjectInputStream is = new ObjectInputStream(in);
            return (FailedMessage) is.readObject();
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException("An unexpected error occurred parsing FailedMessage", e);
        }
    }

IntegrationFlow Spec是:

    @Bean
    public IntegrationFlow errorHandlingFlow(XmlMessageTransformer transformer) {
        return IntegrationFlows.from("ErrorChannel")
                .transform(this::parseFailedMessage)
                .<FailedMessage>handle((p, h) -> {
                    processFailedMessage(p, transformer);
                    return p;
                })
                .channel("discard")
                .get();
    }

这是处理此类消息的可接受方法,还是有一种方法可以自动化转换步骤?

I'm having some issues with Spring Integration.

I have an error channel that is supposed to consume an error dedicated queue in case other flows have an error, this handler allows us to log some important data in a very specific format, and then the message is discarded.

The problem is, that this flow is configured to receive a specific message type, like a FailedMessage class.

Whenever the message is consumed, I get a class cast exception saying that [B cannot be cast to FailedMessage.

So, after doing some research I implemented the following transformer:

    private FailedMessage parseFailedMessage(byte[] genericMessage){
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(genericMessage);
            ObjectInputStream is = new ObjectInputStream(in);
            return (FailedMessage) is.readObject();
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException("An unexpected error occurred parsing FailedMessage", e);
        }
    }

And the IntegrationFlow spec is :

    @Bean
    public IntegrationFlow errorHandlingFlow(XmlMessageTransformer transformer) {
        return IntegrationFlows.from("ErrorChannel")
                .transform(this::parseFailedMessage)
                .<FailedMessage>handle((p, h) -> {
                    processFailedMessage(p, transformer);
                    return p;
                })
                .channel("discard")
                .get();
    }

Is this an acceptable way to handle this kind of message or there's a way to automatize the transform step?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文