Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

@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

Code Block

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

Code Block

public class Foo {

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

h4. 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");

Code Block



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).

h4. Use the Bean endpoint

You can always use the bean endpoint

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

Code Block

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.Created by James Strachan
On Wed Oct 01 17:22:33 BST 2008
Using TimTam