Integration / Apache Camel Interview Questions
How does the Camel Bean component work — binding method calls into a route?
The Bean component invokes a method on a Spring/CDI/JNDI bean from within a route. It is the recommended way to call business logic without tying domain classes to the Camel API: the bean knows nothing about Camel — it just receives parameters and returns a value.
// Bean referenced by class:
from("jms:queue:orders")
.bean(OrderService.class, "processOrder")
.to("jms:queue:results");
// Bean by Spring bean name:
from("direct:validate")
.bean("orderService", "validate(Exchange)")
.log("Validated: ${body}");
// Parameter binding annotations in the bean class:
public class OrderService {
public Order processOrder(
@Body String rawJson,
@Header("orderId") String id,
@ExchangeProperty("tenantId") String tenant) {
return orderRepo.save(parse(rawJson, id, tenant));
}
}Camel uses reflection and the Bean Parameter Binding mechanism to automatically map Exchange data to method parameters: @Body injects the message body, @Header(name) injects a header, @ExchangeProperty(name) injects a property. If there are no annotations, Camel uses the body type to match. The return value of the method becomes the new message body.
More Related questions...