Creating A Custom Camel Component
Join the DZone community and get the full member experience.
Join For Freeusing 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... } }
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-ben
This 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
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
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)
Published at DZone with permission of Ben O'Day, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments