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 Mon, 29 Mar 2010 01:31:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/Stream">Stream</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~njiang">willem
jiang</a>
    </h4>
     
          <br/>
     <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-java">
&lt;dependency&gt;
    &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
    &lt;artifactId&gt;camel-stream&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="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>

<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"
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()"
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>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 unit <tt>tail</tt> command. </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>
</tbody></table>

<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>.<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">@Test
<span class="code-keyword">public</span> void testStringContent() <span class="code-keyword">throws</span>
Exception {
    template.sendBody(<span class="code-quote">"direct:in"</span>, <span class="code-quote">"Hello
Text World\n"</span>);
}

@Test
<span class="code-keyword">public</span> void testBinaryContent() {
    template.sendBody(<span class="code-quote">"direct:in"</span>, <span class="code-quote">"Hello
Bytes World\n"</span>.getBytes());
}

<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>).to(<span class="code-quote">"stream:out"</span>);
        }
    };
}
</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?scanStream=<span
class="code-keyword">true</span>&amp;scanStreamDelay=1000"</span>).to(<span
class="code-quote">"bean:logService?method=parseLogLine"</span>);
</pre>
</div></div>

<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="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Stream">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=81400&revisedVersion=12&originalVersion=11">View
Change</a>
              |
       <a href="http://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