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 > MINA2
Date Wed, 07 Aug 2013 06:51: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/MINA2">MINA2</a></h2>
    <h4>Page  <b>added</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h2><a name="MINA2-MINA2Component"></a>MINA 2 Component</h2>
<p><b>Available as of Camel 2.10</b></p>

<p>The <b>mina2:</b> component is a transport for working with <a href="http://mina.apache.org/"
class="external-link" rel="nofollow">Apache MINA 2.x</a></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="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;dependency&gt;
    &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
    &lt;artifactId&gt;camel-mina2&lt;/artifactId&gt;
    &lt;version&gt;x.x.x&lt;/version&gt;
    &lt;!-- use the same version as your Camel core version --&gt;
&lt;/dependency&gt;
</pre>
</div></div>

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

<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;">
mina2:tcp://hostname[:port][?options]
mina2:udp://hostname[:port][?options]
mina2:vm://hostname[:port][?options]
</pre>
</div></div>
<p>You can specify a codec in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>
using the <b>codec</b> option. If you are using TCP and no codec is specified
then the <tt>textline</tt> flag is used to determine if text line based codec
or object serialization should be used instead. By default the object serialization is used.</p>

<p>For UDP if no codec is specified the default uses a basic <tt>ByteBuffer</tt>
based codec.</p>

<p>The VM protocol is used as a direct forwarding mechanism in the same JVM. </p>

<p>A Mina producer has a default timeout value of 30 seconds, while it waits for a response
from the remote server.</p>

<p>In normal use, <tt>camel-mina</tt> only supports marshalling the body
content&mdash;message headers and exchange properties are not sent.<br/>
However, the option, <b>transferExchange</b>, does allow you to transfer the exchange
itself over the wire. See options below.</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="MINA2-Options"></a>Options</h3>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>codec</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> You can refer to a named <tt>ProtocolCodecFactory</tt>
instance in your <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>
such as your Spring <tt>ApplicationContext</tt>, which is then used for the marshalling.
</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 Mina session right
after use. Can be used for both consumer and producer. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>textline</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> 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 <tt>false</tt>,
then Object Serialization is assumed over TCP. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>textlineDelimiter</tt> </td>
<td class='confluenceTd'> <tt>DEFAULT</tt> </td>
<td class='confluenceTd'> Only used for TCP and if <b>textline=true</b>.
Sets the text line delimiter to use. Possible values are: <tt>DEFAULT</tt>, <tt>AUTO</tt>,
<tt>WINDOWS</tt>, <tt>UNIX</tt> or <tt>MAC</tt>. If none
provided, Camel will use <tt>DEFAULT</tt>. This delimiter is used to mark the
end of text. </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>lazySessionCreation</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Sessions 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>timeout</tt> </td>
<td class='confluenceTd'> <tt>30000</tt> </td>
<td class='confluenceTd'> You can configure the timeout that specifies how long to wait
for a response from a remote server. The timeout unit is in milliseconds, so 60000 is 60 seconds.
The timeout is only used for Mina producer. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>encoding</tt> </td>
<td class='confluenceTd'> <em>JVM Default</em> </td>
<td class='confluenceTd'> You can configure the encoding (a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/nio/charset/Charset.html"
class="external-link" rel="nofollow">charset name</a>) to use for the TCP textline
codec and the UDP protocol. If not provided, Camel will use the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/nio/charset/Charset.html#defaultCharset()"
class="external-link" rel="nofollow">JVM default Charset</a>. </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 <em>serializable</em>. Camel will exclude any non-serializable
objects and log it at <tt>WARN</tt> level. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>minaLogger</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> You can enable the Apache MINA logging filter. Apache MINA
uses <tt>slf4j</tt> logging at <tt>INFO</tt> level to log all input
and output. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>filters</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> You can set a list of <a href="http://mina.apache.org/iofilter.html"
class="external-link" rel="nofollow">Mina IoFilters</a> to register. The <tt>filters</tt>
can be specified as a comma-separate list of bean references (e.g. <tt>&#35;filterBean1,#filterBean2</tt>)
where each bean must be of type <tt>org.apache.mina.common.IoFilter</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>encoderMaxLineLength</tt> </td>
<td class='confluenceTd'> <tt>&#45;1</tt> </td>
<td class='confluenceTd'> Set the textline protocol encoder max line length. By default
the default value of Mina itself is used which are <tt>Integer.MAX_VALUE</tt>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>decoderMaxLineLength</tt> </td>
<td class='confluenceTd'> <tt>&#45;1</tt> </td>
<td class='confluenceTd'> Set the textline protocol decoder max line length. By default
the default value of Mina itself is used which are 1024. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maximumPoolSize</tt> </td>
<td class='confluenceTd'> 16 </td>
<td class='confluenceTd'> The TCP producer is thread safe and supports concurrency much
better. This option allows you to configure the number of threads in its thread pool for concurrent
producers. <b>Note:</b> Camel has a pooled service which ensured it was already
thread safe and supported concurrency already. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>allowDefaultCodec</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> The mina component installs a default codec if both, <tt>codec</tt>
is <tt>null</tt> and <tt>textline</tt> is <tt>false</tt>.
Setting <tt>allowDefaultCodec</tt> to <tt>false</tt> prevents the
mina component from installing a default codec as the first element in the filter chain. This
is useful in scenarios where another filter must be the first in the filter chain, like the
SSL filter. </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 MinaConsumer
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 MinaConsumer 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>orderedThreadPoolExecutor</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Whether to use ordered thread pool, to ensure events are processed
orderly on the same channel. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sslContextParameters</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> SSL configuration using an <tt>org.apache.camel.util.jsse.SSLContextParameters</tt>
instance. See <a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=Using+the+JSSE+Configuration+Utility&amp;linkCreation=true&amp;fromPageId=34013589"
class="createlink">Using the JSSE Configuration Utility</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>autoStartTls</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Whether to auto start SSL handshake. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="MINA2-Usingacustomcodec"></a>Using a custom codec</h3>

<p>See the Mina how to write your own codec. To use your custom codec with <tt>camel-mina</tt>,
you should register your codec in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>;
for example, by creating a bean in the Spring XML file. Then use the <tt>codec</tt>
option to specify the bean ID of your codec. See <a href="/confluence/display/CAMEL/HL7"
title="HL7">HL7</a> that has a custom codec.</p>

<h3><a name="MINA2-Samplewithsync%3Dfalse"></a>Sample with sync=false</h3>

<p>In this sample, Camel exposes a service that listens for TCP connections on port
6200. We use the <b>textline</b> codec. In our route, we create a Mina consumer
endpoint that listens on port 6200:</p>

<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException:
Index: 20, Size: 20</span> </div>

<p>As the sample is part of a unit test, we test it by sending some data to it on port
6200.</p>

<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException:
Index: 20, Size: 20</span> </div>

<h3><a name="MINA2-Samplewithsync%3Dtrue"></a>Sample with sync=true</h3>

<p>In the next sample, we have a more common use case where we expose a TCP service
on port 6201 also use the textline codec. However, this time we want to return a response,
so we set the <tt>sync</tt> option to <tt>true</tt> on the consumer.</p>

<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException:
Index: 20, Size: 20</span> </div>

<p>Then we test the sample by sending some data and retrieving the response using the
<tt>template.requestBody()</tt> method. As we know the response is a <tt>String</tt>,
we cast it to <tt>String</tt> and can assert that the response is, in fact, something
we have dynamically set in our processor code logic.</p>

<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException:
Index: 20, Size: 20</span> </div>

<h3><a name="MINA2-SamplewithSpringDSL"></a>Sample with Spring DSL</h3>

<p>Spring DSL can, of course, also be used for <a href="/confluence/display/CAMEL/MINA"
title="MINA">MINA</a>. In the sample below we expose a TCP server on port 5555:</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;route&gt;
     &lt;from uri="mina2:tcp://localhost:5555?textline=true"/&gt;
     &lt;to uri="bean:myTCPOrderHandler"/&gt;
  &lt;/route&gt;
</pre>
</div></div>

<p>In the route above, we expose a TCP server on port 5555 using the textline codec.
We let the Spring bean with ID, <tt>myTCPOrderHandler</tt>, handle the request
and return a reply. For instance, the handler bean could be implemented as follows:</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;">
    public String handleOrder(String payload) {
        ...
        return "Order: OK"
   }
</pre>
</div></div>

<h3><a name="MINA2-ConfiguringMinaendpointsusingSpringbeanstyle"></a>Configuring
Mina endpoints using Spring bean style</h3>

<p>Configuration of Mina endpoints is possible using regular Spring bean style configuration
in the Spring DSL.</p>

<p>However, in the underlying Apache Mina toolkit, it is relatively difficult to set
up the acceptor and the connector, because you can <em>not</em> use simple setters.
To resolve this difficulty, we leverage the <tt>MinaComponent</tt> as a Spring
factory bean to configure this for us. If you really need to configure this yourself, there
are setters on the <tt>MinaEndpoint</tt> to set these when needed.</p>

<p>The sample below shows the factory approach:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: xml; gutter: false"><![CDATA[
&lt;!-- Creating mina endpoints is a bit complex so we reuse MinaComponnet
     as a factory bean to create our endpoint, this is the easiest to do --&gt;
&lt;bean id="myMinaFactory" class="org.apache.camel.component.mina2.Mina2Component"&gt;
    &lt;!-- we must provide a camel context so we refer to it by its id --&gt;
    &lt;constructor-arg index="0" ref="myCamel"/&gt;
&lt;/bean&gt;

&lt;!-- This is our mina endpoint configured with spring, we will use the factory above
     to create it for us. The goal is to invoke the createEndpoint method with the
     mina configuration parameter we defined using the constructor-arg option --&gt;
&lt;bean id="myMinaEndpoint"
      factory-bean="myMinaFactory"
      factory-method="createEndpoint"&gt;
    &lt;!-- and here we can pass it our configuration --&gt;
    &lt;constructor-arg index="0" ref="myMinaConfig"/&gt;
&lt;/bean&gt;

&lt;!-- this is our mina configuration with plain properties --&gt;
&lt;bean id="myMinaConfig" class="org.apache.camel.component.mina2.Mina2Configuration"&gt;
    &lt;property name="protocol" value="tcp"/&gt;
    &lt;property name="host" value="localhost"/&gt;
    &lt;property name="port" value="1234"/&gt;
    &lt;property name="sync" value="false"/&gt;
&lt;/bean&gt;
]]></script>
</div></div>

<p>And then we can refer to our endpoint directly in the route, as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: xml; gutter: false"><![CDATA[
&lt;route&gt;
    &lt;!-- here we route from or mina endpoint we have defined above --&gt;
    &lt;from ref="myMinaEndpoint"/&gt;
    &lt;to uri="mock:result"/&gt;
&lt;/route&gt;
]]></script>
</div></div>

<h3><a name="MINA2-ClosingSessionWhenComplete"></a>Closing Session When
Complete</h3>

<p>When acting as a server you sometimes want to close the session when, for example,
a client conversion is finished. To instruct Camel to close the session, you should add a
header with the key <tt>CamelMinaCloseSessionWhenComplete</tt> set to a boolean
<tt>true</tt> value.</p>

<p>For instance, the example below will close the session after it has written the <tt>bye</tt>
message back to the client:</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;">
        from("mina2:tcp://localhost:8080?sync=true&amp;textline=true").process(new Processor()
{
            public void process(Exchange exchange) throws Exception {
                String body = exchange.getIn().getBody(String.class);
                exchange.getOut().setBody("Bye " + body);
                exchange.getOut().setHeader(MinaConstants.MINA_CLOSE_SESSION_WHEN_COMPLETE,
true);
            }
        });
</pre>
</div></div>

<h3><a name="MINA2-GettheIoSessionformessage"></a>Get the IoSession for
message</h3>

<p>You can get the IoSession from the message header with this key <tt>Mina2Constants.MINA_IOSESSION</tt>,
and also get the local host address with the key <tt>Mina2Constants.MINA_LOCAL_ADDRESS</tt>
and remote host address with the key <tt>Mina2Constants.MINA_REMOTE_ADDRESS</tt>.</p>

<h3><a name="MINA2-ConfiguringMinafilters"></a>Configuring Mina filters</h3>

<p>Filters permit you to use some Mina Filters, such as <tt>SslFilter</tt>.
You can also implement some customized filters. Please note that <tt>codec</tt>
and <tt>logger</tt> are also implemented as Mina filters of type, <tt>IoFilter</tt>.
Any filters you may define are appended to the end of the filter chain; that is, after <tt>codec</tt>
and <tt>logger</tt>.</p>

<h3><a name="MINA2-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>
	<li><a href="/confluence/display/CAMEL/Netty" title="Netty">Netty</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/MINA2">View Online</a>
              |
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/MINA2?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message