Go to new doc!

+49 228 5552576-0


info@predic8.com

Embedding Membrane Service Proxy into Java Applications

The Membrane Service Proxy API makes it easy to create, start and stop service proxies from your Java application. This page describes how to use the Membrane Service Proxy API in your own Java code.

You can find a fully working example in %MEMBRANE_HOME/examples/embedding-java.

Required Libraries

In order to use the Service Proxy API, you'll need to include the libraries from %MEMBRANE_HOME%/lib in your CLASSPATH or add it as a Maven dependency.

                    <dependencies>
                        <dependency>
                            <groupId>com.predic8</groupId>
                            <artifactId>membrane-esb-core</artifactId>
                            <version>4.0.2</version>
                        </dependency>
                    </dependencies>

                    <repositories>
                        <repository>
                            <id>predic8-releases</id>
                            <url>http://repository.membrane-soa.org/content/groups/public</url>
                        </repository>
                    </repositories>
Listing 1: Maven pom.xml excerpt

Starting a Service Proxy from your Java Application

In this example, the service proxy will forward any incoming HTTP GET requests on port 4000 to predic8.com on port 80. The regular expressions path and hostname can be used to forward only requests with matching HTTP paths or Host:-headers.

Note the call to init on the router instance which initializes the previously added Service Proxy instances.

                    import com.predic8.membrane.core.*;
                    import com.predic8.membrane.core.rules.*;

                    import java.io.IOException;

                    public class EmbeddingJava {
                        public static void main(String[] args) throws Exception {
                            String hostname = "*";
                            String method = "GET";
                            String path = ".*";
                            int listenPort = 4000;

                            ServiceProxyKey key = new ServiceProxyKey(hostname, method, path, listenPort);

                            String targetHost = "predic8.com";
                            int targetPort = 80;

                            ServiceProxy sp = new ServiceProxy(key, targetHost, targetPort);

                            HttpRouter router = new HttpRouter();
                            router.add(sp);
                            router.init();
                        }
                    }
                

Adding an existing Interceptor

Membrane already ships with a number of interceptors which you can plug into service proxies to customize their behaviour. The following lines add a GroovyInterceptor to the previously defined service proxy to print both request and response host headers on the console.

                    GroovyInterceptor gi = new GroovyInterceptor();
                    gi.setSrc("println \"host: ${exchange.getRequest().getHeader().getHost()}\"");
                    sp.getInterceptors().add(gi);
                

Adding a custom Interceptor

Besides adding existing interceptors Membrane also allows to use custom interceptors.

serviceProxy.getInterceptors().add(new AddMyHeaderInterceptor());
Implementing a custom Interceptor is straightforward. Extend your interceptor from AbstractInterceptor and override methods as needed. The following interceptor overrides handleRequest to add an X-Hello header to the request.
                    import com.predic8.membrane.core.exchange.*;
                    import com.predic8.membrane.core.interceptor.*;

                    public class AddMyHeaderInterceptor extends AbstractInterceptor {
                        private static final Logger log = Logger.getLogger(AddMyHeaderInterceptor.class);

                        @Override
                        public Outcome handleRequest(Exchange exchange) {
                            exchange.getRequest().getHeader().add("X-Hello", "Hello World!");

                            return Outcome.CONTINUE;
                        }
                    }