0
votes

I am using camel.version:2.12.1

I have a route that needs

  1. to parse a CSV file
  2. split each rows
  3. for each row, I need to determine target endpoint(s) and the record needs to routed those destinations.

Till date, I have

  org.apache.camel.model.ProcessorDefinition.recipientList().method(beanInstance, "methodName")

to route one incoming Camel Message.

How to implement dynamic routing at row level?

Thanks in advance.

1
After splitting the rows to individual records (ie by token \n), what is the logic of identifying the endpoint? Are you saying that after splitting, the records represent endpoints? Can you provide a sample of the input?Zeena

1 Answers

0
votes

Use the split pattern with a custom dispatcher:

public class MessageRouter {
    public String routeTo(final String row) {
        String id;
        if (row.contains("1")) {
            id = "sub1";
        } else if (row.contains("2")) {
            id = "sub2";
        } else {
            id = "default";
        }
        return "direct:" + id;
    }
}

Route definition:

@Override
public void configure() {
    from("direct:start")
       .split(body().tokenize("\n"))
       .recipientList()
       .method(MessageRouter.class);

    from("direct:token1")
        .log("Token1: body = ${body}");

    from("direct:token2")
        .log("Token2: body = ${body}");

    from("direct:default")
        .log("default: body = ${body}");

}

Testing with:

ProducerTemplate template = context.createProducerTemplate();
template.sendBody("direct:start", "token1\ntoken2\ntoken3");  

This leads to following output:

INFO  sub1: body = token1
INFO  sub2: body = token2
INFO  default: body = token3