330.5. Samples

在以下示例中,我们将来自 direct:in 端点的消息路由到 system .out 流:

// 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());

以下示例演示了如何使用标头类型来确定要使用的流。在示例中,我们使用自己的输出流 MyOutputStream

以下示例演示如何持续读取文件流(与 UNIX tail 命令相关):

from("stream:file?fileName=/server/logs/server.log&scanStream=true&scanStreamDelay=1000")
  .to("bean:logService?method=parseLogLine");

一个带有 scanStream (pre Camel 2.7)或 scanStream + 重试的 getcha 是指文件将重新打开并扫描,并在每次 scanStreamDelay 每次进行扫描。在 NIO2 可用之前,当文件被删除/重新创建时,我们无法可靠检测。

如果要重新加载文件,如果该文件滚动/写入,则您还应打开文件 Watcher重试 选项。

from("stream:file?fileName=/server/logs/server.log&scanStream=true&scanStreamDelay=1000&retry=true&fileWatcher=true")
  .to("bean:logService?method=parseLogLine");