camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Netty
Date Thu, 06 May 2010 13:12:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/9/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="http://cwiki.apache.org/confluence/display/CAMEL/Netty">Netty</a></h2>
    <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (11)</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" >*Available as of Camel 2.3* <br>
<br></td></tr>
            <tr><td class="diff-changed-lines" >The *netty* component in Camel
is a socket communication component, based on the <span class="diff-added-words"style="background-color:
#dfd;">JBoss Netty community offering (available under an Apache 2.0 license).</span>
<br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">JBoss
Netty community offering (available under an Apache 2.0 license). Netty is a  <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color:
#dfd;">Netty is a</span> NIO client server framework which enables quick and easy
development of network applications <span class="diff-added-words"style="background-color:
#dfd;">such as protocol servers and clients.</span> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">such
as protocol servers and clients. Netty greatly simplifies and streamlines network  <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color:
#dfd;">Netty greatly simplifies and streamlines network</span> programming such as
TCP and UDP socket server. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>This camel component supports
both producer and consumer endpoints. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >The netty component has several
options and allows fine-grained control of a number of <span class="diff-added-words"style="background-color:
#dfd;">TCP/UDP communication parameters (buffer sizes, keepAlives, tcpNoDelay etc) and
facilitates both In-Only and In-Out communication on a Camel route.</span> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">TCP/UDP
communication parameters (buffer sizes, keepAlives, tcpNoDelay etc) and facilitates  <br>both
In-Only and In-Out communication on a Camel route. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Maven users will need to
add the following dependency to their {{pom.xml}} for this component: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >| {{receiveTimeoutMillis}} | {{10
seconds}} | Time to wait for a response to be received on a connection | <br>| {{reuseAddress}}
| {{true}} | Setting to facilitate socket multiplexing | <br></td></tr>
            <tr><td class="diff-changed-lines" >| {{sync}} | <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">{{false}}</span>
<span class="diff-added-words"style="background-color: #dfd;">{{true}}</span>
| Setting to set endpoint as one-way or request-response | <br></td></tr>
            <tr><td class="diff-unchanged" >| {{ssl}} | {{false}} | Setting to
specify whether SSL encryption is applied to this endpoint | <br>| {{sendBufferSize}}
| {{65536 bytes}} | The TCP/UDP buffer sizes to be used during outbound communication | <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >| {{maxPoolSize}} | {{100 threads}}
| The maximum number of threads that may be allocated to this endpoint. Defaults to 100 |
<br>| {{disconnect}} | {{false}} | Whether or not to disconnect(close) from Netty Channel
right after use. Can be used for both consumer and producer. | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
{{lazyChannelCreation}} | {{true}} | Channels can be lazily created to avoid exceptions, if
the remote server is not up and running when the Camel producer is started. | <br></td></tr>
            <tr><td class="diff-unchanged" >{div} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{snippet:id=routes|lang=xml|url=camel/trunk/components/camel-netty/src/test/resources/org/apache/camel/component/netty/multiple-codecs.xml}
<br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
Closing Channel When Complete <br>When acting as a server you sometimes want to close
the channel when, for example, a client conversion is finished. <br>You can do this
by simply setting the endpoint option {{disconnect=true}}. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">However
you can also instruct Camel on a per message basis as follows. <br>To instruct Camel
to close the channel, you should add a header with the key {{CamelNettyCloseChannelWhenComplete}}
set to a boolean {{true}} value. <br>For instance, the example below will close the
channel after it has written the bye message back to the client: <br>{code} <br>
       from(&quot;netty:tcp://localhost:8080&quot;).process(new Processor() { <br>
           public void process(Exchange exchange) throws Exception { <br>          
     String body = exchange.getIn().getBody(String.class); <br>                exchange.getOut().setBody(&quot;Bye
&quot; + body); <br>                // some condition which determines if we should
close <br>                if (close) { <br>                    exchange.getOut().setHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE,
true); <br>                } <br>            } <br>        }); <br>{code}
<br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" >{include:Endpoint See Also} <br>
- [Mina] <br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Netty-NettyComponent"></a>Netty Component</h2>

<p><b>Available as of Camel 2.3</b></p>

<p>The <b>netty</b> component in Camel is a socket communication component,
based on the JBoss Netty community offering (available under an Apache 2.0 license).<br/>
Netty is a NIO client server framework which enables quick and easy development of network
applications such as protocol servers and clients.<br/>
Netty greatly simplifies and streamlines network programming such as TCP and UDP socket server.</p>

<p>This camel component supports both producer and consumer endpoints.</p>

<p>The netty component has several options and allows fine-grained control of a number
of TCP/UDP communication parameters (buffer sizes, keepAlives, tcpNoDelay etc) and facilitates
both In-Only and In-Out communication on a Camel route.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-netty<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<h3><a name="Netty-URIformat"></a>URI format</h3>

<p>The URI scheme for a netty component is as follows</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
netty:tcp:<span class="code-comment">//localhost:99999[?options]
</span>netty:udp:<span class="code-comment">//remotehost:99999/[?options]</span>
</pre>
</div></div>

<p>This component supports producer and consumer endpoints for both TCP and UDP.</p>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="Netty-Options"></a>Options</h3>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>keepAlive</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Setting to ensure socket is not closed due to inactivity </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>tcpNoDelay</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Setting to improve TCP protocol performance </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>broadcast</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Setting to choose Multicast over UDP </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>connectTimeoutMillis</tt> </td>
<td class='confluenceTd'> <tt>10 seconds</tt> </td>
<td class='confluenceTd'> Time to wait for a socket connection to be available </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>receiveTimeoutMillis</tt> </td>
<td class='confluenceTd'> <tt>10 seconds</tt> </td>
<td class='confluenceTd'> Time to wait for a response to be received on a connection
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>reuseAddress</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Setting to facilitate socket multiplexing </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sync</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Setting to set endpoint as one-way or request-response </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ssl</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Setting to specify whether SSL encryption is applied to this
endpoint </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sendBufferSize</tt> </td>
<td class='confluenceTd'> <tt>65536 bytes</tt> </td>
<td class='confluenceTd'> The TCP/UDP buffer sizes to be used during outbound communication
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>receiveBufferSize</tt> </td>
<td class='confluenceTd'> <tt>65536 bytes</tt> </td>
<td class='confluenceTd'> The TCP/UDP buffer sizes to be used during inbound communication
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>corePoolSize</tt> </td>
<td class='confluenceTd'> <tt>10 threads</tt> </td>
<td class='confluenceTd'> The number of allocated threads at component startup. Defaults
to 10 </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxPoolSize</tt> </td>
<td class='confluenceTd'> <tt>100 threads</tt> </td>
<td class='confluenceTd'> The maximum number of threads that may be allocated to this
endpoint. Defaults to 100 </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>disconnect</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Whether or not to disconnect(close) from Netty Channel right
after use. Can be used for both consumer and producer. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>lazyChannelCreation</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Channels can be lazily created to avoid exceptions, if the
remote server is not up and running when the Camel producer is started. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Netty-RegistrybasedOptions"></a>Registry based Options</h3>

<p>Codec Handlers and SSL Keystores can be enlisted in the <a href="/confluence/display/CAMEL/Registry"
title="Registry">Registry</a>, such as in the Spring XML file.<br/>
The values that could be passed in, are the following:</p>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>passphrase</tt> </td>
<td class='confluenceTd'> password setting to use in order to encrypt/decrypt payloads
sent using SSH </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>keyStoreFormat</tt> </td>
<td class='confluenceTd'> keystore format to be used for payload encryption. Defaults
to "JKS" if not set </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>securityProvider</tt> </td>
<td class='confluenceTd'> Security provider to be used for payload encryption. Defaults
to "SunX509" if not set. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>keyStoreFile</tt> </td>
<td class='confluenceTd'> Client side certificate keystore to be used for encryption
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>trustStoreFile</tt> </td>
<td class='confluenceTd'> Server side certificate keystore to be used for encryption
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sslHandler</tt> </td>
<td class='confluenceTd'> Reference to a class that could be used to return an SSL Handler
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>encoder</tt> </td>
<td class='confluenceTd'> A custom Handler class that can be used to perform special
marshalling of outbound payloads. Must override <tt>org.jboss.netty.channel.ChannelDownStreamHandler</tt>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>encorders</tt> </td>
<td class='confluenceTd'> A list of encoder to be used. You can use a String which have
values separated by comma, and have the values be looked up in the <a href="/confluence/display/CAMEL/Registry"
title="Registry">Registry</a>. Just remember to prefix the value with # so Camel
knows it should lookup. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>decoder</tt> </td>
<td class='confluenceTd'> A custom Handler class that can be used to perform special
marshalling of inbound payloads. Must override <tt>org.jboss.netty.channel.ChannelUpStreamHandler</tt>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>decoders</tt> </td>
<td class='confluenceTd'> A list of decorder to be used. You can use a String which
have values separated by comma, and have the values be looked up in the <a href="/confluence/display/CAMEL/Registry"
title="Registry">Registry</a>. Just remember to prefix the value with # so Camel
knows it should lookup. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>handler</tt> </td>
<td class='confluenceTd'> A custom Handler class that can be used to perform custom
processing of Netty events triggered <br/>
during communication. Must override <tt>org.jboss.netty.channel.SimpleChannelHandler</tt>.
</td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Netty-SendingMessagesto%2FfromaNettyendpoint"></a>Sending Messages
to/from a Netty endpoint</h3>

<h4><a name="Netty-NettyProducer"></a>Netty Producer </h4>
<p>In Producer mode, the component provides the ability to send payloads to a socket
endpoint <br/>
using either TCP or UDP protocols (with optional SSL support).</p>

<p>The producer mode supports both one-way and request-response based operations.</p>

<h4><a name="Netty-NettyConsumer"></a>Netty Consumer</h4>
<p>In Consumer mode, the component provides the ability to:</p>
<ul class="alternate" type="square">
	<li>listen on a specified socket using either TCP or UDP protocols (with optional SSL
support),</li>
	<li>receive requests on the socket using text/xml, binary and serialized object based
payloads and</li>
	<li>send them along on a route as message exchanges.</li>
</ul>


<p>The consumer mode supports both one-way and request-response based operations.</p>

<h3><a name="Netty-UsageSamples"></a>Usage Samples</h3>

<h4><a name="Netty-AUDPNettyendpointusingRequestReplyandserializedobjectpayload"></a>A
UDP Netty endpoint using Request-Reply and serialized object payload</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
RouteBuilder builder = <span class="code-keyword">new</span> RouteBuilder() {
  <span class="code-keyword">public</span> void configure() {
    from(<span class="code-quote">"netty:udp:<span class="code-comment">//localhost:5155?sync=<span
class="code-keyword">true</span>"</span>)
</span>      .process(<span class="code-keyword">new</span> Processor()
{
         <span class="code-keyword">public</span> void process(Exchange exchange)
<span class="code-keyword">throws</span> Exception {
           Poetry poetry = (Poetry) exchange.getIn().getBody();
           poetry.setPoet(<span class="code-quote">"Dr. Sarojini Naidu"</span>);
           exchange.getOut().setBody(poetry);
         }
       }
    }
};
</pre>
</div></div>

<h4><a name="Netty-ATCPbasedNettyconsumerendpointusingOnewaycommunication"></a>A
TCP based Netty consumer endpoint using One-way communication</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
RouteBuilder builder = <span class="code-keyword">new</span> RouteBuilder() {
  <span class="code-keyword">public</span> void configure() {
       from(<span class="code-quote">"netty:tcp:<span class="code-comment">//localhost:5150"</span>)
</span>           .to(<span class="code-quote">"mock:result"</span>); 
  }
};
</pre>
</div></div>

<h4><a name="Netty-AnSSL%2FTCPbasedNettyconsumerendpointusingRequestReplycommunication"></a>An
SSL/TCP based Netty consumer endpoint using Request-Reply communication </h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
JndiRegistry registry = <span class="code-keyword">new</span> JndiRegistry(createJndiContext());
registry.bind(<span class="code-quote">"password"</span>, <span class="code-quote">"changeit"</span>);
registry.bind(<span class="code-quote">"ksf"</span>, <span class="code-keyword">new</span>
File(<span class="code-quote">"src/test/resources/keystore.jks"</span>));
registry.bind(<span class="code-quote">"tsf"</span>, <span class="code-keyword">new</span>
File(<span class="code-quote">"src/test/resources/keystore.jks"</span>));

context.createRegistry(registry);
context.addRoutes(<span class="code-keyword">new</span> RouteBuilder() {
  <span class="code-keyword">public</span> void configure() {
      <span class="code-object">String</span> netty_ssl_endpoint = 
         <span class="code-quote">"netty:tcp:<span class="code-comment">//localhost:5150sync=<span
class="code-keyword">true</span>&amp;ssl=<span class="code-keyword">true</span>&amp;passphrase=#password"</span>
</span>         + <span class="code-quote">"&amp;keyStoreFile=#ksf&amp;trustStoreFile=#tsf"</span>;
      <span class="code-object">String</span> return_string =
         <span class="code-quote">"When You Go Home, Tell Them Of Us And Say,"</span>
         + <span class="code-quote">"For Your Tomorrow, We Gave Our Today."</span>;
      
      from(netty_ssl_endpoint)
       .process(<span class="code-keyword">new</span> Processor() {
          <span class="code-keyword">public</span> void process(Exchange exchange)
<span class="code-keyword">throws</span> Exception {
            exchange.getOut().setBody(return_string);                           
          }
       }
  }
});
</pre>
</div></div>

<h4><a name="Netty-UsingMultipleCodecs"></a>Using Multiple Codecs</h4>

<p>In certain cases it may be necessary to add chains of encoders and decoders to the
netty pipeline. To add multpile codecs to a camel netty endpoint the 'encoders' and 'decoders'
uri parameters should be used. Like the 'encoder' and 'decoder' parameters they are used to
supply references (to lists of ChannelUpstreamHandlers and ChannelDownstreamHandlers) that
should be added to the  pipeline. Note that if encoders is specified then the encoder param
will be ignored, similarly for decoders and the decoder param.</p>

<p>The lists of codecs need to be added to the Camel's registry so they can be resolved
when the endpoint is created. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">LengthFieldBasedFrameDecoder lengthDecoder = <span class="code-keyword">new</span>
LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4);
StringDecoder stringDecoder = <span class="code-keyword">new</span> StringDecoder();
registry.bind(<span class="code-quote">"length-decoder"</span>, lengthDecoder);
registry.bind(<span class="code-quote">"string-decoder"</span>, stringDecoder);

LengthFieldPrepender lengthEncoder = <span class="code-keyword">new</span> LengthFieldPrepender(4);
StringEncoder stringEncoder = <span class="code-keyword">new</span> StringEncoder();
registry.bind(<span class="code-quote">"length-encoder"</span>, lengthEncoder);
registry.bind(<span class="code-quote">"string-encoder"</span>, stringEncoder);

List&lt;ChannelUpstreamHandler&gt; decoders = <span class="code-keyword">new</span>
ArrayList&lt;ChannelUpstreamHandler&gt;();
decoders.add(lengthDecoder);
decoders.add(stringDecoder);

List&lt;ChannelDownstreamHandler&gt; encoders = <span class="code-keyword">new</span>
ArrayList&lt;ChannelDownstreamHandler&gt;();
encoders.add(lengthEncoder);
encoders.add(stringEncoder);

registry.bind(<span class="code-quote">"encoders"</span>, encoders);
registry.bind(<span class="code-quote">"decoders"</span>, decoders);
</pre>
</div></div>

<p>Spring's native collections support can be used to specify the codec lists in an
application context</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">  <span class="code-tag">&lt;util:list id=<span class="code-quote">"decoders"</span>
list-class=<span class="code-quote">"java.util.LinkedList"</span>&gt;</span>
   
    <span class="code-tag">&lt;bean class=<span class="code-quote">"org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder"</span>&gt;</span>
      <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"1048576"</span>
/&gt;</span>
      <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"0"</span>
/&gt;</span>
      <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"4"</span>
/&gt;</span>
      <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"0"</span>
/&gt;</span>
      <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"4"</span>
/&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>
    <span class="code-tag">&lt;bean class=<span class="code-quote">"org.jboss.netty.handler.codec.string.StringDecoder"</span>
/&gt;</span>
  <span class="code-tag">&lt;/util:list&gt;</span>
  
  <span class="code-tag">&lt;util:list id=<span class="code-quote">"encoders"</span>
list-class=<span class="code-quote">"java.util.LinkedList"</span>&gt;</span>
    <span class="code-tag">&lt;bean class=<span class="code-quote">"org.jboss.netty.handler.codec.frame.LengthFieldPrepender"</span>&gt;</span>
      <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"4"</span>
/&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>
    <span class="code-tag">&lt;bean class=<span class="code-quote">"org.jboss.netty.handler.codec.string.StringEncoder"</span>
/&gt;</span>
  <span class="code-tag">&lt;/util:list&gt;</span>
  
  <span class="code-tag">&lt;bean id=<span class="code-quote">"length-encoder"</span>
class=<span class="code-quote">"org.jboss.netty.handler.codec.frame.LengthFieldPrepender"</span>&gt;</span>
      <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"4"</span>
/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"string-encoder"</span>
class=<span class="code-quote">"org.jboss.netty.handler.codec.string.StringEncoder"</span>
/&gt;</span>
  
  <span class="code-tag">&lt;bean id=<span class="code-quote">"length-decoder"</span>
class=<span class="code-quote">"org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder"</span>&gt;</span>
    <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"1048576"</span>
/&gt;</span>
    <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"0"</span>
/&gt;</span>
    <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"4"</span>
/&gt;</span>
    <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"0"</span>
/&gt;</span>
    <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"4"</span>
/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"string-decoder"</span>
class=<span class="code-quote">"org.jboss.netty.handler.codec.string.StringDecoder"</span>
/&gt;</span>
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>The bean names can then be used in netty endpoint definitions either as a comma separated
list or contained in a List e.g. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">                from(<span class="code-quote">"direct:multiple-codec"</span>).to(<span
class="code-quote">"netty:tcp:<span class="code-comment">//localhost:5150?encoders=#encoders"</span>);
</span>                from(<span class="code-quote">"netty:tcp:<span class="code-comment">//localhost:5150?decoders=#length-decoder,#string-decoder"</span>).to(<span
class="code-quote">"mock:multiple-codec"</span>);
</span>            }
        };
    }
}
</pre>
</div></div>

<p>or via spring.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext id=<span
class="code-quote">"multiple-netty-codecs-context"</span> xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:multiple-codec"</span>
/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"netty:tcp://localhost:5150?encoders=#encoders"</span>
/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"netty:tcp://localhost:5150?decoders=#length-decoder,#string-decoder"</span>
/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:multiple-codec"</span>
/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<h3><a name="Netty-ClosingChannelWhenComplete"></a>Closing Channel When
Complete</h3>
<p>When acting as a server you sometimes want to close the channel when, for example,
a client conversion is finished.<br/>
You can do this by simply setting the endpoint option <tt>disconnect=true</tt>.</p>

<p>However you can also instruct Camel on a per message basis as follows.<br/>
To instruct Camel to close the channel, you should add a header with the key <tt>CamelNettyCloseChannelWhenComplete</tt>
set to a boolean <tt>true</tt> value.<br/>
For instance, the example below will close the channel after it has written the bye message
back to the client:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
        from(<span class="code-quote">"netty:tcp:<span class="code-comment">//localhost:8080"</span>).process(<span
class="code-keyword">new</span> Processor() {
</span>            <span class="code-keyword">public</span> void process(Exchange
exchange) <span class="code-keyword">throws</span> Exception {
                <span class="code-object">String</span> body = exchange.getIn().getBody(<span
class="code-object">String</span>.class);
                exchange.getOut().setBody(<span class="code-quote">"Bye "</span>
+ body);
                <span class="code-comment">// some condition which determines <span
class="code-keyword">if</span> we should close
</span>                <span class="code-keyword">if</span> (close) {
                    exchange.getOut().setHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE,
<span class="code-keyword">true</span>);
                }
            }
        });
</pre>
</div></div>


<h3><a name="Netty-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/MINA" title="MINA">MINA</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="http://cwiki.apache.org/confluence/display/CAMEL/Netty">View Online</a>
        |
        <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=14814487&revisedVersion=26&originalVersion=25">View
Changes</a>
                |
        <a href="http://cwiki.apache.org/confluence/display/CAMEL/Netty?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message