camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Stream
Date Wed, 13 Mar 2013 07:38: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/Stream">Stream</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~njiang">willem
jiang</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" >| {{groupLines}} | {{0}} | *Camel
2.5:* To group X number of lines in the consumer. For example to group 10 lines and therefore
only spit out an [Exchange] with 10 lines, instead of 1 [Exchange] per line. | <br>|
{{autoCloseCount}} | {{0}} | *Camel 2.10.0:* (2.9.3 and 2.8.6) Number of messages to process
before closing stream on Producer side. Never close stream by default (only when Producer
is stopped). If more messages are sent, the stream is reopened for another {{autoCloseCount}}
batch. | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
{{closeOnDone}} | {{false}} | *Camel 2.11.0:* If it is true and stream is based on file, the
producer will close the stream once it saw the flag of split complit. | <br></td></tr>
            <tr><td class="diff-unchanged" >{div} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Stream-StreamComponent"></a>Stream Component</h2>

<p>The <b>stream:</b> component provides access to the <tt>System.in</tt>,
<tt>System.out</tt> and <tt>System.err</tt> streams as well as allowing
streaming of file and URL.</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-stream<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="Stream-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
stream:in[?options]
stream:out[?options]
stream:err[?options]
stream:header[?options]
</pre>
</div></div>

<p>In addition, the <tt>file</tt> and <tt>url</tt> endpoint
URIs are supported:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
stream:file?fileName=/foo/bar.txt
stream:url[?options]
</pre>
</div></div>

<p>If the <tt>stream:header</tt> URI is specified, the <tt>stream</tt>
header is used to find the stream to write to. This option is available only for stream producers
(that is, it cannot appear in <tt>from()</tt>).</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="Stream-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>delay</tt> </td>
<td class='confluenceTd'> <tt>0</tt> </td>
<td class='confluenceTd'> Initial delay in milliseconds before consuming or producing
the stream. </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 (is 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 text-based streams
(for example, message body is a <tt>String</tt> object). If not provided, Camel
uses 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>promptMessage</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Message prompt to use when reading from <tt>stream:in</tt>;
for example, you could set this to <tt>Enter a command:</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>promptDelay</tt> </td>
<td class='confluenceTd'> <tt>0</tt> </td>
<td class='confluenceTd'> Optional delay in milliseconds before showing the message
prompt. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>initialPromptDelay</tt> </td>
<td class='confluenceTd'> <tt>2000</tt> </td>
<td class='confluenceTd'> Initial delay in milliseconds before showing the message prompt.
This delay occurs only once. Can be used during system startup to avoid message prompts being
written while other logging is done to the system out. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>fileName</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> When using the <tt>stream:file</tt> URI format,
this option specifies the filename to stream to/from. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>url</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> When using the <tt>stream:url</tt> URI format,
this option specifies the URL to stream to/from. The input/output stream will be opened using
the <a href="http://docs.oracle.com/javase/6/docs/api/java/net/URLConnection.html" class="external-link"
rel="nofollow">JDK URLConnection</a> facility. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>scanStream</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> To be used for continuously reading a stream such as the unix
<tt>tail</tt> command. <br/>
                               <b>Camel 2.4 to Camel 2.6:</b> will retry opening
the file if it is overwritten, somewhat like <tt>tail --retry</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>retry</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.7:</b> will retry opening the file
if it's overwritten, somewhat like <tt>tail --retry</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>scanStreamDelay</tt> </td>
<td class='confluenceTd'> <tt>0</tt> </td>
<td class='confluenceTd'> Delay in milliseconds between read attempts when using <tt>scanStream</tt>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>groupLines</tt> </td>
<td class='confluenceTd'> <tt>0</tt> </td>
<td class='confluenceTd'> <b>Camel 2.5:</b> To group X number of lines in
the consumer. For example to group 10 lines and therefore only spit out an <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> with 10 lines, instead of 1 <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> per line. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>autoCloseCount</tt> </td>
<td class='confluenceTd'> <tt>0</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10.0:</b> (2.9.3 and 2.8.6) Number
of messages to process before closing stream on Producer side. Never close stream by default
(only when Producer is stopped). If more messages are sent, the stream is reopened for another
<tt>autoCloseCount</tt> batch. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>closeOnDone</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.11.0:</b> If it is true and stream
is based on file, the producer will close the stream once it saw the flag of split complit.
</td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Stream-Messagecontent"></a>Message content</h3>

<p>The <b>stream:</b> component supports either <tt>String</tt>
or <tt>byte[]</tt> for writing to streams. Just add either <tt>String</tt>
or <tt>byte[]</tt> content to the <tt>message.in.body</tt>. Messages
sent to the <b>stream:</b> producer in binary mode are not followed by the newline
character (as opposed to the <tt>String</tt> messages). Message with <tt>null</tt>
body will not be appended to the output stream.<br/>
The special <tt>stream:header</tt> URI is used for custom output streams. Just
add a <tt>java.io.OutputStream</tt> object to <tt>message.in.header</tt>
in the key <tt>header</tt>.<br/>
See samples for an example.</p>

<h3><a name="Stream-Samples"></a>Samples</h3>

<p>In the following sample we route messages from the <tt>direct:in</tt>
endpoint to the <tt>System.out</tt> stream:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// Route messages to the standard output.
</span>from(<span class="code-quote">"direct:in"</span>).to(<span class="code-quote">"stream:out"</span>);

<span class="code-comment">// Send <span class="code-object">String</span>
payload to the standard output.
</span><span class="code-comment">// Message will be followed by the newline.
</span>template.sendBody(<span class="code-quote">"direct:in"</span>, <span
class="code-quote">"Hello Text World"</span>);

<span class="code-comment">// Send <span class="code-object">byte</span>[]
payload to the standard output.
</span><span class="code-comment">// No newline will be added after the message.
</span>template.sendBody(<span class="code-quote">"direct:in"</span>, <span
class="code-quote">"Hello Bytes World"</span>.getBytes());
</pre>
</div></div>

<p>The following sample demonstrates how the header type can be used to determine which
stream to use. In the sample we use our own output stream, <tt>MyOutputStream</tt>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">private</span> OutputStream
mystream = <span class="code-keyword">new</span> MyOutputStream();
<span class="code-keyword">private</span> StringBuilder sb = <span class="code-keyword">new</span>
StringBuilder();

@Test
<span class="code-keyword">public</span> void testStringContent() {
    template.sendBody(<span class="code-quote">"direct:in"</span>, <span class="code-quote">"Hello"</span>);
    <span class="code-comment">// StreamProducer appends \n in text mode
</span>    assertEquals(<span class="code-quote">"Hello\n"</span>, sb.toString());
}

@Test
<span class="code-keyword">public</span> void testBinaryContent() {
    template.sendBody(<span class="code-quote">"direct:in"</span>, <span class="code-quote">"Hello"</span>.getBytes());
    <span class="code-comment">// StreamProducer is in binary mode so no \n is appended
</span>    assertEquals(<span class="code-quote">"Hello"</span>, sb.toString());
}

<span class="code-keyword">protected</span> RouteBuilder createRouteBuilder()
{
    <span class="code-keyword">return</span> <span class="code-keyword">new</span>
RouteBuilder() {
        <span class="code-keyword">public</span> void configure() {
            from(<span class="code-quote">"direct:in"</span>).setHeader(<span
class="code-quote">"stream"</span>, constant(mystream)).
                to(<span class="code-quote">"stream:header"</span>);
        }
    };
}

<span class="code-keyword">private</span> class MyOutputStream <span class="code-keyword">extends</span>
OutputStream {

    <span class="code-keyword">public</span> void write(<span class="code-object">int</span>
b) <span class="code-keyword">throws</span> IOException {
        sb.append((<span class="code-object">char</span>)b);
    }
}
</pre>
</div></div>

<p>The following sample demonstrates how to continuously read a file stream (analogous
to the UNIX <tt>tail</tt> command):</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"stream:file?fileName=/server/logs/server.log&amp;scanStream=<span
class="code-keyword">true</span>&amp;scanStreamDelay=1000"</span>).to(<span
class="code-quote">"bean:logService?method=parseLogLine"</span>);
</pre>
</div></div>

<p>One gotcha with scanStream (pre Camel 2.7) or scanStream + retry is the file will
be re-opened and scanned with each iteration of scanStreamDelay.  Until NIO2 is available
we cannot reliably detect when a file is deleted/recreated.</p>

<h3><a name="Stream-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>

    </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/Stream">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=81400&revisedVersion=24&originalVersion=23">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Stream?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message