Using the Bean component in Camel

The Bean component can be used as part of a camel route to filter/intercept the payload of data contained in the route. Let’s see a very simple example.

The purpose of this example is to convert money contained in a text file from Dollars to Euros. The beans that will do the job is MoneyConverter which needs to be stored in org.apache.camel.impl.SimpleRegistry in order to be used in our route

import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;

public class JavaRouter {
  public static void main(String[] args) throws Exception {
    SimpleRegistry registry = new SimpleRegistry();
    registry.put("moneyConverter", new MoneyConverter());
    CamelContext context = new DefaultCamelContext(registry);
    context.addRoutes(new MyRouteBuilder());

At its heart, the MoneyConverter class turns the payload received from one currency to another:

public class MoneyConverter {
  public String convertValue(String data) throws Exception {
    double amount = (Integer.parseInt(data.replace("$", "").trim()) * 0.9);
    System.out.println("amount " + amount);
    return "€ " + amount;

The RouteBuilder class, drives the execution by including in the route a reference to the MoneyConverter and to its method convertValue:

import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder extends RouteBuilder {
  public void configure() throws Exception {
        .beanRef("moneyConverter", "convertValue")