LibraryLink ToToggle FramesPrintFeedback

Chapter 51. Stream

The stream: component provides access to the System.in, System.out and System.err streams together with allowing streaming of file and url.

stream:in
stream:out
stream:err
stream:header

And the file and url is supported in FUSE Mediation Router 2.0:

stream:file?fileName=/foo/bar.txt
stream:url 

If the stream:header option is specified then the stream header is used to find the stream to write to. This option is only available for StreamProducer.

Name Default Value Description
delay 0 Initial delay in millis before consuming or producing the stream.
encoding JVM Default As of 1.4 or later you can configure the encoding (is a charset name) to use text based streams (eg. message body is a String object). If not provided FUSE Mediation Router will use the JVM default Charset.
promptMessage null FUSE Mediation Router 2.0: Leading prompt message that can be used when reading from stream:in to have a leading text such as Enter a command:
promptMessageDelay 0 FUSE Mediation Router 2.0: Optional delay in millis before showing the prompt message. Can be used when system startup to avoid prompt message being written while other logging is done to the System out.
promptDelay 0 FUSE Mediation Router 2.0: Optional delay in millis before showing the prompt message.
promptInitialDelay 2000 FUSE Mediation Router 2.0: initial delay in millis before showing the prompt message. This delay only occurs once. Can be used when system startup to avoid prompt message being written while other logging is done to the System out.
fileName null FUSE Mediation Router 2.0: When using the stream:file notation this specifies the file name to stream to/from.
scanStream false FUSE Mediation Router 2.0: To be used for continuously reading a stream such as the unit tail command.
scanStreamDelay 0 FUSE Mediation Router 2.0: Delay in millis between read attempts when using scanStream.

The stream: component supports either String or byte[] for writing to streams. Just add to the message.in.body either a Stirng or byte[] content. The special stream:header URI is used for custom output streams. Just add a java.io.OutputStream to message.in.header in the key header. See samples for an example.

In this sample we output to System.out the content from the message when its put on the direct:in queue.

public void testStringContent() throws Exception {
    template.sendBody("direct:in", "Hello Text World\n");
}

public void testBinaryContent() {
    template.sendBody("direct:in", "Hello Bytes World\n".getBytes());
}

protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {
        public void configure() {
            from("direct:in").to("stream:out");
        }
    };
}

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

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

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);
    }
}

This sample demonstrates how to continously read a file stream such as the Unix tail command:

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