...
@Consume
Info | ||
---|---|---|
| ||
|
Use 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
parameter of the annotation defines the Camel Endpoint from which to consume from.
e.g. lets invoke For example, the following code invokes the onCheese()
method with the String body
of the inbound JMS message from ActiveMQ on the cheese
queue; this will use the . 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).
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. What this does is basically create a route that looks kinda like thisThis is equivalent to the following explicit route written in the Java DSL:
Code Block |
---|
from(uri).bean(theBean, "methodName"); |
Warning | ||
---|---|---|
| ||
When you use Using more than 1 CamelContext you might end up with each of them one |
Using context option to apply only a certain CamelContext
Available as As of Camel 2.0
See the warning above.You can use the context
option to specify which CamelContext the consumer should only apply for. For example:x, the context
option may be used to specify a specific CamelContext.
Code Block |
---|
@Consume(uri="activemq:cheese", context="camel-1") public void onCheese(String name) { |
The above consumer above will only be created for the CamelContext that have has the context id = camel-1
. You set this id in the XML tag as follows:
Code Block | ||||
---|---|---|---|---|
| ||||
<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 then cause Camel to look up myBean
in the Registry and find the bean and invoke the given bean name. (You can omit invoke methodName
. Optionally, the method name and have Camel 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
...
.
You can always use the bean endpoint
Code Block |
---|
from(uri).to("bean:myBean/methodName"); |
Which approach to use?
Using Use the @MessageDriven/@Consume annotations are simpler when you are creating a simple route with a single well-defined input URI. However if you require If more complex routes are required or the same bean method needs to be invoked from many places then please use the routing DSL as shown above, consider using explicit routes.