Wiki Markup |
---|
h2. Stream Component |
...
The *stream:* component provides access to the {{System.in}}, {{System.out}} and {{System.err}} streams as well as allowing streaming of file and URL. |
...
Maven users will need to add the following dependency to their {{pom.xml}} for this component: |
...
{code | ||
:xml | xml | } <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-stream</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> |
URI format
Code Block |
---|
{code} h3. URI format {code} stream:in[?options] stream:out[?options] stream:err[?options] stream:header[?options] {code} In addition, the {{file}} and {{url}} endpoint URIs are supported in *Camel 2.0*: |
...
{code |
} stream:file?fileName=/foo/bar.txt stream:url[?options] {code} If the {{stream:header}} URI is specified, the {{stream}} header is used to find the stream to write to. This option is available only for stream producers (that is, it cannot appear in {{from()}}). |
...
You can append query options to the URI in the following format, {{?option=value&option=value&...}} h3. |
...
Options
Name | Default Value | Description |
---|---|---|
| | Initial delay in milliseconds before consuming or producing the stream. |
| JVM Default | As of 1.4, you can configure the encoding (is a charset name) to use text-based streams (for example, message body is a |
| | Camel 2.0: Message prompt to use when reading from |
| | Camel 2.0: Optional delay in milliseconds before showing the message prompt. |
| | Camel 2.0: 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. |
| | Camel 2.0: When using the |
| | Camel 2.0: To be used for continuously reading a stream such as the unix |
| | Camel 2.0: Delay in milliseconds between read attempts when using |
| | 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. |
Message content
The stream: component supports either String
or byte[]
for writing to streams. Just add either String
or byte[]
content to the message.in.body
.
The special stream:header
URI is used for custom output streams. Just add a java.io.OutputStream
object to message.in.header
in the key header
.
See samples for an example.
Samples
In the following sample we route messages from the direct:in
endpoint to the System.out
stream:
Wiki Markup |
---|
Options {div:class=confluenceTableSmall} || Name || Default Value || Description || | {{delay}} | {{0}} | Initial delay in milliseconds before consuming or producing the stream. | | {{encoding}} | _JVM Default_ | As of 1.4, you can configure the encoding (is a [charset name|http://java.sun.com/j2se/1.5.0/docs/api/java/nio/charset/Charset.html]) to use text-based streams (for example, message body is a {{String}} object). If not provided, Camel uses the [JVM default Charset|http://java.sun.com/j2se/1.5.0/docs/api/java/nio/charset/Charset.html#defaultCharset()]. | | {{promptMessage}} | {{null}} | *Camel 2.0:* Message prompt to use when reading from {{stream:in}}; for example, you could set this to {{Enter a command:}} | | {{promptDelay}} | {{0}} | *Camel 2.0:* Optional delay in milliseconds before showing the message prompt. | | {{initialPromptDelay}} | {{2000}} | *Camel 2.0:* 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. | | {{fileName}} | {{null}} | *Camel 2.0:* When using the {{stream:file}} URI format, this option specifies the filename to stream to/from. | | {{scanStream}} | {{false}} | *Camel 2.0:* To be used for continuously reading a stream such as the unix {{tail}} command. *Camel 2.4:* will retry opening the file if it is overwritten, like {{tail --retry}} | | {{scanStreamDelay}} | {{0}} | *Camel 2.0:* Delay in milliseconds between read attempts when using {{scanStream}}. | | {{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. | {div} h3. Message content The *stream:* component supports either {{String}} or {{byte[]}} for writing to streams. Just add either {{String}} or {{byte[]}} content to the {{message.in.body}}. The special {{stream:header}} URI is used for custom output streams. Just add a {{java.io.OutputStream}} object to {{message.in.header}} in the key {{header}}. See samples for an example. h3. Samples In the following sample we route messages from the {{direct:in}} endpoint to the {{System.out}} stream: {snippet:id=e1|lang=java|url=camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamSystemOutTest.java} 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 |
...
Wiki Markup |
---|
}}. {snippet:id=e1|lang=java|url=camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamHeaderTest.java} The following sample demonstrates how to continuously read a file stream (analogous to the UNIX {{tail}} command): |
...
{code | ||
:java | java | } from("stream:file?fileName=/server/logs/server.log&scanStream=true&scanStreamDelay=1000").to("bean:logService?method=parseLogLine"); |
...
{code} {include:Endpoint See |
...
Also}
|