camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Claus Ibsen (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > Netty HTTP Server Example
Date Wed, 26 Jun 2013 11:48:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Netty+HTTP+Server+Example">Netty
HTTP Server Example</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >&lt;/blueprint&gt; <br>{code}
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{note:title=Must
use unique context-path in Camel routes} <br>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: <br> <br>-
/one <br>- /two <br> <br>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. <br>{note} <br>
<br></td></tr>
            <tr><td class="diff-unchanged" >h3. Running the example <br>
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="NettyHTTPServerExample-NettyHTTPServerExample"></a>Netty
HTTP Server Example</h2>
<p><b>Available as of Camel 2.12</b></p>

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

<p>If you use maven then you can easily package the example from the command line:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
mvn package
</pre>
</div></div>

<h3><a name="NettyHTTPServerExample-About"></a>About</h3>

<p>This example shows how to use a shared <a href="/confluence/display/CAMEL/Netty+HTTP"
title="Netty HTTP">Netty HTTP</a> Server in an OSGi environment.</p>

<p>There is 3 modules in this example</p>

<ul>
	<li>shared-netty-http-server - The Shared Netty HTTP server that the other Camel applications
uses.</li>
	<li>myapp-one - A Camel application that reuses the shared Netty HTTP server</li>
	<li>myapp-two - A Camel application that reuses the shared Netty HTTP server</li>
</ul>


<h3><a name="NettyHTTPServerExample-Implementation"></a>Implementation</h3>

<p>In the <tt>shared-netty-http-server/src/main/resources/OSGI-INF/blueprint/http-server.xml</tt>
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 <tt>NettySharedHttpServerBootstrapConfiguration</tt> class in the configuration
bean. In this example we use port 8888 as the shared port number.</p>

<p>Then we define the shared <a href="/confluence/display/CAMEL/Netty+HTTP" title="Netty
HTTP">Netty HTTP</a> server using the <tt>DefaultNettySharedHttpServer</tt>
class in the httpServer bean.</p>

<p>And finally we need to enlist the shared <a href="/confluence/display/CAMEL/Netty+HTTP"
title="Netty HTTP">Netty HTTP</a> server in the OSGi Service Registry, so we can
refer and use it from other bundles.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
           http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"&gt;

  &lt;!-- netty http bootstrap configuration --&gt;
  &lt;bean id="configuration" class="org.apache.camel.component.netty.http.NettySharedHttpServerBootstrapConfiguration"&gt;
     &lt;!-- the port and host is mandatory and must be set --&gt;
    &lt;property name="port" value="8888"/&gt;
    &lt;property name="host" value="0.0.0.0"/&gt;
    &lt;!-- additional options --&gt;
    &lt;property name="backlog" value="50"/&gt;
  &lt;/bean&gt;

  &lt;!-- the netty http server --&gt;
  &lt;bean id="httpServer" class="org.apache.camel.component.netty.http.DefaultNettySharedHttpServer"
        init-method="start" destroy-method="stop"&gt;
    &lt;property name="nettyServerBootstrapConfiguration" ref="configuration"/&gt;
  &lt;/bean&gt;

  &lt;!-- export in the OSGi server registry so we can use it from the Camel application
bundles --&gt;
  &lt;service ref="httpServer" interface="org.apache.camel.component.netty.http.NettySharedHttpServer"/&gt;

&lt;/blueprint&gt;
</pre>
</div></div>

<h4><a name="NettyHTTPServerExample-TheCamelroute"></a>The Camel route</h4>
<p>In the two Camel applications, each have a Camel route that uses the shared <a
href="/confluence/display/CAMEL/Netty+HTTP" title="Netty HTTP">Netty HTTP</a> server.
The Camel application is defined in an OSGi blueprint file, for example from myapp-one its
the <tt>myapp-one/src/main/resources/OSGI-INF/blueprint/camel-one.xml</tt> file.</p>

<p>First we need to refer to the shared <a href="/confluence/display/CAMEL/Netty+HTTP"
title="Netty HTTP">Netty HTTP</a> server which was enlisted in the OSGi service registry
using the reference tag as shown below.</p>

<p>In the Camel route, we then use the <tt>nettySharedHttpServer</tt> option
to use the shared server, with <tt>nettySharedHttpServer=#sharedNettyHttpServer</tt>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
           http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"&gt;

  &lt;!-- reference the shared http server --&gt;
  &lt;reference id="sharedNettyHttpServer" interface="org.apache.camel.component.netty.http.NettySharedHttpServer"/&gt;

  &lt;!-- Camel application which uses the netty-http component and the shared Netty HTTP
server --&gt;
  &lt;camelContext xmlns="http://camel.apache.org/schema/blueprint"&gt;

    &lt;route id="http-route-one"&gt;
      &lt;from uri="netty-http:http://localhost/one?matchOnUriPrefix=true&amp;amp;nettySharedHttpServer=#sharedNettyHttpServer"/&gt;
      &lt;transform&gt;
        &lt;simple&gt;Response from Camel one using thread: ${threadName}&lt;/simple&gt;
      &lt;/transform&gt;
    &lt;/route&gt;

  &lt;/camelContext&gt;

&lt;/blueprint&gt;
</pre>
</div></div>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Must use unique context-path
in Camel routes</b><br />When using the <tt>nettySharedHttpServer</tt>
option in Camel routes, then each context-path must be <b>unique</b>. For example
in this example we have 2 Camel applications, where they use unique context-path names:

<ul class="alternate" type="square">
	<li>/one</li>
	<li>/two</li>
</ul>


<p>This is because the shared <a href="/confluence/display/CAMEL/Netty+HTTP" title="Netty
HTTP">Netty HTTP</a> 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.</p></td></tr></table></div>

<h3><a name="NettyHTTPServerExample-Runningtheexample"></a>Running the example</h3>

<p>This example runs in Apache Karaf / ServiceMix container.</p>

<p>To install Apache Camel in Karaf you type in the shell (we use version 2.12.0):</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
  features:chooseurl camel 2.12.0
  features:install camel
</pre>
</div></div>

<p>First you need to install the following features in Karaf/ServiceMix with:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
  features:install camel-netty-http
</pre>
</div></div>

<p>In the Apache Karaf / ServiceMix shell type:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
  osgi:install -s mvn:org.apache.camel/camel-example-netty-http-shared/2.12.0
</pre>
</div></div>

<p>Then you can install the Camel applications:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
  osgi:install -s mvn:org.apache.camel/camel-example-netty-myapp-one/2.12.0
  osgi:install -s mvn:org.apache.camel/camel-example-netty-myapp-two/2.12.0
</pre>
</div></div>

<p>From a web browser you can then try the example by accessing the followign URLs:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
  http://localhost:8888/one
  http://localhost:8888/two
</pre>
</div></div>


<h3><a name="NettyHTTPServerExample-SeeAlso"></a>See Also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a></li>
	<li><a href="/confluence/display/CAMEL/Netty+HTTP" title="Netty HTTP">Netty HTTP</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email notification preferences</a>
</div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Netty+HTTP+Server+Example">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=33292792&revisedVersion=4&originalVersion=3">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Netty+HTTP+Server+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message