代码之家  ›  专栏  ›  技术社区  ›  James Kingsbery

springmbeanexporter:记录异常的stacktrace

  •  2
  • James Kingsbery  · 技术社区  · 14 年前

    我知道我可以试着用这个方法来做这件事,但那会导致混乱。我查了Spring文档( Chapter 22 是JMX上的那个),什么都没看到。我也没见过这么多人。我还查看了MBeanExporter的源代码,似乎有一种方法可以为MBean注册注册监听器,但不能为MBean请求处理注册监听器。

    2 回复  |  直到 14 年前
        1
  •  2
  •   blacelle    11 年前

    所以我研究了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;
                          }
                    }
              };
            }
    }
    
        2
  •  0
  •   skaffman    14 年前

    MBeanExporter @ManagedOperation @ManagedAttribute ,因为这些似乎是最常见的。

    如果有一个getter方法 @管理属性 ,而这个getter抛出一个异常,那么这个异常将不会被记录到任何地方,它只会被传播到客户端供它处理。这有时很烦人,但似乎没有办法将其配置为其他方式。

    但是,方法将捕获并记录其异常。我不知道为什么会有这种区别,但事实就是这样。