Escolar Documentos
Profissional Documentos
Cultura Documentos
The Enterprise Resource Planning (ERP) vendors have had some success at
creating larger-than-ever business applications. The reality, though, is that
even the heavyweights like SAP, Oracle, Peoplesoft, and the like perform
only a fraction of the business functions required in a typical enterprise. We
can see this easily by the fact that ERP systems are one of the most popular
integration points in today's enterprises.
Over time, developers have overcome these challenges with four main
approaches:
1. File Transfer --One application writes a file that another later reads. The
applications need to agree on the filename and location, the format of
the file, the timing of when it will be written and read, and who will
delete the file.
2. Shared Database --Multiple applications share the same database
schema, located in a single physical database. Because there is no
duplicate data storage, no data has to be transferred from one
application to the other.
3. Remote Procedure Invocation --One application exposes some of its
functionality so that it can be accessed remotely by other applications
as a remote procedure. The communication occurs in real time and
synchronously.
4. Messaging --One application publishes a message to a common
mes- sage channel. Other applications can read the message from the
channel at a later time. The applications must agree on a channel as
well as on the for- mat of the message. The communication is
asynchronous.
m e d ia tio n _ ro u t in g .m p 4
What a Java Integration Framework Looks Like
Supports all concepts presented previously
Simplifies the following:
◦ How endpoints are exposed
◦ How messages are processed
◦ How applications are integrated
Also supports:
◦ Enterprise integration patterns (EIPs)
◦ A standards-based application program interface (API)
A domain specific language (DSL)
Enterprise Integration Patterns
Provide solutions to application integration problems
Like Java patterns used by Java developers for Java application design
problems
Provide vocabulary and grammar that analysts, architects, and
developers can use to design solutions for complex integration projects
Book by Gregor Hohpe and Bobby Woolf covers 65 patterns, from
simple to complex, including:
◦ Splitter
◦ Content-based router
◦ Aggregator
◦ Filter
DSL
The DSL is not a new programming language. It is a vocabulary used to
represent and implement EIPs.
Most of the time, a DSL is expressed using a fluent API or fluent
interface.
DSL is mini-language designed for a special purpose.
Routes are described using DSL languages such as Java, XML (Spring,
Blueprint), Scala, or Groovy
Wordings and vocabularies are similar to English
The model used relies on the same Java classes
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(). 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. 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.
In particular, Java DSL is less verbose than its XML counterpart. In addition,
many integrated development environments (IDEs) provide an auto-
completion feature in their editors. This auto-completion feature works with
Java DSL, thereby making it easier for developers to write Java DSL.
Camel Framework
Open source projects use Java and various design frameworks for
application integration:
◦ Apache Camel
◦ Spring Integration
◦ Mule Software
Apache Camel project
◦ Began in 2007
◦ Only framework that implements and extends all EIPs using an API,
including:
▪ RecipientList
▪ Throttler, delayer
▪ Loadbalancer
◦ Deploys standalone or inside any Java container used with Red Hat®
JBoss® Fuse (JBoss Fuse), Red Hat® JBoss® Enterprise Application
Platform (JBoss EAP), Red Hat® JBoss® Web Server, Apache Tomcat,
and Wildfly
◦ Works directly with most transport or messaging models to rapidly
integrate existing services and applications
Reference : http://camel.apache.org/maven/current/
Summary
This module introduced you to the following key integration concepts:
Messaging and the decoupling of the producer and consumer
How to simplify integration projects
EIP and DSL
Apache Camel framework
Core Engine
Messaging & Payload
A Camel Exchange object is a wrapper that encapsulates a message and its
metadata. It consists of the following elements:
Body
Headers and Properties
Attachment (optional)
Exception
Exchange ID
p ip e lin e .m p 4
Processor
Processors encapsulate logic that manipulates an Exchange.
Route definitions start with from() and are followed by a list of
.to()processors: from().to().to()
Route definitions create two lists:
◦ Inputs = list of from()
◦ Outputs = list of .to()
The first processor acts as a Consumer and initiates Exchange creation.
(Optional) The pipeline can contain processor(s) acting as a producer.
Endpoint
The term endpoint is often used when talking about inter-process
communication. For example, in client-server communication, the client is
one endpoint and the server is the other endpoint. Depending on the
context, an endpoint might refer to an address, such as a host:port pair for
TCP-based communication, or it might refer to a software entity that is
contactable at that address.
Components
Component is confusing terminology; EndpointFactory would have been
more appropriate because a Component 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 the createEndpoint() operation on this JmsComponent object
several times. Each invocation of JmsComponent.createEndpoint() creates
an instance of the JmsEndpoint class (which implements the Endpoint
interface). Actually, application-level code does not invoke
Component.createEndpoint() directly. Instead, application-level code
normally invokes CamelContext.getEndpoint(); internally, the CamelContext
object finds the desired Component object (as I will discuss shortly) and
then invokes createEndpoint() on it.
Consider the following code.
myCamelContext.getEndpoint("pop3://john.smith@mailserv.example.com?
password=myPassword");
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://john.smith@mailserv.example.com?
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.
In the previous paragraph, I mentioned that a CamelContext object
maintains a mapping from component names to Component objects. This
raises the question of how this map is populated with named Component
objects. There are two ways of populating the map. The first way is for
application-level code to invoke CamelContext.addComponent(String
componentName, Component component). The example below shows a
single MailComponent object being registered in the map under 3 different
names.
The second (and preferred) way to populate the map of named Component
objects in the CamelContext object is to let the CamelContext object
perform lazy initialization.
CamelContext
A CamelContext object represents the Camel runtime system.
The CamelContext represents a single Camel routing rulebase.
Interface used to represent the context used to configure routes and the
policies to use during message exchanges between endpoints.
The context offers the following methods to control the lifecycle:
start() - to start (important: the start method is not blocked, see more details here)
stop() - to shutdown (will stop all routes/components/endpoints etc and clear internal state/cache)
SuspendableService.suspend() - to pause routing messages
SuspendableService.resume() - to resume after a suspend
Camel uses a Java based Routing Domain Specific Language (DSL) or an Xml
Configuration to configure routing and mediation rules which are added to a
CamelContext to implement the various Enterprise Integration Patterns.
CamelTemplate
Camel used to have a class called CamelClient, but this was renamed to be
CamelTemplate to be similar to a naming convention used in some other
open-source projects, such as the TransactionTemplate and JmsTemplate
classes in Spring.
The CamelTemplate class is a thin wrapper around the CamelContext class.
It has methods that send a Message or Exchange – to an Endpoint .This
provides a way to enter messages into source endpoints, so that the
messages will move along routes – to destination endpoints.
URI-Scheme
Endpoints are defined using this URI syntax: “scheme://contextPath?
key=value&key=value …”
Scheme is the name of a component
Camel checks the classpath to discover the component to be used
c h a rle s _ in o u t.m p 4
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 messge.
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 fault messages.
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. The public API of the Exchange interface is quite limited. This is
intentional, and it is expected that each class that implements this interface
will provide its own technology-specific operations.
Application-level programmers rarely access the Exchange interface (or
classes that implement it) directly. However, many classes in Camel are
generic types that are instantiated on (a class that implements) Exchange.
Because of this, the Exchange interface appears a lot in the generic
signatures of classes and methods.
Processor
The Processor interface represents a class that processes a message.
package org.apache.camel;
public interface Processor {
void process(Exchange exchange) throws Exception;
}
The other way is through what Camel calls a Java DSL (domain-specific
language).
Camel supports the definition of routing rules using a Java DSL (domain
specific language) which avoids the need for cumbersome XML using a
RouteBuilder.
Filters
You can combine simple routes with filters.
Choices
With a choice you provide a list of predicates and outcomes along with an
optional default otherwise clause.
Camel Container
CamelContext is the runtime environment
Registry for endpoints, routes
Acts as a server runtime
Can be deployed in any kind of Java Container: JBoss Fuse/JBoss A-MQ,
JBoss EAP, Wildfly, Apache Tomcat, Apache ActiveMQ or in standalone
CamelContext is created programmatically in Java, using Dependency
injection (Spring, Blueprint) or using Java Annotation (CDI)
Route Registration - JAVA DSL
The RouteBuilder is a base class which is derived from to create routing
rules using the DSL. Instances of RouteBuilder are then added to the
CamelContext.
Route Registration - XML DSL
Camel uses a simple lifecycle interface called Service which has a single
start() and stop() method.
Various classes implement Service such as CamelContext along with a
number of Component and Endpoint classes.
When you use Camel you typically have to start the CamelContext which
will start all the various components and endpoints and activate the routing
rules until the context is stopped again.
Routes lifecycle
Routes in Camel have the following operations to control its lifecycle
start
stop
suspend
resume
remove (previously named shutdown)
The remove operation will remove the route, for example in JMX the route
will then be unregistered and its gone. So only use remove if you really want
to remove the route. The route must have been stopped before you can
remove.
The start and resume operations in JMX checks the state beforehand. So if a
route is stopped and you click resume, it will know to invoke start. And
likewise if a route has been suspended and you click start it knows to
resume instead. This makes management a bit easier.
Summary
This module introduced you to the following topics:
Apache Camel core engine
Key concepts of the Apache Camel framework
Container where the integration project is deployed
Development of an Apache Camel project in JBoss Developer Studio