3

I define a custom interceptor used to log time, but it doesn't work. here is my custom interceptor code:

public class TimeConsumedInterceptor extends AbstractInterceptor
{

/*
 * {@inheritDoc}
 */
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
    long start = System.currentTimeMillis();
    String result = invocation.invoke();
    long end = System.currentTimeMillis();
    System.out.println("time consumed: " + (end - start));
    return result;
}

}

I define a new xml file: ecs-default.xml(it is directly under src package)

<struts>
<include file="struts-default.xml"></include>
<!-- ecs-default package is abstract -->
<package name="ecs-default" extends="struts-default" abstract="true">
    <interceptors>
        <interceptor name="ecsTimer" class="com.nader.interceptor.TimeConsumedInterceptor"></interceptor>

        <interceptor-stack name="ecsStack">
            <interceptor-ref name="ecsTimer"></interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
        </interceptor-stack>
    </interceptors>
           <!-- default stack used for ecs-default package -->
    <default-interceptor-ref name="ecsStack"></default-interceptor-ref>

    <global-results>
        <result name="error">error.jsp</result>
    </global-results>

    <global-exception-mappings>
        <exception-mapping exception="java.lang.Exception" result="error" />
    </global-exception-mappings>

</package>
</struts>

and struts.xml file:

<struts>
<include file="ecs-default.xml"></include>
  <!-- define two empty package, / and /manager, extends ecs-default package -->
<package name="default" namespace="/" extends="ecs-default">
</package>

<package name="manager" namespace="/manager" extends="ecs-default">
</package>

</struts>

I debug the code, in com.opensymphony.xwork2.DefaultActionInvocation class, List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(proxy.getConfig().getInterceptors());, proxy.getConfig().getInterceptors() returns the 18 interceptors of defaultStack defined in struts-default.xml, my ecsTimer interceptor is not in it. so why? Is something wrong in my configuration? Thanks.

Roman C
  • 49,761
  • 33
  • 66
  • 176
hiway
  • 3,906
  • 10
  • 34
  • 57

1 Answers1

2

The DefaultActionInvocation is being created for every action execution. You just need to execute your action which is configured with your custom interceptor to see it in debug.

BTW there is already timer interceptor that does what you want.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • I use struts annotation, I just do as you said, `@ParentPackage(value = "json-default") @Namespace(value = "/manager") @InterceptorRef(value="ecsStack") public class UserSearchAction extends ActionSupport`..., now it reports an error: Unable to find interceptor class referenced by ref-name ecsStack - [unknown location]. so how to solve it ? this interceptor class exists for sure. – hiway Mar 19 '13 at 10:38
  • I solve it, see http://stackoverflow.com/questions/8600148/how-to-link-custom-interceptor-using-annotation. I think struts2 annotation is more complicated than xml configuration. Thanks very much. – hiway Mar 19 '13 at 11:10