Struts 2ブロッキング実行原理

2581 ワード

1.サーバに要求を送信すると、struts 2は責任チェーンモードで複数のブロッカーを接続して1つずつ実行し、次にターゲットアクションを実行し、ブロッカーコードを逆方向に実行し、結果をインタフェースに戻す
struts 2実装コードの説明:

DefaultActionInvocation.java

public String invoke() throws Exception {
  String profileKey = "invoke: ";
  try {
    UtilTimerStack.push(profileKey);
    if (executed) {
      throw new IllegalStateException("Action has already executed");
     }
     //interceptors           ,            
     if (interceptors.hasNext()) {
       final InterceptorMapping interceptor = 
         (InterceptorMapping) interceptors.next();
        String interceptorMsg = "interceptor: " + interceptor.getName();
        UtilTimerStack.push(interceptorMsg);
        try {
          resultCode = interceptor.getInterceptor()
                         .intercept(DefaultActionInvocation.this);
        }
          finally
        {
           UtilTimerStack.pop(interceptorMsg);
        }
       }
         //           ,   Action(  ) 
         else 
       {
           resultCode = invokeActionOnly();
        }

            // this is needed because the result will be executed, then control will return to the Interceptor, which will
            // return above and flow through again
            if (!executed) {
                if (preResultListeners != null) {
                    for (Object preResultListener : preResultListeners) {
                        PreResultListener listener = (PreResultListener) preResultListener;

                        String _profileKey = "preResultListener: ";
                        try {
                            UtilTimerStack.push(_profileKey);
                            listener.beforeResult(this, resultCode);
                        }
                        finally {
                            UtilTimerStack.pop(_profileKey);
                        }
                    }
                }

                // now execute the result, if we're supposed to
                if (proxy.getExecuteResult()) {
                    executeResult();
                }

                executed = true;
            }

            return resultCode;
        }
        finally {
            UtilTimerStack.pop(profileKey);
        }
    }