Routing messages directly to a Java method

When you just want to send the message received from an endpoint directly to one of your Java methods, Camel provides a Bean Integration capability called POJO Consuming. POJO Consuming allows you to annotate a Java method with the Camel URI for the endpoint that you want to receive messages from. Messages will be passed directly to that Java method.

Through an example, this recipe will demonstrate to you how to annotate a Java method to act as a JMS (or WS, or TCP/IP, or any other Camel consuming endpoint) listener, in the same way as a Java EE Message Driven Bean (MDB). It will also provide you with the ability to run the code inside an environment as simple as a standalone Java process, with no extra container required.

This recipe will show you how to annotate your Java code so that it will be called with messages received from any specified Camel endpoint.

Getting ready

The Java code for this recipe is located in the org.camelcookbook.extend.consume package. The Spring XML files are located under src/main/resources/META-INF/spring and prefixed with consume.

The included example uses an embedded (in-process) ActiveMQ instance to act as a JMS provider for its simple JMS message consuming method. In practice, you would normally connect Camel to an external messaging infrastructure.

How to do it...

Camel's POJO Consuming requires use of the @Consume annotation with the URI of the Camel endpoint you want to consume messages from. This example shows the creation of a JMS request/response message consumer. Camel will send the response returned from the onMyMessage(..) Java method as a message into the JMS queue that is specified in the JMSReplyTo header of the request message; Camel handles all of this for you.

import org.apache.camel.Consume;

public class ConsumeMdb {
  @Consume(uri="activemq:queue:sayhello")
  public String onMyMessage(String message) {
    return "Hello " + message;
  }
}

In order to use the ConsumeMdb class in Camel, it must be defined as a bean in the same Spring application context as the Camel context.

<beans xmlns="http://www.springframework.org/schema/beans" ...>
  <bean id="activemq"
        class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="brokerURL" 
              value="vm://myBroker?create=false"/>
  </bean>

  <bean class="org.camelcookbook.extend.consume.ConsumeMdb"/>

  <camelContext xmlns="http://camel.apache.org/schema/spring"/>
</beans>

How it works...

Camel's POJO Consuming uses an @Consume annotation on a method for letting Camel know it needs to create an endpoint consumer using the specified endpoint URI.

public class MyBean {
  @Consume(uri="someEndpoint")
  public String myMethod(String message) {
    //...
  }
}

At runtime, this is effectively equivalent to having explicitly created the following route in the Java DSL:

from("someEndpoint")
  .bean(MyBean.class, "myMethod");

Any exceptions thrown from your Java method will be handled in the same way as an exception thrown from any endpoint or processor; the route specified error handlers and exception handers would catch it. See Chapter 7, Error Handling and Compensation, for more details.

There's more...

Camel will handle the result of the method call in a similar fashion regardless of whether the method was called through POJO Consuming, or via using the Bean DSL statement as shown in the preceding equivalent route. If the method returns void, then it is equivalent to having called an InOnly processor. If the method returns something, then it is equivalent to having called an InOut processor, and the return value will be used as the new message body.

Camel uses its data type converters to automatically convert the message body to the type specified in the parameters of the method. A method parameter can also be of type Exchange, in which case the method would have full access to all of the data within the Camel exchange. See Camel's documentation on Bean Binding for more details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.45.5