java MDB 中处理异常的最佳方法是什么?

发布于 2024-10-14 21:03:07 字数 1408 浏览 3 评论 0原文

我得到了这个用例:

该图代表了一个企业模型。 Weblogic 10.3 上的 Java EE 技术,利用 Spring 框架实现 IoC 和 AOP,利用 Spring jpatemplate 实现 JPA 持久化,利用 Spring 集成交互框架。 正如您所看到的,服务和网关之间没有耦合,因为 Spring 集成添加了所需的所有魔糖。

现在我必须处理异常处理。所有链都没有检查异常:数据访问也没有检查异常,因为 jpatemplate 将所有 sql 异常包装在运行时异常中。

因此,我处理的唯一已检查异常是在 MDB 上,

@Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void onMessage(Message message) {
        try {
            TextMessage textMessage = (TextMessage) message;
            String stringMessage = textMessage.getText();

            OnlineEventMessage<? extends Serializable> event = eventMessageParser.parse(stringMessage);

            legacyEventMessageService.handle(event);
        } catch (JMSException e) {
            logger.error("si e' verificato un errore JMS nel processamento dell'evento {}", message, e);
        }
    }

我注意到,如果我在链的某个组件上收到 NPE,则消息将在 JMS 队列上回滚,并且进程将循环返回。

在这种情况下,处理异常的最佳方法是什么? 捕获 MDB 中的所有运行时异常?

亲切的问候 马西莫

I got this use case:

This diagram represents an enterprise model. Java EE technology on Weblogic 10.3 with the leverage of spring framework for IoC and AOP, JPA for persistence with spring jpatemplate, spring integration for the interaction frame.
As you can see there is no coupling between the Service and the Gateway since spring integration add all the magic sugar needed.

Now I have to deal with the exception handling. All the chain has no checked exceptions: also the data access has no checked exception since jpatemplate wraps all the sql exception in runtime exceptions.

So the only checked exception that I handle is on the MDB

@Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void onMessage(Message message) {
        try {
            TextMessage textMessage = (TextMessage) message;
            String stringMessage = textMessage.getText();

            OnlineEventMessage<? extends Serializable> event = eventMessageParser.parse(stringMessage);

            legacyEventMessageService.handle(event);
        } catch (JMSException e) {
            logger.error("si e' verificato un errore JMS nel processamento dell'evento {}", message, e);
        }
    }

I noticed that if I get a NPE for example on some component of the chain the message is rolled back on the JMS queue and the process is looped back.

Which is the best way to handle exceptions in this scenario?
Catch all the runtimeExceptions in the MDB?

Kind regards
Massimo

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

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

发布评论

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

评论(1

我不会写诗 2024-10-21 21:03:07

在这种情况下处理异常的最佳方法是什么?捕获 MDB 中的所有运行时异常?

这取决于您想要实现的目标。如果从您的描述中感觉您希望阻止消息被回滚。是这样吗?

在这种情况下,捕获所有运行时异常只能让您到目前为止。系统也可能会抛出错误,而您将无法捕获这些错误。所以你必须捕获 Throwable。但是,事务仍然可能超时,导致回滚。

简而言之,您希望 MDB 具有事务性吗?

另请注意,来自发送方的事务上下文不会传播到 MDB。

有点偏离主题,但您真的确定需要 jpatemplate 吗?似乎几乎每个人都同意 JPA API 本身就很好,不需要 Spring 的任何“增强”,包括 SpringSource 本身。

Which is the best way to handle exceptions in this scenario? Catch all the runtimeExceptions in the MDB?

It depends on what you want to achieve. If feel from your description that you want to prevent the message being rolled back. Is that right?

In that case catching all run time exceptions gets you only so far. The system could also throw errors, which you won't catch then. So you have to catch Throwable instead. But, then the transaction could still time out, resulting in a roll-back.

In short, do you want your MDB to be transactional at all?

Also note that the transaction context from the sender does not propagate to the MDB.

Slightly off topic, but are you really sure you need jpatemplate? It seems pretty much everybody has agreed that the JPA API is fine by itself and doesn't need any 'enhancements' from Spring, including SpringSource themselves.

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