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 10:40: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-unchanged" >h2. Netty HTTP Server Example <br>*Available
as of Camel 2.12* <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
<br>This example is located in the {{examples/camel-example-netty-http}} directory of
the Camel distribution. <br>There is a {{README.txt}} file with instructions how to
run it. <br> <br>If you use maven then you can easily package the example from
the command line: <br>{code} <br>mvn package <br>{code} <br> <br>h3.
About <br> <br>This example shows how to use a shared [Netty HTTP] Server in an
OSGi environment. <br> <br>There is 3 modules in this example <br> <br>*
shared-netty-http-server - The Shared Netty HTTP server that the other Camel applications
uses. <br>* myapp-one - A Camel application that reuses the shared Netty HTTP server
<br>* myapp-two - A Camel application that reuses the shared Netty HTTP server <br>
<br>h3. Implementation <br> <br>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. <br> <br>Then we define the
shared [Netty HTTP] server using the {{DefaultNettySharedHttpServer}} class in the httpServer
bean. <br> <br>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. <br>{code:xml}
<br>&lt;blueprint xmlns=&quot;http://www.osgi.org/xmlns/blueprint/v1.0.0&quot;
<br>           xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
<br>           xsi:schemaLocation=&quot; <br>           http://www.osgi.org/xmlns/blueprint/v1.0.0
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd&quot;&gt; <br> <br>
 &lt;!-- netty http bootstrap configuration --&gt; <br>  &lt;bean id=&quot;configuration&quot;
class=&quot;org.apache.camel.component.netty.http.NettySharedHttpServerBootstrapConfiguration&quot;&gt;
<br>     &lt;!-- the port and host is mandatory and must be set --&gt; <br>
   &lt;property name=&quot;port&quot; value=&quot;8888&quot;/&gt;
<br>    &lt;property name=&quot;host&quot; value=&quot;0.0.0.0&quot;/&gt;
<br>    &lt;!-- additional options --&gt; <br>    &lt;property name=&quot;backlog&quot;
value=&quot;50&quot;/&gt; <br>  &lt;/bean&gt; <br> <br>
 &lt;!-- the netty http server --&gt; <br>  &lt;bean id=&quot;httpServer&quot;
class=&quot;org.apache.camel.component.netty.http.DefaultNettySharedHttpServer&quot;
<br>        init-method=&quot;start&quot; destroy-method=&quot;stop&quot;&gt;
<br>    &lt;property name=&quot;nettyServerBootstrapConfiguration&quot;
ref=&quot;configuration&quot;/&gt; <br>  &lt;/bean&gt; <br>
<br>  &lt;!-- export in the OSGi server registry so we can use it from the Camel
application bundles --&gt; <br>  &lt;service ref=&quot;httpServer&quot;
interface=&quot;org.apache.camel.component.netty.http.NettySharedHttpServer&quot;/&gt;
<br> <br>&lt;/blueprint&gt; <br>{code} <br> <br>h4.
The Camel route <br>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. <br> <br>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. <br>
<br>In the Camel route, we then use the {{nettySharedHttpServer}} option to use the
shared server, with {{nettySharedHttpServer=#sharedNettyHttpServer}}. <br> <br>{code:xml}
<br>&lt;blueprint xmlns=&quot;http://www.osgi.org/xmlns/blueprint/v1.0.0&quot;
<br>           xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
<br>           xsi:schemaLocation=&quot; <br>           http://www.osgi.org/xmlns/blueprint/v1.0.0
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd&quot;&gt; <br> <br>
 &lt;!-- reference the shared http server --&gt; <br>  &lt;reference id=&quot;sharedNettyHttpServer&quot;
interface=&quot;org.apache.camel.component.netty.http.NettySharedHttpServer&quot;/&gt;
<br> <br>  &lt;!-- Camel application which uses the netty-http component and
the shared Netty HTTP server --&gt; <br>  &lt;camelContext xmlns=&quot;http://camel.apache.org/schema/blueprint&quot;&gt;
<br> <br>    &lt;route id=&quot;http-route-one&quot;&gt; <br>
     &lt;from uri=&quot;netty-http:http://localhost/one?matchOnUriPrefix=true&amp;amp;nettySharedHttpServer=#sharedNettyHttpServer&quot;/&gt;
<br>      &lt;transform&gt; <br>        &lt;simple&gt;Response
from Camel one using thread: ${threadName}&lt;/simple&gt; <br>      &lt;/transform&gt;
<br>    &lt;/route&gt; <br> <br>  &lt;/camelContext&gt;
<br> <br>&lt;/blueprint&gt; <br>{code} <br> <br>h3.
Running the example <br> <br>This example runs in Apache Karaf / ServiceMix container.
<br> <br>To install Apache Camel in Karaf you type in the shell (we use version
2.12.0): <br>{code} <br>  features:chooseurl camel 2.12.0 <br>  features:install
camel <br>{code} <br> <br>First you need to install the following features
in Karaf/ServiceMix with: <br>{code} <br>  features:install camel-netty-http <br>{code}
<br> <br>In the Apache Karaf / ServiceMix shell type: <br>{code} <br>
 osgi:install -s mvn:mvn:org.apache.camel/camel-example-netty-http-shared/2.12.0 <br>{code}
<br> <br>Then you can install the Camel applications: <br>{code} <br>
 osgi:install -s mvn:org.apache.camel/camel-example-netty-myapp-one/2.12.0 <br>  osgi:install
-s mvn:org.apache.camel/camel-example-netty-myapp-two/2.12.0 <br>{code} <br> <br>From
a web browser you can then try the example by accessing the followign URLs: <br>{code}
<br>  http://localhost:8888/one <br>  http://localhost:8888/two <br>{code}
<br> <br> <br>h3. See Also <br>- [Examples] <br>- [Netty HTTP]
<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>

<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: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=2&originalVersion=1">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