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 Mon, 17 Dec 2012 14:58:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/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="https://cwiki.apache.org/confluence/display/CAMEL/Netty">Netty</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" >| {{keepAlive}} | {{true}} | Setting to ensure socket is not closed due to inactivity | <br>| {{tcpNoDelay}} | {{true}} | Setting to improve TCP protocol performance | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">| {{backlog}} | | *Camel 2.9.6/2.10.4/2.11:* Allows to configure a backlog for netty consumer (server). Note the backlog is just a best effort depending on the OS. Setting this option to a value such as {{200}}, {{500}} or {{1000}}, tells the TCP stack how long the &quot;accept&quot; queue can be. If this option is not configured, then the backlog depends on OS setting. | <br></td></tr>
            <tr><td class="diff-unchanged" >| {{broadcast}} | {{false}} | Setting to choose Multicast over UDP | <br>| {{connectTimeout}} | {{10000}} | Time to wait for a socket connection to be available. Value is in millis. | <br></td></tr>
            <tr><td class="diff-snipped" >...<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 <a href="http://netty.io/" class="external-link" rel="nofollow">Netty</a> project.<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>backlog</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <b>Camel 2.9.6/2.10.4/2.11:</b> Allows to configure a backlog for netty consumer (server). Note the backlog is just a best effort depending on the OS. Setting this option to a value such as <tt>200</tt>, <tt>500</tt> or <tt>1000</tt>, tells the TCP stack how long the "accept" queue can be. If this option is not configured, then the backlog depends on OS setting. </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>connectTimeout</tt> </td>
<td class='confluenceTd'> <tt>10000</tt> </td>
<td class='confluenceTd'> Time to wait for a socket connection to be available. Value is in millis. </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>synchronous</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Whether <a href="/confluence/display/CAMEL/Asynchronous+Routing+Engine" title="Asynchronous Routing Engine">Asynchronous Routing Engine</a> is not in use. <tt>false</tt> then the <a href="/confluence/display/CAMEL/Asynchronous+Routing+Engine" title="Asynchronous Routing Engine">Asynchronous Routing Engine</a> is used, <tt>true</tt> to force processing synchronous. </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. Size is bytes. </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. Size is bytes. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>corePoolSize</tt> </td>
<td class='confluenceTd'> <tt>10</tt> </td>
<td class='confluenceTd'> The number of allocated threads at component startup. Defaults to 10. <b>Note:</b> This option is removed from Camel 2.9.2 onwards. As we rely on Nettys default settings. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxPoolSize</tt> </td>
<td class='confluenceTd'> <tt>100</tt> </td>
<td class='confluenceTd'> The maximum number of threads that may be allocated to this endpoint. Defaults to 100. <b>Note:</b> This option is removed from Camel 2.9.2 onwards. As we rely on Nettys default settings. </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>
<tr>
<td class='confluenceTd'> <tt>transferExchange</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Only used for TCP. You can transfer the exchange over the wire instead of just the body. The following fields are transferred: In body, Out body, fault body, In headers, Out headers, fault headers, exchange properties, exchange exception. This requires that the objects are serializable. Camel will exclude any non-serializable objects and log it at WARN level. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>disconnectOnNoReply</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> If sync is enabled then this option dictates NettyConsumer if it should disconnect where there is no reply to send back. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>noReplyLogLevel</tt> </td>
<td class='confluenceTd'> <tt>WARN</tt> </td>
<td class='confluenceTd'> If sync is enabled this option dictates NettyConsumer which logging level to use when logging a there is no reply to send back. Values are: <tt>FATAL, ERROR, INFO, DEBUG, OFF</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>allowDefaultCodec</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> The netty component installs a default codec if both, encoder/deocder is null and textline is false. Setting allowDefaultCodec to false prevents the netty component from installing a default codec as the first element in the filter chain. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>textline</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> Only used for TCP. If no codec is specified, you can use this flag to indicate a text line based codec; if not specified or the value is false, then Object Serialization is assumed over TCP. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>delimiter</tt> </td>
<td class='confluenceTd'> <tt>LINE</tt> </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> The delimiter to use for the textline codec. Possible values are <tt>LINE</tt> and <tt>NULL</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>decoderMaxLineLength</tt> </td>
<td class='confluenceTd'> <tt>1024</tt> </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> The max line length to use for the textline codec. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>autoAppendDelimiter</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> Whether or not to auto append missing end delimiter when sending using the textline codec. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>encoding</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> The encoding (a charset name) to use for the textline codec. If not provided, Camel will use the JVM default Charset. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>workerCount</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> When netty works on nio mode, it uses default workerCount parameter from Netty, which is cpu_core_threads*2. User can use this operation to override the default workerCount from Netty </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sslContextParametersRef</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> Reference to a <tt>org.apache.camel.util.jsse.SSLContextParameters</tt> in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.&nbsp; This reference overrides any configured SSLContextParameters at the component level.&nbsp; See <a href="#Netty-UsingtheJSSEConfigurationUtility">Using the JSSE Configuration Utility</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>receiveBufferSizePredictor</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> Configures the buffer size predictor. See details at Jetty documentation and this <a href="http://lists.jboss.org/pipermail/netty-users/2010-January/001958.html" class="external-link" rel="nofollow">mail thread</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>needClientAuth</tt> </td>
<td class='confluenceTd'><tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.11:</b> Configures whether the server needs client authentication when using SSL. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>orderedThreadPoolExecutor</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10.2:</b> Whether to use ordered thread pool, to ensure events are processed orderly on the same channel. See details at the netty javadoc of <tt>org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor</tt> for more details. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maximumPoolSize</tt> </td>
<td class='confluenceTd'> <tt>16</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10.2:</b> The core pool size for the ordered thread pool, if its in use. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>producerPoolMaxActive</tt> </td>
<td class='confluenceTd'> <tt>-1</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10.3:</b> Producer only. Sets the cap on the number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time. Use a negative value for no limit. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>producerPoolMinIdle</tt> </td>
<td class='confluenceTd'> <tt>0</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10.3:</b> Producer only. Sets the minimum number of instances allowed in the producer pool before the evictor thread (if active) spawns new objects. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>producerPoolMaxIdle</tt> </td>
<td class='confluenceTd'> <tt>100</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10.3:</b> Producer only. Sets the cap on the number of "idle" instances in the pool. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>producerPoolMinEvictableIdle</tt> </td>
<td class='confluenceTd'> <tt>30000</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10.3:</b> Producer only. Sets the minimum amount of time (value in millis) an object may sit idle in the pool before it is eligible for eviction by the idle object evictor. </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 <tt>ChannelHandler</tt> 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 encoders 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 <tt>ChannelHandler</tt> 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 decoders 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>
</tbody></table>
</div>
</div>

<p><b>Important:</b> Read below about using non shareable encoders/decoders.</p>

<h4><a name="Netty-Usingnonshareableencodersordecoders"></a>Using non shareable encoders or decoders</h4>

<p>If your encoders or decoders is not shareable (eg they have the @Shareable class annotation), then your encoder/decoder must implement the <tt>org.apache.camel.component.netty.ChannelHandlerFactory</tt> interface, and return a new instance in the <tt>newChannelHandler</tt> method. This is to ensure the encoder/decoder can safely be used. If this is not the case, then the Netty component will log a WARN when<br/>
an endpoint is created.</p>

<p>The Netty component offers a <tt>org.apache.camel.component.netty.ChannelHandlerFactories</tt> factory class, that has a number of commonly used methods.</p>

<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>

<h5><a name="Netty-UsingtheJSSEConfigurationUtility"></a>Using the JSSE Configuration Utility</h5>

<p>As of Camel 2.9, the Netty component supports SSL/TLS configuration through the <a href="/confluence/display/CAMEL/Camel+Configuration+Utilities" title="Camel Configuration Utilities">Camel JSSE Configuration Utility</a>.&nbsp;  This utility greatly decreases the amount of component specific code  you need to write and is configurable at the endpoint and component  levels.&nbsp; The following examples demonstrate how to use the utility with  the Netty component.</p>

<h6><a name="Netty-Programmaticconfigurationofthecomponent"></a>Programmatic configuration of the component</h6>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
KeyStoreParameters ksp = <span class="code-keyword">new</span> KeyStoreParameters();
ksp.setResource(<span class="code-quote">"/users/home/server/keystore.jks"</span>);
ksp.setPassword(<span class="code-quote">"keystorePassword"</span>);

KeyManagersParameters kmp = <span class="code-keyword">new</span> KeyManagersParameters();
kmp.setKeyStore(ksp);
kmp.setKeyPassword(<span class="code-quote">"keyPassword"</span>);

SSLContextParameters scp = <span class="code-keyword">new</span> SSLContextParameters();
scp.setKeyManagers(kmp);

NettyComponent nettyComponent = getContext().getComponent(<span class="code-quote">"netty"</span>, NettyComponent.class);
nettyComponent.setSslContextParameters(scp);
</pre>
</div></div>

<h6><a name="Netty-SpringDSLbasedconfigurationofendpoint"></a>Spring DSL based configuration of endpoint</h6>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
...
  &lt;camel:sslContextParameters
      id=<span class="code-quote">"sslContextParameters"</span>&gt;
    &lt;camel:keyManagers
        keyPassword=<span class="code-quote">"keyPassword"</span>&gt;
      &lt;camel:keyStore
          resource=<span class="code-quote">"/users/home/server/keystore.jks"</span>
          password=<span class="code-quote">"keystorePassword"</span>/&gt;
    <span class="code-tag">&lt;/camel:keyManagers&gt;</span>
  <span class="code-tag">&lt;/camel:sslContextParameters&gt;</span>...
...
  <span class="code-tag">&lt;to uri=<span class="code-quote">"netty:tcp://localhost:5150?sync=true&amp;ssl=true&amp;sslContextParameters=#sslContextParameters"</span>/&gt;</span>
...
</pre>
</div></div>

<h5><a name="Netty-UsingBasicSSL%2FTLSconfigurationontheJettyComponent"></a>Using Basic SSL/TLS configuration on the Jetty Component</h5>

<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:5150?sync=<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>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>Read further above about using non shareable encoders/decoders.</td></tr></table></div>

<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">ChannelHandlerFactory lengthDecoder = ChannelHandlerFactories.newLengthFieldBasedFrameDecoder(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;ChannelHandler&gt; decoders = <span class="code-keyword">new</span> ArrayList&lt;ChannelHandler&gt;();
decoders.add(lengthDecoder);
decoders.add(stringDecoder);

List&lt;ChannelHandler&gt; encoders = <span class="code-keyword">new</span> ArrayList&lt;ChannelHandler&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.apache.camel.component.netty.ChannelHandlerFactories"</span> factory-method=<span class="code-quote">"newLengthFieldBasedFrameDecoder"</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.apache.camel.component.netty.ChannelHandlerFactories"</span> factory-method=<span class="code-quote">"newLengthFieldBasedFrameDecoder"</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:{{port}}?encoders=#encoders&amp;sync=<span class="code-keyword">false</span>"</span>);
</span>                
                from(<span class="code-quote">"netty:tcp:<span class="code-comment">//localhost:{{port}}?decoders=#length-decoder,#string-decoder&amp;sync=<span class="code-keyword">false</span>"</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&amp;amp;sync=false"</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&amp;amp;sync=false"</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-Addingcustomchannelpipelinefactoriestogaincompletecontroloveracreatedpipeline"></a>Adding custom channel pipeline factories to gain complete control over a created pipeline</h3>
<p><b>Available as of Camel 2.5</b></p>

<p>Custom channel pipelines provide complete control to the user over the handler/interceptor chain by inserting custom handler(s), encoder(s) &amp; decoders without having to specify them in the Netty Endpoint URL in a very simple way.</p>

<p>In order to add a custom pipeline, a custom channel pipeline factory must be created and registered with the context via the context registry (JNDIRegistry,or the camel-spring ApplicationContextRegistry etc).</p>

<p>A custom pipeline factory must be constructed as follows</p>
<ul>
	<li>A Producer linked channel pipeline factory must extend the abstract class <tt>ClientPipelineFactory</tt>.</li>
	<li>A Consumer linked channel pipeline factory must extend the abstract class <tt>ServerPipelineFactory</tt>.</li>
	<li>The classes should override the getPipeline() method in order to insert custom handler(s), encoder(s) and decoder(s). Not overriding the getPipeline() method creates a pipeline with no handlers, encoders or decoders wired to the pipeline.</li>
</ul>


<p>The example below shows how ServerChannel Pipeline factory may be created</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Using custom pipeline factory</b></div><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class SampleServerChannelPipelineFactory <span class="code-keyword">extends</span> ServerPipelineFactory {
    <span class="code-keyword">private</span> <span class="code-object">int</span> maxLineSize = 1024;

    <span class="code-keyword">public</span> ChannelPipeline getPipeline() <span class="code-keyword">throws</span> Exception {
        ChannelPipeline channelPipeline = Channels.pipeline();

        channelPipeline.addLast(<span class="code-quote">"encoder-SD"</span>, <span class="code-keyword">new</span> StringEncoder(CharsetUtil.UTF_8));
        channelPipeline.addLast(<span class="code-quote">"decoder-DELIM"</span>, <span class="code-keyword">new</span> DelimiterBasedFrameDecoder(maxLineSize, <span class="code-keyword">true</span>, Delimiters.lineDelimiter()));
        channelPipeline.addLast(<span class="code-quote">"decoder-SD"</span>, <span class="code-keyword">new</span> StringDecoder(CharsetUtil.UTF_8));
        <span class="code-comment">// here we add the <span class="code-keyword">default</span> Camel ServerChannelHandler <span class="code-keyword">for</span> the consumer, to allow Camel to route the message etc.
</span>        channelPipeline.addLast(<span class="code-quote">"handler"</span>, <span class="code-keyword">new</span> ServerChannelHandler(consumer));

        <span class="code-keyword">return</span> channelPipeline;
    }
}
</pre>
</div></div>

<p>The custom channel pipeline factory can then be added to the registry and instantiated/utilized on a camel route in the following way</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Registry registry = camelContext.getRegistry();
serverPipelineFactory = <span class="code-keyword">new</span> TestServerChannelPipelineFactory();
registry.bind(<span class="code-quote">"spf"</span>, serverPipelineFactory);
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:5150?serverPipelineFactory=#spf"</span>
</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>


<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="https://cwiki.apache.org/confluence/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Netty">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=14814487&revisedVersion=54&originalVersion=53">View Changes</a>
                |
        <a href="https://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