Spring MBeanExporter:记录异常的堆栈跟踪

3
在Spring MVC中,如果异常一直传递到框架(例如出现NullPointerException),则会记录异常的堆栈跟踪。是否有使用Spring的MBeanExporter简单的方法来实现这一点?
我知道可以在方法中使用try-catch来完成这个功能,但这会导致代码混乱。我查看了Spring文档(第22章是关于JMX的),但没有找到相关内容。我也没有在SO上找到任何信息。我还查看了MBeanExporter的源代码,似乎有一种方法可以为MBean注册添加监听器,但不能为MBean请求处理添加。
2个回答

2
在我的基于Spring 3.1的应用程序中,@ManagedAttributes和@ManagedOperations都没有被捕获或记录。
因此,我通过对MBeanExporter进行扩展来解决这个问题,当MBean方法调用失败时,它将失败。
public class LoggingFailedCallsMBeanExporter extends MBeanExporter {

    protected ModelMBean createModelMBean() throws MBeanException {
        // super method does:
        // return (this.exposeManagedResourceClassLoader ? new SpringModelMBean() : new RequiredModelMBean());
        ModelMBean superModelMBean = super.createModelMBean();

        // but this.exposeManagedResourceClassLoader is not visible, so we switch on the type of the returned ModelMBean
        if (superModelMBean instanceof SpringModelMBean) {
              return new SpringModelMBean() {
                    @Override
                    public Object invoke(String opName, Object[] opArgs, String[] sig) throws MBeanException, ReflectionException {
                          try {
                                return super.invoke(opName, opArgs, sig);
                          } catch (MBeanException e) {
                                LOGGER.warn("Issue on a remote call", e);
                                throw e;
                          } catch (ReflectionException e) {
                                LOGGER.warn("Issue on a remote call", e);
                                throw e;
                          } catch (RuntimeException e) {
                                LOGGER.warn("Issue on a remote call", e);
                                throw e;
                          } catch (Error e) {
                                LOGGER.warn("Issue on a remote call", e);
                                throw e;
                          }
                    }
              };
        } else {
            return new RequiredModelMBean() {
                @Override
                public Object invoke(String opName, Object[] opArgs, String[] sig) throws MBeanException, ReflectionException {
                      try {
                            return super.invoke(opName, opArgs, sig);
                      } catch (MBeanException e) {
                            LOGGER.warn("Issue on a remote call", e);
                            throw e;
                      } catch (ReflectionException e) {
                            LOGGER.warn("Issue on a remote call", e);
                            throw e;
                      } catch (RuntimeException e) {
                            LOGGER.warn("Issue on a remote call", e);
                            throw e;
                      } catch (Error e) {
                            LOGGER.warn("Issue on a remote call", e);
                            throw e;
                      }
                }
          };
        }
}

0

MBeanExporter 是一个非常灵活的东西,可以处理许多不同的情况。由于您没有展示任何示例代码,我会假设您正在使用注释 @ManagedOperation@ManagedAttribute,因为这些似乎是最常见的。

如果您有一个带有 @ManagedAttribute 注释的 getter 方法,并且此 getter 抛出异常,则不会在任何地方记录此异常,它只会传播到客户端进行处理。这有时很烦人,但似乎没有其他配置方法。

然而,@ManagedOperation 方法将捕获并记录它们的异常。我不知道为什么会有这个区别,但事实就是这样。


我没有使用任何注解 - Spring似乎默认导出所有的操作/属性。我会尝试在相关的操作上添加一个注解,看看会发生什么,谢谢。 - James Kingsbery
3
在我的应用程序中,@ManagedOperation 方法未被捕获和记录。 - blacelle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接