Creating A Custom Camel Component
While Camel supports an ever growing number of components, you might
have a need to create a custom component. This could be to either
promote reuse across projects, customize an existing component or
provide a simplified interface to an existing system. Whatever the
reason, here is an overview of the options that are available within the
Camel framework...
first, consider just creating a Bean or Processor
Before you jump in and create a component, consider just creating a simple class to handle your custom logic. Behind the scenes, all components are just Processors with a bunch of lifecycle support around them. Beans and Processors are simple, streamlined and easy to manage.
using a Bean...
create a custom component
If you decide to go down this route, you should start by start by using a Maven archetype to stub out a new component project for you.
When you define a route that uses your new component as a consumer, like this
It does the following:
Producer Lifecycle
When you define a route that uses your new component as a producer, like this
Other Resources:
http://camel.apache.org/writing-components.html http://fusesource.com/docs/router/2.8/prog_guide/Component.html
Published at DZone with permission of its author, Ben O' Day. (source)first, consider just creating a Bean or Processor
Before you jump in and create a component, consider just creating a simple class to handle your custom logic. Behind the scenes, all components are just Processors with a bunch of lifecycle support around them. Beans and Processors are simple, streamlined and easy to manage.
using a Bean...
from(uri).bean(MyBean.class);
...
public class MyBean {
public void doSomething(Exchange exchange) {
//do something...
}
} using a Processor...from(uri).process(new MyProcessor());
...
public class MyProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
//do something...
}
}
create a custom component
If you decide to go down this route, you should start by start by using a Maven archetype to stub out a new component project for you.
mvn archetype:generate
-DarchetypeGroupId=org.apache.camel.archetypes
-DarchetypeArtifactId=camel-archetype-component
-DarchetypeVersion=2.7
-DarchetypeRepository=https://repository.apache.org/content/groups/snapshots-group
-DgroupId=org.apache.camel.component
-DartifactId=camel-benThis will create a new Maven component project that contains an example HelloWorld component as seen here...
- HelloWorldComponent
- endpoint factory which implements createEndpoint()
- HelloWorldEndpoint
- producer/consumer factory which implements createConsumer(), createProducer(), createExchange()
- HelloWorldConsumer
- acts as a service to consumes request at the start of a route
- HelloWorldProducer
- acts as a service consumer to dispatch outgoing requests and receive incoming replies
- Exchange
- encapsulate the in/out message payloads and meta data about the data flowing between endpoints
- Message
- represent the message payload
- their is an IN and OUT message for each exchange
Consumer Lifecycle
When you define a route that uses your new component as a consumer, like this
from("helloworld:foo").to("log:result");
It does the following:
- creates a HelloWorldComponent instance (one per CamelContext)
- calls HelloWorldComponent createEndpoint() with the given URI
- creates a HelloWorldEndpoint instance (one per route reference)
- creates a HelloWorldConsumer instance (one per route reference)
- register the route with the CamelContext and call doStart() on the Consumer
- consumers will then start in one of the following modes:
- event driven - wait for message to trigger route
- polling consumer - manually polls a resource for events
- scheduled polling consumer - events automatically generated by timer
- custom threading - custom management of the event lifecyle
Producer Lifecycle
When you define a route that uses your new component as a producer, like this
from("direct:start").to("helloworld:foo");It does the following:
- creates a HelloWorldComponent instance (one per CamelContext)
- calls HelloWorldComponent createEndpoint() with the given URI
- creates a HelloWorldEndpoint instance (one per route reference)
- creates a HelloWorldProducer instance (one per route reference)
- register the route with the CamelContext and start the route consumer
- the Producer's process(Exchange) method is then executed
- generally, this will decorate the Exchange by interfacing with some external resource (file, jms, database, etc)
Other Resources:
http://camel.apache.org/writing-components.html http://fusesource.com/docs/router/2.8/prog_guide/Component.html
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)
Tags:




