Using Camel components
When writing integration code, you will inevitably have to work directly with libraries that deal with the technology being integrated. The details of invoking web services, consuming or sending files to FTP servers, or messaging over JMS often take up a substantial proportion of development time on a project. Camel abstracts away the repeated details of consuming from or producing to these "transports", by encapsulating this interaction within a Component.
Camel provides a rich set of components, which abstract away this plumbing code from you. These allow you to focus your energies on the business logic of your integration without worrying about the finer details of the transport.
This recipe will show you the basic steps of associating a Camel Component for a given technology with your integration routing logic.
Getting ready
To make use of a component, you will need to make sure that the component's library is included in your project. The camel-core
library provides a set of fundamental components to get you started underneath the org.apache.camel.component
package.
For integration with any other technologies, your first stop should be the component listing on the Camel website (http://camel.apache.org/components.html). Once you have found the technology that you are looking for, add the JAR dependency to your project, ensuring that the version matches that of the camel-core
library you are using. For example, to use the Camel FTP component within your Camel route, you need to add the camel-ftp
dependency to your POM:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ftp</artifactId> <version>${camel-version}</version> </dependency>
The ${camel-version}
property is defined once in the Maven POM.
The Java code for this recipe is located in the org.camelcookbook.structuringroutes.simple
package. The Spring XML files are located under src/main/resources/META-INF/spring
and prefixed with simplespring
.
How to do it...
In order to use a Camel component you need to instantiate and register the component, and then reference it from your Camel route as per the following steps:
- If working within a Spring application, you use a Component by instantiating it as a
bean
and give it a meaningful name (id
); the Component is automatically visible to Camel:<bean id="mylogger" class="org.apache.camel.component.LogComponent"/>
Alternatively, in a non-Spring Java application, you register a Component by instantiating it and then adding it to the
CamelContext
before starting it:CamelContext context = new DefaultCamelContext(); camelContext.addComponent("mylogger", new LogComponent()); // add routes here context.start();
- To use the component as an endpoint in a
to(...)
orfrom(...)
statement, refer to the name that you assigned it within the scheme part of the endpoint URI:.to("mylogger:insideTheRoute?showHeaders=true")
How it works...
To understand exactly what happens when we use an endpoint URI, it is easiest to take a brief look under the covers at the classes that Camel uses within its component framework.
A Component is a factory for creating Endpoints that can act as message Consumers, or Producers, or both. An implementation will typically have bean properties on it that will apply to the transport as a whole. For example, the JMS Component requires that a ConnectionFactory
be set on it, in order to make use of the same message broker for all JMS communication:
<bean id="myFavouriteMQ" class="org.apache.camel.component.jms.JmsComponent"> <property name="connectionFactory" ref="myConnectionFactory"/> </bean>
All Components implement a method used to parse the endpoint URI:
Endpoint createEndpoint(String uri) throws Exception;
An Endpoint can be thought of as an address that is specific to the component technology–it is instantiated from the URI by the framework when the route using it is started. The scheme portion of the URI, the part before the first colon (:
), identifies the component implementation being used.
Continuing the JMS example, given that the Component already knows where a message broker is, the Endpoint itself describes which queues or topics a message should be sent to or received from, and how:
myFavouriteMQ:someQueue?concurrentConsumers=5
The portion of the URI after the scheme is specific to the endpoint technology. The URI properties themselves, such as concurrentConsumers
, correspond to bean properties on the endpoint implementation for that technology, and are set using introspection. If you were to take a look inside the JMS Component library, you would see a JmsEndpoint
object with a setConcurrentConsumers(int consumers)
method.
You should always refer back to the applicable Camel component page for a full list of properties that can be used.
The Endpoint is also a factory. Camel uses it for creating Producers and Consumers, depending on the context of where the URI is used. The following factory methods are defined on the interface:
Producer createProducer() throws Exception; Consumer createConsumer(Processor processor) throws Exception;
These classes handle the heavy lifting of talking to the underlying technology.
If the endpoint URI is used in a from(...)
statement, Camel will create a Consumer
. If it is used in a to(...)
block, will create a Producer
.
There's more...
The same component can be instantiated multiple times with different IDs. For example, two JMS components might be used when writing routing logic to bridge message brokers from different vendors.
See also
- Camel Components: http://camel.apache.org/components.html