0% found this document useful (0 votes)
51 views5 pages

Represents The Camel Runtime System

This document provides information about Camel components, endpoints, messages, exchanges, routes, and processors. It discusses how CamelContext manages components and endpoints. Routes connect endpoints and can be defined using Java DSL or XML. Messages and exchanges move along routes through processors like filters and routers.

Uploaded by

Mahesh Abnave
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views5 pages

Represents The Camel Runtime System

This document provides information about Camel components, endpoints, messages, exchanges, routes, and processors. It discusses how CamelContext manages components and endpoints. Routes connect endpoints and can be defined using Java DSL or XML. Messages and exchanges move along routes through processors like filters and routers.

Uploaded by

Mahesh Abnave
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TODO

1. .process(Processor)
2. .bean()
3. seda
4. supported components
a. timer
b. direct
c. vm
5. supported processors
a. ChoiceProcessor
b. FilterProcessor
6.

Links
1. Enterprise Integration Patterns: http://camel.apache.org/enterprise-integration-
patterns.html
2. Tuts
http://www.mastertheintegration.com/camel/camel-introduction.html
3. http://camel.apache.org/bean-binding.html

CamelContext
A CamelContext object represents the Camel runtime system. You typically have one
CamelContext object in an application. A typical application executes the following steps:

1. Create a CamelContext object.


2. Add endpoints – and possibly Components, which are discussed in Section 4.5
("Components") –
to the CamelContext object.
3. Add routes to the CamelContext object to connect the endpoints.
4. Invoke the start() operation on the CamelContext object. This starts Camel-internal
threads that are used to process the sending, receiving and processing of messages in the
endpoints.
5. Eventually invoke the stop() operation on the CamelContext object. Doing this gracefully
stops all the endpoints and Camel-internal threads.

Note that the CamelContext.start() operation does not block indefinitely. Rather, it starts
threads internal to each Component and Endpoint and then start() returns. Conversely,
CamelContext.stop() waits for all the threads internal to each Endpoint and Component to
terminate and then stop() returns.
CamelTemplate
The CamelTemplate class is a thin wrapper around the CamelContext class. It has methods that
send a Message or Exchange – both discussed in Section 4.6 ("Message and Exchange")) – to
an Endpoint – discussed in Section 4.1 ("Endpoint"). This provides a way to enter messages
into source endpoints,so that the messages will move along routes – discussed in Section 4.8
("Routes, RouteBuilders and Java DSL") – to destination endpoints.

URL, URN, URI and IRI


● URL: "http://...", "ftp://...", "mailto:...".
● URN: have format "urn:<scheme-name>:<unique-identifier>"
● URI: can be URL or URN
● IRI: an internationalized version of a URI. In particular, a URI can contain letters and
digits in the US-ASCII character set, while a IRI can contain those same letters and
digits, and also European accented characters, Greek letters, Chinese ideograms and
so on.

Component
● Can better called as EndpointFactory as it is a factory for creating Endpoint instances.
● For example, if a Camel-based application uses several JMS queues then the
application will create one instance of the JmsComponent class (which implements the
Component interface), and then the application invokes CamelContext.getEndpoint();
internally, the CamelContext object finds the desired Component object (as I will discuss
shortly) and then invokes createEndpoint() on it.
● Another example:
myCamelContext.getEndpoint("pop3://[email protected]?password=m
yPassword");
The parameter to getEndpoint() is a URI. The URI prefix (that is, the part before ":")
specifies the name of a component. Internally, the CamelContext object maintains a
mapping from names of components to Component objects. For the URI given in the
above example, the CamelContext object would probably map the pop3 prefix to an
instance of the MailComponent class. Then the CamelContext object invokes
createEndpoint("pop3://[email protected]?password=myPassword") on
that MailComponent object. The createEndpoint() operation splits the URI into its
component parts and uses these parts to create and configure an Endpoint object.

Components list
1. http://camel.apache.org/timer.html
2. s
Component Types
1. SEDA components: https://camel.apache.org/seda.html
2. Direct compoenents: https://camel.apache.org/direct.html
3. VM components: https://camel.apache.org/vm.html

Adding Components programatically


Component mailComponent = new org.apache.camel.component.mail.MailComponent();
myCamelContext.addComponent("pop3", mailComponent);
myCamelContext.addComponent("imap", mailComponent);
myCamelContext.addComponent("smtp", mailComponent);

Adding Components through properties file and jar


Adding foo component:
1. Write com.example.myproject.FooComponent implementing Component interface
2. "META-INF/services/org/apache/camel/component/foo" (without a ".properties" file
extension) that has a single entry in it called class, the value of which is the fully-scoped
name of your class. For example,
META-INF/services/org/apache/camel/component/foo:
class=com.example.myproject.FooComponent
3. Create a jar file that contains the com.example.myproject.FooComponent class and the
properties file(s), and you add this jar file to your CLASSPATH.
4. Then, when application-level code invokes createEndpoint("foo:...") on a CamelContext
object, Camel will find the "foo"" properties file on the CLASSPATH, get the value of the
class property from that properties file, and use reflection APIs to create an instance of
the specified class.

Endpoint vs Component
Parsing "jms:queue:order", Camel will use the "jms" scheme to lookup the
JmsComponent in the Component registry, which will create a JMS queue Endpoint
named "order". The Endpoint creates Producers and Consumers to handle the message.

Message and Exchange


The Message interface provides an abstraction for a single message, such as a request, reply
or exception message. There are concrete classes that implement the Message interface for
each Camel-supported communications technology. For example, the JmsMessage class
provides a JMS-specific implementation of the Message interface. The public API of the
Message interface provides get- and set-style methods to access the message id, body and
individual header fields of a message.
The Exchange interface provides an abstraction for an exchange of messages, that is, a request
message and its corresponding reply or exception message. In Camel terminology, the request,
reply and exception messages are called in, out and faultmessages. There are concrete
classes that implement the Exchange interface for each Camel-supported communications
technology. For example, the JmsExchange class provides a JMS-specific implementation of
the Exchange interface.

Using getIn & getOut of Exchange


http://camel.apache.org/using-getin-or-getout-methods-on-exchange.html

Processor
The Processor interface represents a class that processes a message. The signature of this
interface is shown below.

package org.apache.camel;public interface Processor


{
void process(Exchange exchange) throws Exception;
}

Notice that the parameter to the process() method is an Exchange rather than a Message. This
provides flexibility. For example, an implementation of this method initially might call
exchange.getIn() to get the input message and process it. If an error occurs during processing
then the method can call exchange.setException().

An application-level developer might implement the Processor interface with a class that
executes some business logic.

Inbuilt Processors
● ChoiceProcessor: implements the message router pattern, that is, it uses a cascading if-
then-else statement to route a message from an input queue to one of several output
queues.
● FilterProcessor: discards messages that do not satisfy a stated predicate

Routes
A route is the step-by-step movement of a Message from an input queue, through arbitrary
types of decision making (such as filters and routers) to a destination queue (if any).
Two ways to specify routes.
● Using Xml
● Using Java DSL
Java DSL Example
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("queue:a").filter(header("foo").isEqualTo("bar")).to("queue:b");
from("queue:c").choice()
.when(header("foo").isEqualTo("bar")).to("queue:d")
.when(header("foo").isEqualTo("cheese")).to("queue:e")
.otherwise().to("queue:f");
}
};
CamelContext myCamelContext = new DefaultCamelContext();
myCamelContext.addRoutes(builder);

The first line in the above example creates an object which is an instance of an anonymous
subclass of RouteBuilder with the specified configure() method.

● The CamelContext.addRoutes(RouterBuilder builder) method invokes


builder.setContext(this) – so the RouteBuilder object knows which CamelContext
object it is associated with – and then invokes builder.configure().
CamelContext.addRoute(routeBuilder)
{
// routeBuilder.CamelContext = this
// routeBuilder.configure()
}
● The body of configure() invokes methods such as from(), filter(), choice(), when(),
isEqualTo(), otherwise() and to().
● The RouteBuilder.from(String uri) method invokes getEndpoint(uri) on the
CamelContext associated with the RouteBuilder object to get the specified Endpoint
and then puts a FromBuilder "wrapper" around this Endpoint.
RouteBuilder.from(uri)
{
//fromBuilder = CamelContext.getEndPoint(uri)
}
● The FromBuilder.filter(Predicate predicate) method creates a FilterProcessor object
for the Predicate (that is, condition) object built from the header("foo").isEqualTo("bar")
expression. In this way, these operations incrementally build up a Route object (with a
RouteBuilder wrapper around it) and add it to the CamelContext object associated with
the RouteBuilder.

You might also like