Netty HTTP Server Example
Skip to end of metadata
Go to start of metadata

Netty HTTP Server Example

Available as of Camel 2.12

This example is located in the examples/camel-example-netty-http directory of the Camel distribution.
There is a README.txt file with instructions how to run it.

If you use maven then you can easily package the example from the command line:

About

This example shows how to use a shared Netty HTTP Server in an OSGi environment.

There is 3 modules in this example

  • shared-netty-http-server - The Shared Netty HTTP server that the other Camel applications uses.
  • myapp-one - A Camel application that reuses the shared Netty HTTP server
  • myapp-two - A Camel application that reuses the shared Netty HTTP server

Implementation

In the shared-netty-http-server/src/main/resources/OSGI-INF/blueprint/http-server.xml file we have a OSGi Blueprint XML file that defines the shared Netty HTTP server we are using. First we need to configure the options on the shared Netty HTTP server which is done using the NettySharedHttpServerBootstrapConfiguration class in the configuration bean. In this example we use port 8888 as the shared port number.

Then we define the shared Netty HTTP server using the DefaultNettySharedHttpServer class in the httpServer bean.

And finally we need to enlist the shared Netty HTTP server in the OSGi Service Registry, so we can refer and use it from other bundles.

The Camel route

In the two Camel applications, each have a Camel route that uses the shared Netty HTTP server. The Camel application is defined in an OSGi blueprint file, for example from myapp-one its the myapp-one/src/main/resources/OSGI-INF/blueprint/camel-one.xml file.

First we need to refer to the shared Netty HTTP server which was enlisted in the OSGi service registry using the reference tag as shown below.

In the Camel route, we then use the nettySharedHttpServer option to use the shared server, with nettySharedHttpServer=#sharedNettyHttpServer.

Must use unique context-path in Camel routes

Icon

When using the nettySharedHttpServer option in Camel routes, then each context-path must be unique. For example in this example we have 2 Camel applications, where they use unique context-path names:

  • /one
  • /two

This is because the shared Netty HTTP server needs to know exactly which Camel application that should route the incoming message. And therefore the context-path must be unique among all the Camel routes.

Running the example

This example runs in Apache Karaf / ServiceMix container.

To install Apache Camel in Karaf you type in the shell (we use version 2.12.0):

First you need to install the following features in Karaf/ServiceMix with:

In the Apache Karaf / ServiceMix shell type:

Then you can install the Camel applications:

From a web browser you can then try the example by accessing the followign URLs:

See Also

Labels
  • No labels