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

Compare with Current View Page History

« Previous Version 7 Next »

@Consume

@MessageDriven is deprecated

@MessageDriven is deprecated in Camel >= 1.5.x and has been removed in Camel 2.x. Use @Consume instead.

Use the @Consume annotation to mark a particular method of a bean as being a consumer method. The uri parameter of the annotation defines the Camel Endpoint from which to consume.

For example, the following code invokes the onCheese() method with the String body of the inbound JMS message from ActiveMQ on the cheese queue. The built in Type Converter will be used to convert the JMS ObjectMessage or BytesMessage to a String (or just use a TextMessage from JMS).

public class Foo {

  @Consume(uri="activemq:cheese")
  public void onCheese(String name) {
    ...
  }
}

Bean Binding is then used to convert the inbound Message to the parameter list used to invoke the method. This is equivalent to the following explicit route written in the Java DSL:

from(uri).bean(theBean, "methodName");

When using more than one CamelContext

Using more than one CamelContext may create creating a POJO Consuming for each CamelContext. Camel 2.x allows you to bind to a specific CamelContext via a new option on the @Consume annotation.

Using context option to apply only a certain CamelContext

As of Camel 2.x, the context option may be used to specify a specific CamelContext.

  @Consume(uri="activemq:cheese", context="camel-1")
  public void onCheese(String name) {

The above consumer will only be created for the CamelContext that has the context id camel-1. You set this id in the XML tag as follows:

<camelContext id="camel-1" ...>

Using an explicit route

If you want to invoke a bean method from many different endpoints or within different complex routes in different circumstances you can just use the normal routing DSL or the Spring XML configuration file.

For example, the following DSL

from(uri).beanRef("myBean", "methodName");

will cause Camel to look up myBean in the Registry and invoke methodName. Optionally, the method name may be omitted; Camel will then figure out the right method based on the method annotations and body type.

Here is the equivalent code using the Bean endpoint.

from(uri).to("bean:myBean/methodName");

Which approach to use?

Use the annotations when you are creating a simple route with a single well-defined input URI. If more complex routes are required or the same bean method needs to be invoked from many places, consider using explicit routes.

  • No labels