You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 19 Next »

Bean Binding

The Bean Binding in Camel defines both which methods are invoked and also how the Message is converted into the parameters of the method when it is invoked.

Choosing the method to invoke

The binding of a Camel Message to a bean method call can occur in different ways

  • the method name can be specified explicitly in the DSL or when using POJO Consuming
  • if the bean can be converted to a Processor using the Type Converter mechanism then this is used to process the message. This mechanism is used by the ActiveMQ component to allow any MessageListener to be invoked by the Bean component
  • if the body of the message can be converted to a BeanInvocation (the default payload used by the ProxyHelper) - then that its used to invoke the method and pass the arguments
  • if the message contains the header org.apache.camel.MethodName then that method is invoked, converting the body to whatever the argument is to the method
  • otherwise the type of the method body is used to try find a method which matches; an error is thrown if a single method cannot be chosen unambiguously.
  • you can also use Exchange as the parameter itself, but then the return type must be void.

By default the return value is set on the outbound message body.

Binding Annotations

You can use the Binding Annotations to customize how paramter values are created from the Message

Examples

For example a POJO such as:

public class Bar {

    public String doSomething(String body) {
      // process the in body and return whatever you want
      return "Bye World";
   }

Or the Exchange example. Notice that the return type must be void:

public class Bar {

    public void doSomething(Exchange exchange) {
      // process the exchange
      exchange.getIn().setBody("Bye World");
   }

For example you could write a method like this (showing also a feature in Camel, the @MessageDriven annotation):

@MessageDriven requires camel-spring

Using the @MessageDriven annotations requires camel-spring that uses the org.apache.camel.spring.CamelBeanPostProcessor to perform the setup for this consumer and the needed bean bindings.

public class Foo {

    @MessageDriven(uri = "activemq:my.queue")
    public void doSomething(String body) {
		// process the inbound message here
    }

}

Here Camel with subscribe to an ActiveMQ queue, then convert the message payload to a String (so dealing with TextMessage, ObjectMessage and BytesMessage in JMS), then process this method.

  • No labels