/ Monitor / Documentation / Development / Message Interceptors - Membrane

Message Interceptors

requires version 1.3.0 or above

The architecture of the Membrane monitor and router is open for extension. To add functionality or to change the behavior, an interceptor can be plugged into the flow of messages. When a message flows through the router, the interceptors are called. An interceptor can:

During a synchronous exchange an interceptor is called twice. The router invokes the handleRequest method of the interceptor first and passes a reference to an exchange object to the interceptor. At this time the exchange object contains a request message only. Now the interceptor can access, read and modify the request. Than the handleRequest method either returns Outcome.CONTINUE or an Outcome.ABORT to indicate whether the processing of the exchange should be continued.

Message flow through an Interceptor
Figure 1: Message flow through an Interceptor

After the router has done it’s processing, the request is sent to the server. Then the server does its work and returns a response to the router. The router then invokes the interceptor again and passes the exchange reference to the handleResponse method. The exchange object is the same as the one passed to handle Request. But this time it contains a request and a response message.

Lifecycle

An interceptor is a singleton and therefore has only one instance. The instance is configured by a Spring configuration or by an API. If you need a per exchange lifecycle you can simulate this by using a Map like the following in your interceptor:

Map<Exchange, Object> session = new HashMap<Exchange, Object>();

Interceptor Interface

Listening 1 shows the interceptor interface.

    public interface Interceptor extends XMLElement {

	    public Outcome handleRequest(Exchange exc) throws Exception;
	
	    public Outcome handleResponse(Exchange exc) throws Exception;
	
	    public String getDisplayName();
	
	    public void setDisplayName(String name);
	
	    public String getId();
	
	    public void setId(String id);
}
Listing 1: Interceptor Interface
The methods handleRequest and handleResponse are called during the processing of an exchange. The property displayName is a human readable desciption that is displayed in the monitor GUI. The id is used to reference an interceptor from a rule.

Configuration

Interceptors are configuerd in the monitor-beans.xml or router-beans.xml file. You will find there some examples and comments about the configuration.

Included Interceptors

Membrane comes with a couple of interceptors.

Core functionality

Some interceptors are used by the router and monitor to provide core functionality.

Without these interceptors the monitor wouldn´t work.

Additional Functionality

These interceptors provide additional functionality to the monitor and router

Interceptor Chains

Transport Interceptors

Interceptors that are placed into a transport are invoked for all the messages that are flowing through that transport. See the transport bean in the monitor-beans.xml file.

Rule Interceptors

Interceptors that are placed into a rule are invoked when a message is matched by that rule. In the figure below there is a SimpleURLMappingInterceptor that manipulates the URI path of requests matching that rule.

Message Flow through Interceptors
Figure 2: Message Flow through Interceptors