2010-09-03 90 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