이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 124. Stream


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.

URI format

stream:in[?options]
stream:out[?options]
stream:err[?options]
stream:header[?options]
Copy to Clipboard Toggle word wrap
In addition, the file and url endpoint URIs are supported in Apache Camel 2.0:
stream:file?fileName=/foo/bar.txt
stream:url[?options]
Copy to Clipboard Toggle word wrap
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&...

Options

Expand
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) to use text-based streams (for example, message body is a String object). If not provided, Apache Camel uses the JVM default Charset.
promptMessage null Apache Camel 2.0: Message prompt to use when reading from stream:in; for example, you could set this to Enter a command:
promptDelay 0 Apache Camel 2.0: Optional delay in milliseconds before showing the message prompt.
initialPromptDelay 2000 Apache 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 Apache Camel 2.0: When using the stream:file URI format, this option specifies the filename to stream to/from.
url null When using the stream:url URI format, this option specifies the URL to stream to/from. The input/output stream will be opened using the JDK URLConnection facility.
scanStream false
Apache Camel 2.0: To be used for continuously reading a stream such as the unix tail command. Camel 2.4 to Camel 2.6: will retry opening the file if it is overwritten, somewhat like tail --retry
retry false Camel 2.7: will retry opening the file if it's overwritten, somewhat like tail --retry
scanStreamDelay 0 Apache 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.
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.
closeOnDone false Camel 2.11.0: This option is used in combination with Splitter and streaming to the same file. The idea is to keep the stream open and only close when the Splitter is done, to improve performance. Mind this requires that you only stream to the same file, and not 2 or more files.

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. Messages sent to the stream: producer in binary mode are not followed by the newline character (as opposed to the String messages). Message with null body will not be appended to the output stream.

Samples

In the following sample we route messages from the direct:in endpoint to the System.out stream:
// Route messages to the standard output.
from("direct:in").to("stream:out");

// Send String payload to the standard output.
// Message will be followed by the newline.
template.sendBody("direct:in", "Hello Text World");

// Send byte[] payload to the standard output.
// No newline will be added after the message.
template.sendBody("direct:in", "Hello Bytes World".getBytes());
Copy to Clipboard Toggle word wrap
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.
private OutputStream mystream = new MyOutputStream();
private StringBuffer sb = new StringBuffer();

@Test
public void testStringContent() {
    template.sendBody("direct:in", "Hello");
    // StreamProducer appends \n in text mode
    assertEquals("Hello\n", sb.toString());
}

@Test
public void testBinaryContent() {
    template.sendBody("direct:in", "Hello".getBytes());
    // StreamProducer is in binary mode so no \n is appended
    assertEquals("Hello", sb.toString());
}

protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {
        public void configure() {
            from("direct:in").setHeader("stream", constant(mystream)).
                to("stream:header");
        }
    };
}

private class MyOutputStream extends OutputStream {

    public void write(int b) throws IOException {
        sb.append((char)b);
    }
}
Copy to Clipboard Toggle word wrap
The following sample demonstrates how to continuously read a file stream (analogous to the UNIX tail command):
from("stream:file?fileName=/server/logs/server.log&scanStream=true&scanStreamDelay=1000").to("bean:logService?method=parseLogLine");
Copy to Clipboard Toggle word wrap
Note
One difficulty with using the combination of scanStream and retry is that 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 or recreated.
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat