2010-09-03 141 views
2

在Spring MVC中,如果一切都返回到框架(例如,如果存在NullPointerException),则会记录一个异常的堆栈跟踪。有没有简单的方法来使用Spring的MBeanExporter来做到这一点?Spring MBeanExporter:记录一个异常的堆栈跟踪

我知道我可以在方法中尝试捕获这样做,但这会导致混乱。我检查了Spring文档(Chapter 22是JMX上的文档),但没有看到任何内容。我也没有看到任何东西。我还查看了MBeanExporter的源代码,并且似乎有一种方法可以注册MBean注册的监听器,但不能用于MBean请求处理。

回答

2

在我的基于Spring 3.1的应用程序中,@ManagedAttributes和@ManagedOperations都未被捕获或记录。 - 春天似乎默认情况下,出口的所有操作/属性

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但是,方法会捕获并记录它们的异常。我不知道为什么会这样做,但事实就是这样。

+0

我没有使用任何注释:

所以我通过扩展的MBeanExporter的,其失败每当MBean的方法调用失败去了。我会尝试在有问题的操作中添加一个注释,然后看看它做了什么,谢谢。 – 2010-09-07 19:04:42

+0

在我的应用程序中,@ManagedOperation方法没有被捕获和记录 – bla 2013-12-12 08:52:58