Chapter 3. Determining When the Interceptor is Invoked

When developing a custom interceptor the first thing to consider is where in the message processing chain the interceptor belongs. The developer can control an interceptor's position in the message processing chain in two ways:

Typically the code specifying an interceptor's location is placed in the interceptor's constructor. This makes it possible for the runtime to instantiate the interceptor and put in the proper place without any explicit action in the application level code.

An interceptor's phase is set in the interceptor's constructor. The AbstractPhaseInterceptor class defines three constructors for instantiating an interceptor:

  • public AbstractPhaseInterceptor(String phase)—sets the phase of the interceptor to the specified phase and automatically sets the interceptor's id to the interceptor's class name.

    Tip

    This constructor will satisfy most use cases.

  • public AbstractPhaseInterceptor(String id, String phase)—sets the interceptor's id to the string passed in as the first parameter and the interceptor's phase to the second string.

  • public AbstractPhaseInterceptor(String phase, boolean uniqueId)—specifies if the interceptor should use a unique, system generated id. If the uniqueId parameter is true the interceptor's id will be calculated by the system. If the uniqueId parameter is true the interceptor's id is set to the interceptor's class name.

The recommended way to set a custom interceptor's phase is to pass the phase to the AbstractPhaseInterceptor constructor using the super() method as shown in Example 3.1, “Setting an Interceptor's Phase”.


The StreamInterceptor interceptor shown in Example 3.1, “Setting an Interceptor's Phase” is placed into the PRE_STREAM phase.