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, 06 Jun 2012 15:54: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/~hekonsek">Henryk
Konsek</a>
    </h4>
        <br/>
                         <h4>Changes (3)</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" >In the following sample we route messages
from the {{direct:in}} endpoint to the {{System.out}} stream: <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">{snippet:id=e1|lang=java|url=camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamSystemOutTest.java}
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{code:java}
<br>// Route messages to the standard output. <br>from(&quot;direct:in&quot;).to(&quot;stream:out&quot;);
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">//
Send String payload to the standard output. <br>// Message will be followed by the newline.
<br>template.sendBody(&quot;direct:in&quot;, &quot;Hello Text World&quot;);
<br> <br>// Send byte[] payload to the standard output. <br>// No newline
will be added after the message. <br>template.sendBody(&quot;direct:in&quot;,
&quot;Hello Bytes World&quot;.getBytes()); <br>{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >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, {{MyOutputStream}}. <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 in <b>Camel 2.0</b>:</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'>  As of 1.4, 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'> <b>Camel 2.0:</b> 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'> <b>Camel 2.0:</b> 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'> <b>Camel 2.0:</b> 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'> <b>Camel 2.0:</b> 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'> <b>Camel 2.0:</b> 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'> <b>Camel 2.0:</b> 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'> <b>Camel 2.0:</b> 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>
</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).<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> <span class="code-object">StringBuffer</span>
sb = <span class="code-keyword">new</span> <span class="code-object">StringBuffer</span>();

@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=21&originalVersion=20">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