Home > Mobile >  Apache Camel: get consumer endpoint information in bean method called by the Camel route
Apache Camel: get consumer endpoint information in bean method called by the Camel route

Time:08-01

I have multiple databases, all containing the same table Data. I want to read from them, input all Data elements into the MyBean method @Handler public Data updateData(Data data) and write back the output of the method.

from("jpa://Data?persistenceUnit=persUnit1").to("direct:collector");
from("jpa://Data?persistenceUnit=persUnit2").to("direct:collector");
from("jpa://Data?persistenceUnit=persUnit3").to("direct:collector");
...

from("direct:collector").bean(new MyBean()).to("jpa://Data?persistenceUnit=destinationUnit");

However I need the information from which source the Data element came (e.g. the name of the persistence unit) within the bean for validation. What's the best way to do so?

CodePudding user response:

You could set a header

from("jpa://Data?persistenceUnit=persUnit1")
    .setHeader("dataSource", constant("dataSource1"))
    .to("direct:collector");
from("jpa://Data?persistenceUnit=persUnit2")
    .setHeader("dataSource", constant("dataSource2"))
    .to("direct:collector");

CodePudding user response:

The exchange object provides the necessary information to about the "from" endpoint (of current exchange):

https://www.javadoc.io/doc/org.apache.camel/camel-api/latest/org/apache/camel/Exchange.html#getFromRouteId()

It's quite usual to put id on routes (but also on endpoints)

from("jpa://Data?persistenceUnit=persUnit1")
  .routeId("ComingFromRouteA")
  .to("direct:collector");

This way, you can know where you come from, using:

exchange.getFromRouteId()
  • Related