Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reverted from v. 6 (Scott meed CLA)

@MessageDriven or @Consume

Info
title@MessageDriven is deprecated@deprecated

@MessageDriven is deprecated in Camel >= 1.5.x and has been .x. You should use @Consume instead. Its removed in Camel 2.x. Use @Consume instead.0.

To consume a message you use either the @MessageDriven annotation or from 1.5.0 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 from.

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

Code Block
public class Foo {

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

The 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:

What this does is basically create a route that looks kinda like this

Code Block
from(uri).bean(theBean, "methodName");
Warning
titleWhen using more than one CamelContext

Using When you use more than one CamelContext may create 1 CamelContext you might end up with each of them creating a POJO Consuming for each CamelContext.
In Camel 2.x allows you to bind to a specific CamelContext via 0 there is a new option on the @Consume annotation @Consume that allows you to specify which CamelContext id/name you want it to apply for.

Using context option to apply only a certain CamelContext

As Available as of Camel 2.x, 0
See the warning above.

You can use the context option may be used to specify a specific CamelContext.which CamelContext the consumer should only apply for. For example:

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

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

Code Block
xml
xml
<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

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

which will cause Camel to then look up myBean in the Registry and invoke methodName. Optionally, find the bean and invoke the given bean name. (You can omit the method name may be omitted; Camel will then and have Camel figure out the right method based on the method annotations and body type).

Use the Bean endpoint

You can always use the bean endpointHere is the equivalent code using the Bean endpoint.

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

Which approach to use?

Use Using the @MessageDriven/@Consume annotations are simpler when you are creating a simple route with a single well - defined input URI. If

However if you require more complex routes are required or the same bean method needs to be invoked from many places , consider using explicit routesthen please use the routing DSL as shown above.