Go to new doc!

+49 228 5552576-0


Tutorial: Build and deploy your own Interceptor using Maven and OSGi

Warning: This tutorial only works with Membrane ESB up to version 3.5.X. The 4.X branch still has some work left.

Lets assume you have just finished the previous tutorial, that explained how to extend ServiceMix with Membrane using a custom proxies.xml file.

For this tutorial, you need an installation of Eclipse, for example the Java EE edition.

1. Install the m2e Plugin

In Eclipse, select Help, Install Software from the menu. Choose a releases repository and install m2e - Maven Integration for Eclipse. Follow the dialog and restart Eclipse.

2. Create the New Project

Select File, New, Project... from the menu. Select Maven Project and Next. Then check Create simple project (skip archetype selection) and choose Next. Choose a Group Id and Archetype Id for your new project: In this example, we choose com.predic8 and com.predic8.membrane.tutorial.interceptor. Click Finish.

3. Add Dependencies and Build an OSGi Bundle

Replace the content of pom.xml with the following (except your groupId and artifactId in lines 6 and 7): (Use the left icon shown when hovering for Copy&Paste.)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">






Save this file, right-click the project and choose Maven, Update Project Configuration. (Note that some build errors will remain until you have completed the next step.)

4. Add your Interceptor Implementation

Add a class MyInterceptor in a package com.predic8.membrane.tutorial.interceptor with the following implementation:

package com.predic8.membrane.tutorial.interceptor;

import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.http.MimeType;
import com.predic8.membrane.core.http.Response;
import com.predic8.membrane.core.interceptor.AbstractInterceptor;
import com.predic8.membrane.core.interceptor.Outcome;

public class MyInterceptor extends AbstractInterceptor {
	public Outcome handleRequest(Exchange exc) throws Exception {
				body("This is an intercepted response.").
		return Outcome.ABORT;


In this class you can add your customizations.

Any previous build errors should now have been resolved.

5. Add the Proxies and Beans Configuration Files

In src/main/resources, create the folder META-INF/membrane and add the following two files in it:

First, create a file called proxies.xml:

<proxies xmlns="http://membrane-soa.org/schemas/proxies/v1/"
      	 xsi:schemaLocation="http://membrane-soa.org/schemas/proxies/v1/ http://membrane-soa.org/schemas/proxies/v1/proxies.xsd">
	<serviceProxy port="9001" >

	<serviceProxy port="2001" >
		<interceptor refid="myInterceptor"/>

Then, create a file called monitor-beans.xml:

<?xml version="1.0" encoding="UTF-8"?>

  Copyright 2009 predic8 GmbH, www.predic8.com Licensed under the Apache 
  License, Version 2.0 (the "License"); you may not use this file except in 
  compliance with the License. You may obtain a copy of the License at
  Unless required by applicable law or agreed to in writing, software distributed 
  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 
  OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
  the specific language governing permissions and limitations under the License. 

<spring:beans xmlns="http://membrane-soa.org/router/beans/1/"
	xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
					    http://membrane-soa.org/router/beans/1/ http:/www.predic8.com/schema/router/conf/router-conf.xsd">


		<ruleMatching />
		<dispatching />
		<userFeature />
		<httpClient />
	<spring:bean id="transformerFactory" class="com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl" />
	<spring:bean id="myInterceptor" class="com.predic8.membrane.tutorial.interceptor.MyInterceptor" />


6. Install the Bundle into Your Local Maven Repository

Right-click the project and choose Run As and Maven install. The Console view should activate.

Wait until BUILD SUCCESS appears.

7. Install your Bundle in Karaf

Go to the Karaf shell, install and activate your new bundle:

karaf@root> osgi:install -s mvn:com.predic8/com.predic8.membrane.tutorial.interceptor
karaf@root> _

8. The Result

Open http://localhost:2001/ in a browser and watch.

Also visit the admin console.