@MessageDriven or @Consume
To consume a message you use either the @MessageDriven annotation or from 1.5.0 the @Consume annotation to mark a particular method of a bean as being a consumer method. The uri of the annotation defines the Camel Endpoint to consume from.
What this does is basically create a route that looks kinda like this
from(uri).bean(theBean, "methodName");
The Bean Binding is then used to convert the inbound Message to the parameter list used to invoke the method
e.g. lets invoke the onCheese() method with the String body of the inbound JMS message from ActiveMQ on the cheese queue; this will use the Type Converter to convert the JMS ObjectMessage or BytesMessage to a String - or just use a TextMessage from JMS
public class Foo { @MessageDriven(uri="activemq:cheese") public void onCheese(String name) { ... } }
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
from(uri).beanRef("myBean", "methodName");
which will then look up in the Registry and find the bean and invoke the given bean name. (You can omit the method name 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 endpoint
from(uri).to("bean:myBean/methodName");
Which approach to use?
Using the @MessageDriven/@Consume annotations are simpler when you are creating a simple route with a single well defined input URI.
However if you require more complex routes or the same bean method needs to be invoked from many places then please use the routing DSL as shown above.