23 Eylül 2022 Cuma

Camel Choice ve When - Content Based Routing İçindir

Giriş
Açıklaması şöyle
Apache Camel contains a powerful feature called content-based routers. This allows you to process the message differently relying on the content.

They are quite similar to the if/else statement in Java. Regardless, in Camel, the equivalent words are when and otherwise.
Örnek
Şöyle yaparız
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class ChoiceWhenRoute extends RouteBuilder {
  @Override
  public void configure() throws Exception {

    from("direct:startWhenChoiceRoute")
      .routeId("direct:startWhenChoiceRoute")
      .log( "${body}")
      .choice()
      .when(body().isNull())
      .log("Message body is empty.")
      .log("${body}")
      .end();
  }
}
Test için şöyle yaparız
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.ExchangeBuilder;

@RestController
public class Controler {

  @Autowired
  private ProducerTemplate producerTemplate;

  @Autowired
  private CamelContext camelContext;

  @GetMapping
  public void function() {
    Exchange requestExchange = ExchangeBuilder
      .anExchange(camelContext)
      .build();

    producerTemplate
      .send("direct:startWhenChoiceRoute",requestExchange);

  }
}