如何对 EJB 客户端隐藏 RuntimeException 详细信息?

发布于 2024-07-25 06:55:48 字数 406 浏览 3 评论 0原文

我有一个 JEE5 应用程序,它使用(本地)会话 bean 公开服务。

当服务执行期间发生内部错误时,JBoss(5.0.1) 将抛出 RuntimeException 并将其封装在 javax.ejb.EJBTransactionRolledbackException 中。

问题是接收此 EJBTransactionRolledbackException 的客户端应用程序可以访问有关导致运行时异常的详细信息,从而暴露我的应用程序的内部体系结构。 我不想要这样。

相反,我希望 JBoss 始终将公开的会话 bean 抛出的 RuntimeException 封装到单个(简单的)TechnicalException(没有原因)中。

实现这一目标的最佳方法是什么? (使用拦截器?使用 JBoss 配置?)

I have a JEE5 application that exposes services using (local) session beans.

When an internal fault occurs during service execution, a RuntimeException is thrown and encapsulated by JBoss(5.0.1) in a javax.ejb.EJBTransactionRolledbackException.

The problem is that client applications receiving this EJBTransactionRolledbackException can access detailled informations about the cause runtime exception, exposing internal architecture of my application. And I don't want that.

Instead, I would like JBoss to always encapsulate RuntimeException thrown by exposed session beans into a single (and simple) TechnicalException (with no cause).

What's the best way to achieve this ? (Using interceptors ? Using JBoss configuration ?)

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

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

发布评论

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

评论(2

满意归宿 2024-08-01 06:55:48

最后,根据之前的回答和我个人的研究,我保留了以下解决方案。

我创建了一个专门用于管理服务器故障的拦截器:

public class FaultBarrierInterceptor {

@AroundInvoke
public Object intercept(final InvocationContext invocationContext) throws Exception {
    try {
        return invocationContext.proceed();
    } catch (final RuntimeException e) {
        final Logger logger = Logger.getLogger(invocationContext.getMethod().getDeclaringClass());
        logger.error("A fault occured during service invocation:" +
                "\n-METHOD: " + invocationContext.getMethod() +
                "\n-PARAMS: " + Arrays.toString(invocationContext.getParameters()), e);
        throw new TechnicalException();
    }
}}

抛出的技术异常扩展了 EJBException 并且不公开原因 RuntimeException:

public class TechnicalException extends EJBException {}

我在所有公共服务中使用此拦截器:

@Stateless
@Interceptors({FaultBarrierInterceptor.class})
public class ShoppingCardServicesBean implements ShoppingCardServices { ...

这是 故障屏障模式

任何运行时异常都会被捕获、记录,并使用 TechnicalException 向客户端发出故障信号(没有内部详细信息)。 检查的异常将被忽略。

RuntimeException 处理是集中的,并且与任何业务方法分离。

Finally, based on previous answer and my personal researches, I retained the folowing solution.

I've created an interceptor dedicated to manage server faults :

public class FaultBarrierInterceptor {

@AroundInvoke
public Object intercept(final InvocationContext invocationContext) throws Exception {
    try {
        return invocationContext.proceed();
    } catch (final RuntimeException e) {
        final Logger logger = Logger.getLogger(invocationContext.getMethod().getDeclaringClass());
        logger.error("A fault occured during service invocation:" +
                "\n-METHOD: " + invocationContext.getMethod() +
                "\n-PARAMS: " + Arrays.toString(invocationContext.getParameters()), e);
        throw new TechnicalException();
    }
}}

The thrown technical exception extends EJBException and does not expose the cause RuntimeException:

public class TechnicalException extends EJBException {}

I use this interceptor in all public services :

@Stateless
@Interceptors({FaultBarrierInterceptor.class})
public class ShoppingCardServicesBean implements ShoppingCardServices { ...

This is an implementation of the Fault Barrier pattern.

Any runtime exception is catched, logged and a fault is signaled to the client (without internal details) using a TechnicalException. Checked exceptions are ignored.

RuntimeException handling is centralized and separated from any business methods.

烟柳画桥 2024-08-01 06:55:48

任何 RuntimeException 都会扩展 java.lang.Exception。

EJB 规范提供了对 2 种类型的异常(应用程序和系统)的处理。

如果您想抛出系统异常,通常会这样做:

try {
.... your code ...
}catch(YourApplicationException ae) {
   throw ae;
}catch(Exception e) {
   throw new EJBException(e); //here's where you need to change.
}

要隐藏系统异常的内部详细信息,只需将: 替换

throw new EJBException(e); 

为:

throw new EJBException(new TechnicalException("Technical Fault"));

Hope这就是您正在寻找的。

干杯

Any RuntimeException extends java.lang.Exception.

The EJB spec provides handling for 2 types of exceptions (Application and System)

If you'd like to throw a System Exception, you would usually do it like so:

try {
.... your code ...
}catch(YourApplicationException ae) {
   throw ae;
}catch(Exception e) {
   throw new EJBException(e); //here's where you need to change.
}

To hide the internal details of your system exception, simply replace:

throw new EJBException(e); 

with:

throw new EJBException(new TechnicalException("Technical Fault"));

Hope this is what you were looking for.

Cheers

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