Using Camel Stream Component

Apache Camel is an open-source integration framework that allows you to connect various systems and software applications. One of the components provided by Camel is the Stream component, which allows you to work with streams of data such as reading from and writing to files, standard input and output, and sockets.

In this tutorial, we will go through some examples of using the Camel Stream component to read from and write to streams.

Setting up the project

To use the Camel Stream component in your project, you will need to add the following dependency to your project’s POM file:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-stream</artifactId>
    <version>x.x.x</version>
</dependency>

When using the stream component, the following URI syntax is available:

  • stream:in[?options]
  • stream:out[?options]
  • stream:err[?options]
  • stream:header[?options]

Reading from a stream

To read from a stream, you can use the from method and specify the stream as the endpoint. For example, to read from the standard input stream (i.e. the console), you can use the following route:

from("stream:in")
    .log("Received message: ${body}");

In this example, the route will log any input entered in the console.

You can also read from a file by specifying the file path as the endpoint. For example:

from("stream:file?fileName=input.txt")
    .log("Received message: ${body}");

This route will read the contents of the input.txt file and log it.

Writing to a stream

To write to a stream, you can use the to method and specify the stream as the endpoint. For example, to write to the standard output stream (i.e. the console), you can use the following route:

from("direct:start")
    .to("stream:out");

Here is another example. Here, we are using the stream:out to perform a System.out of the content of files in the folder C:\Camel\In

package com.sample;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.DataFormat;

public class Hello {
  public static void main(String args[]) throws Exception {
    CamelContext context = new DefaultCamelContext();
    final DataFormat bindy = new BindyCsvDataFormat("com.sample.model");
    context.addRoutes(
        new RouteBuilder() {
          public void configure() {
            from("file:C:camelin?noop=true").to("stream:out");
          }
        });
    context.start();
    Thread.sleep(10000);
    context.stop();
  }
}

Note that you can combine different type of streams in your route: for example, the following RouteBuilder takes an input stream from the Console and prints it to the System.out stream:

context.addRoutes(new RouteBuilder() {
  public void configure() {
    from("stream:in").to("stream:out");
  }
});
Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon