2012-03-02 20 views
1

JSFアプリケーションでのすべてのアクションの実行時間を測定する方法を探しています。その後JSFでアクションの実行時間を測定する方法は?

public class MyActionListener extends ActionListenerImpl { 

      public void processAction(ActionEvent event) throws FacesException { 
        watch.start(); 
        super.processAction(event); 
        watch.stop(); 
      } 
} 

私がこれまでに見つかったハックソリューションは、com.sun.faces.application.ActionListenerImpl延長のprocessActionを上書きし、そして私の実装でsuper.processActionを呼び出すことです私は自分のActionListener実装をfacesに追加します。

<application> 
     <action-listener>MyActionListener</action-listener> 
</application 

しかし、これはjsf-implに依存しますハッキーです。より良い解決策はありますか?

答えて

0

ベストプラクティスのソリューションは、ServletContextListenerを実装し、contextInitializedを実装し、現在のデフォルトアクションリスナーのリフレクションプロキシを作成し、プロキシの呼び出しハンドラで時間を測定することでした。

@Override 
    public void contextInitialized(ServletContextEvent sce) { 
     // get JSF application factory 
     ApplicationFactory applicationFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); 
     Application application = applicationFactory.getApplication(); 
     ActionListener defaultActionListener = application.getActionListener(); 

     // create proxy for the default actionlistener 
     ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 
     ActionListenerInvocationHandler actionListenerInvocationHandler = new ActionListenerInvocationHandler(defaultActionListener); 
     @SuppressWarnings("rawtypes") 
     Class[] interfaces = new Class[] { ActionListener.class }; 
     ActionListener actionListenerProxy = (ActionListener) Proxy.newProxyInstance(contextClassLoader, interfaces, actionListenerInvocationHandler); 

     // set proxied actionListener as new default actionlistener 
     application.setActionListener(actionListenerProxy); 

    } 
2

代わりにPhaseListenerを使用し、PhaseId.INVOKE_APPLICATIONにフックすることができます。

代わりに<phase-listener>として登録してください。

あなたのコードは擬似であることを理解していますが、完全性のために、リスナーの全く同じインスタンスがすべてのスレッド/要求で共有されていることを認識する必要があります。あなたはむしろwatchを要求マップに格納したいし、間違いなくインスタンス変数として格納したくなるでしょう。

関連する問題