Bean Integration (Route Definition)

Bean integration provides a general-purpose method for processing messages using arbitrary POJO objects within our routes. Through the insertion of a bean reference into a route, we can call a method on a POJO Java object, which will then have the ability to access and modify the incoming exchange. The mechanism that maps an exchange’s contents to the parameters and return values of a bean method is known as parameter binding. Parameter binding can use any combination of the following approaches in order to initialize a method’s parameters:

  • Conventional method signatures  —  If the method signature conforms to certain conventions, the parameter binding can use Java reflection to determine what parameters to pass.

    Annotations and dependency injection  —  For a more flexible binding mechanism, we can employ Java annotations to specify what to inject into the method’s arguments. This dependency injection mechanism relies on Spring 2.5 component scanning. If you are deploying your Apache Camel application into a Spring container, the dependency injection mechanism will work automatically.

    Explicitly specified parameters  —  You can specify parameters explicitly (either as constants or using the Simple language), at the point where the bean is invoked.

Bean registry

Beans are made accessible and visible to us through a bean registry, this is a service that enables you to lookup beans via the class name or the bean ID as a key. The way that you create an entry in the bean registry depends on the underlying framework (ie. Java, Spring, etc). All Registry entries are created implicitly (ie. instantiate a Spring bean in a Spring XML file).

Accessing a bean created in Java

To process exchange objects using a Java bean (which is a plain old Java object or POJO), use the bean() processor, which binds the inbound exchange to a method on the Java object. For example, to process inbound exchanges using the class, MyBeanProcessor, define a route like the following:

from("file:data/inbound") .bean(MyBeanProcessor.class, "processBody") .to("file:data/outbound");

 

<route> <from uri="file:data/inbound"/> <bean ref="MyBeanProcessor" method="processBody"/> <to uri="file:data/outbound"/> </route>

Method Signature to Process Message

If we need to implement a method on a bean that will access or update an incoming message body, then we must define a method signature that takes a single String argument and returns a String value. For example:

package com.acme; public class MyBeanProcessor { public String processBody(String body) { // Do whatever you like to 'body'... return newBody; } }

Method Signature tp Process Exchange

For even greater flexibility, we can implement a bean method that will access the incoming exchange. This will enable us to access or modify all headers, bodies, and exchange properties.

Spring Bean from Spring XML

Rather than creating a bean instance in Java, we can create a bean instance using Spring XML. In fact, this is the only feasible approach if you are defining your routes in XML. To define a bean in XML, use the standard Spring bean element.

Where the processor invokes the MyBeanProcessor.processBody() method on the bean instance. We can also invoke the bean from within a Spring XML route, using the Camel schema’s bean element.

Spring Bean using Injection

Alternatively, you can reference the Spring bean by injection, using the @BeanInject annotation:

NOTE: If we omit the bean ID from the @BeanInject annotation, then it will perform the lookup within the bean registry based on the type. However, this only works if there is just a single bean of the given type.

Basic Annotations

Annotation

Meaning

Parameters

Annotation

Meaning

Parameters

@Attachments

Binds to a list of attachments

 

@Body

Binds to an inbound message body.

 

@Header

Binds to an inbound message header.

String name of the header.

@Headers

Binds to a java.util.Map of the inbound message headers.

 

@OutHeaders

Binds to a java.util.Map of the outbound message headers.

 

@Property

Binds to a named exchange property.

String name of the property.

@Properties

Binds to a java.util.Map of the exchange properties.

 

For example, the following example will show the use of basic annotations to inject message data into the processExchange() method arguments.

Expression Language Annotations

Another option is the use of the expression language annotations, they provide a powerful and flexible method for injecting message data into a bean method’s arguments. With these annotations, we can invoke an arbitrary script, written in your chosen scripting language, to extract data from an inbound exchange and then inject the data into a method argument.

Annotation

Description

Annotation

Description

@Bean

Injects a Bean expression.

@Constant

Injects a Constant expression

@EL

Injects an EL expression.

@Groovy

Injects a Groovy expression.

@Header

Injects a Header expression.

@JavaScript

Injects a JavaScript expression.

@OGNL

Injects an OGNL expression.

@PHP

Injects a PHP expression.

@Python

Injects a Python expression.

@Ruby

Injects a Ruby expression.

@Simple

Injects a Simple expression.

@XPath

Injects an XPath expression.

@XQuery

Injects an XQuery expression.

For example, the following example shows how to use @XPath annotation to extract a username and a password from the body of an incoming message in XML format:

 

Invoke Static Methods

The Bean integration process also has the capacity to invoke static methods without creating an instance of the associated class. For example, consider the following that defines the static method, changeSomething():

Use bean integration to invoke the static changeSomething method, as follows: