2

I am new to Apache Camel, and trying to figure out the best way to configure switching routes based on certain values.

What I do at the moment is:

  1. Retrieve the data from Kafka
  2. Then, process it in a controller, and put a certain value in a header (here I feel something is not right)
  3. Process the controller output header and check the value
  4. Choose the route to go based on the header's value
this.from("direct:kafka.scenario.update").routeId("publish.scenario.kafka.controller.route")
        .log(LoggingLevel.INFO, "Send Scenario update 2 Kafka Route").process(this.publishScenarioUpdateKafkaController).choice()
        .when( simple( "${out.header.updateType} == '" + ChangeType.UPDATE + "'" ))
        .process(this.publishVmsUpdateKafkaController)
        .to(dataRefScenarioUpdateProducerRoute)
        .when( simple( "${out.header.updateType} == '" + ChangeType.CREATE + "'" ))
        .process(this.publishVmsUpdateKafkaController)
        .to(dataRefScenarioCreateProducerRoute)
        .when( simple( "${out.header.updateType} == '" + ChangeType.DELETE + "'" ))
        .process(this.publishVmsUpdateKafkaController)
        .to(dataRefMessageDeleteProducerRoute);

Any thoughts?

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
Khalil Bouzekri
  • 210
  • 4
  • 12
  • Nothing wrong with using a header value to route a message. See the answer below about content based router. If your message is json or xml then you could possibly do the routing without a processor. – Namphibian Jan 30 '19 at 22:23
  • Yeah Content Based Router can be okay to use. You can also use a java bean method with the recipient list pattern (or the optimised toD) where the returned value of the bean method is a Camel endpoint uri where to route to. – Claus Ibsen Jan 31 '19 at 07:51

1 Answers1

2

You are implementing Content Based Router EIP and I believe your approach is correct. You can always use CamelTestSupport to test your RouteBuilders.

Possible caveat: Please see if the .when() checks on out.header.updateType invokes exchange.getOut()(I don't remember it exactly), if it does, you may be losing vital information in IN message. This may cause unexpected results. Its just a word of caution and you may already know the difference. Ensure to reach and understand this page if you haven't done it already.

ShellDragon
  • 1,712
  • 2
  • 12
  • 24