Essential Camel Components
-
Upload
ceposta -
Category
Technology
-
view
2.510 -
download
0
description
Transcript of Essential Camel Components
Apache Camel Essential Components
Presenter: Christian PostaTitle: Senior ConsultantDate: January 23, 2013
2
Agenda
FuseSource/Red Hat What is Camel? Camel Intro Components More Information Q & A
3
FuseSource – the leading open source integration and messaging vendor...now a part of Red Hat!
FuseSource – a company built on success
Founded in 2005
Commercial Support Camel, ActiveMQ, ServiceMix, CXF
Enterprise Products
Certified, hardened, based on open source
Production Support, Training, Consulting
Proven track record in mission-critical apps
Acquired by Red Hat in September 2012
Two open source leaders in the market:comprehensive middleware solutions
Share a maniacal focus on community
Offices in almost all time zones and over 30 countries
4
Same Great Products and Services – and More
FuseSource / Red Hat will continue to offer and support all FuseSource products
Continued training, consulting, and subscriptions – more coverage
fusesource.com transitioning to redhat.com
JIRAS → redhat.com Community – need to re-register at redhat.com fusesource.com - maintained at least through 2013
5
About Me
Christian Posta Senior Consultant Committer at Apache on ActiveMQ, Apollo Email: [email protected]
[email protected] Blog: http://www.christianposta.com/blog Twitter: @christianposta Google+
6
What is Camel?
7
What isn’t Apache Camel
• Enterprise Service Bus (ESB)
• Container
• ServiceMix/FuseESB
• OSGi container
• Tomcat, JBoss, Geronimo
• Commercial App Servers
• Proprietary, closed source
8
Integration
Use Camel to Integrate disparate systems that talk different protocols and data formats.
9
Integrate??
• Integration is Hard!
• Different system vintages
• Mainframe, EAI Hub, MOM, EJB, Web Services
• Evolving business processes
• Systems must work together
• File exchange, Shared Database, Remote Procedure Call (RPC), Messaging
10
Why integration is hard…
• Platforms
• Protocols
• Data Formats
• Timing
• Organizational mismatch
• Communication
Hard, but not impossible, definitely not new…
11
Patterns!
• Enterprise Integration Patterns (EIP)
• Specific context
• Forces at work
• Concrete solution
• Guidance for solutions to your problems
• 65 patterns• Content Based Router
• Splitter
• Aggregator
• Filter
12
What is Apache Camel?
• Lightweight Integration Framework
• Open Source! (Apache Software Foundation)
• Routing and Mediation (like an ESB?)
• Enterprise Integration Patterns
• Components
• Domain Specific Language
• Runs in any container (or stand alone)
13
Camel Intro
14
Quick Example
File System Message Oriented Middleware
15
Quick Example
From A Send to BFilter
message
16
Quick Example
from(A) to(B)filter(predicate)
17
Quick Example
from(A) .to(B).filter(isWidget)
18
Quick Example
isWidget = xpath(“/quote/product = ‘widget’”);
from(A) .filter(isWidget). to(B)
19
Quick Example
public class MyExampleRouteBuilder extends RouteBuilder {
@Override public void configure() throws Exception {
Predicate isWidget = xpath("/quote/product = ‘widget’");
from(“file:quote/location”).filter(isWidget).to(“jms:quote”);
}}
20
Domain Specific Language
• Domain specific (integration)
• Used to build and describe Camel Routes
• Embedded within a general programming language
• Java, Spring XML, Scala, Groovy
• Take advantage of existing tools
• Fluent builders (builder pattern…)• from(“..”).enrich(“…”).filter(“..”).to(“…”);
21
Pipes and Filters Architecture
• Step by Step
• Complex processing
• Flexible
• Testing
• Reuse
22
Camel Routes
• Defined in Java, XML, Scala, Groovy
• Step by step processing of a message:
• Consumer – Listen for incoming message
• Zero or more “filters” or Processors
• Producer – Send outgoing message
• Number of processing filters, or “Processors” in Camel-speak
• EIPs
• Tranform, redirect, enrich
23
Domain Specific Language
• Example Java DSL
from("file:src/data?noop=true”)
.choice()
.when(xpath("/person/city = 'London'"))
.to("file:target/messages/uk »)
.otherwise()
.to("file:target/messages/others");
24
Domain Specific Language
• Example Spring XML DSL
<route> <from uri="file:src/data?noop=true” /> <choice> <when> <xpath>/person/city = 'London'</xpath>
<to uri="file:target/messages/uk” /> </when>
<otherwise> <to uri="file:target/messages/others" /> </otherwise> </choice></route>
25
Components
• What is “file:src/data?noop=true” ??
• Prepackaged bits of code
• Highly configurable
• Used to build “Adapters” to existing systems
• Don’t reinvent the wheel and end up with a box
26
Components…
• URI format:
• scheme:localPart[?options]
• scheme: identifies the “component”
• localPart: specific to the component
• options: is a list of name-value pairs
• Creates endpoints based on configuration
• Route endpoint “factories”
• Integrate with Camel Routes by creating producer/consumer endpoints
27
Components…
• ActiveMQ, Websphere, Weblogic (JMS)
• AMQP
• ATOM feeds
• AWS (Amazon Web Services)
• Bean
• Cache (EHCache)
• CXF (JAX-WS, JAX-RS)
• EJB
• Drools
• File
• FTP
• Google App Engine
• GMail
• HTTP
• IRC
• jclouds
• JDBC
• Jetty
• MQTT
• MyBatis
• JPA
• Spring Integration
• Spring Web Services
http://camel.apache.org/components.html
To see list of all
components!!
28
Essential Components
29
Essential Components
• File (camel-core)
• Bean (camel-core)
• Log (camel-core)
• JMS (camel-jms)
• CXF (camel-cxf)
• Mock (camel-core)
http://refcardz.dzone.com/refcardz/essential-camel-components
30
Essential Components
• File (camel-core)
• Bean (camel-core)
• Log (camel-core)
• JMS (camel-jms)
• CXF (camel-cxf)
• Mock (camel-core)
31
File Component (camel-core)
• File integrations still exist!
• Legacy systems
• Batch jobs
• Many third party libraries for interfacing with the filesystem
• Why write app-specific file-system code for every app?
32
File Component (camel-core)
• How long would it take you to implement this:
• Periodically polls a predefined location
• Picks up files
• Sends them to a JMS queue?
• How about…
• URI format: file:path[?options]
from(“file:/location/path?move=.processed”).to(“jms:queueName”)
33
File Component (camel-core)
• Reads and writes file to the file system
• Endpoint URIs
• UNIX, absolute path file:/directoryPath[?options]
• Windows absolute path file:C://directoryPath[?options]
• Relative path file:directoryPath[?options]
34
File Component (camel-core)
• Default behavior
1. Read all files in directory
2. Create a new message
3. Process message through route
• Filenames starting with ‘.’ character are ignored
• File component is one of the most flexible components with many config options
35
File Component configuration options
Option Default Description
delay 500ms Time to delay between polling
initialDelay 1000ms How long before polling starts
delete False Whether or not to delete the file after it’s been processed
doneFileName null This file must exist before Camel will process the files in the directory
fileName null Explicit filename to poll. Only processes if file exists
include null A {regex} that can specify patterns of files to process
exclude null A {regex} specifying patterns of files to ignore
preMove null Move files to sub-directory before processing
readLock markerFile Strategy for how to exclusively lock a file before processing
See http://camel.apache.org/file2.html for more
36
File Component things to watch out
• Files are locked until route completes
• Files starting with “.” are ignored
• By default, when a file has been processed, Camel will move the file to .camel file unless a move=<location> option specified
• Moving/Deleting files will happen after routing
37
File Examples
from("file://inbox?preMove=.inprogress&move=.done")
.to(“activemq:queue:myQueue");
from("direct:report")
.to("file://target/reports/?fileName=report.txt");
from("file://data?exclude=_.*")
.to("bean:handleData?method=processData");
from("file://data?sortBy=date:file:yyyyMMdd;file:name")
.to(“direct:sorted");
See http://camel.apache.org/direct.html for info on direct component
38
Essential Components
• File (camel-core)
• Bean (camel-core)
• Log (camel-core)
• JMS (camel-jms)
• CXF (camel-cxf)
• Mock (camel-core)
39
Bean Component (camel-core)
• Implements Service Activator EIP• http://www.eaipatterns.com/MessagingAdapter.html
• http://camel.apache.org/bean.html
• Allows connecting existing Java Bean/POJO logic to Camel route
Camel Route
40
Bean Component (camel-core)
• Invoke method on Java object to process incoming message
• Endpoint URI format: bean:beanID[?options]• from(“direct:incoming”).to(“bean:enrichService”).to(“file:data/output”)
• Define your beans in the Spring Context as you would any bean
• <bean id="enrichService" class="com.christianposta.refcard.CreditService" />
• Binds message and/or headers to bean method parameters
41
Bean Component (camel-core)
• Call using to(..) • from(“direct:incoming”).to(“bean:enrichService?method=getCreditScore”)
• from(“direct:incoming”).to(“bean:enrichService”)
• Call using .bean(…)• from(“direct:incoming”).bean(CreditService.class, “getCreditScore”)
• from(“direct:incoming”).bean(new CreditService(), “getCreditScore”)
• Call using .beanRef(…)• from(“direct:incoming”).beanRef(“enrichService”, “getCreditScore”)
42
Bean Examples
public class CreditService {
public int getCreditScore(@XPath("/Borrower/BorrowerId") long borrowerId) { … body of impl here …
}}
from(“jms:incoming”).to(“bean:enrichService?method=getCreditScore”).to(“jms:outgoing”);
<bean id="enrichService” class="com.christianposta.refcard.CreditService" />
Custom class
Camel route using bean component
Spring bean definition
43
Bean Component: How are methods matched?
• By configuration: ?method=getCreditScore
• Single method in class
• Message Header named CamelBeanMethodName
• Method with only one parameter
• @Handler annotation
• By type (following internal algorithm)
44
Bean Component: How are parameters matched?
• Automatically binds method parameters
• public void getCreditScore(Exchange exchange)
• public void getCreditScore(@Header(“customerId”) String customerId)
• public void getCreditScore(Message message)
• public void getCreditScore(byte[] bytes)
• Expression languages (simple, UEL, OGNL, groovy)
• TypeConverters if possible to bind parameters
• Error if cannot convert exchange to param type
• See http://camel.apache.org/bean-binding.html
45
Essential Components
• File (camel-core)
• Bean (camel-core)
• Log (camel-core)
• JMS (camel-jms)
• CXF (camel-cxf)
• Mock (camel-core)
46
Log Component (camel-core)
• Always use logging in your route!
• Camel uses SLF4J
• Log4j
• Logback
• JDK Util Logging
• Log Component used for logging message exchanges and/or parts of an exchange
• Headers
• Body
47
Log Component (camel-core)
• Log Component vs .log() DSL?
• Component logs exchanges
• DSL logs custom expressions
• Both allow you to specify log level
• Endpoint URI format: log:category[?options]
• category would be package name, eg. com.mycompanyname.project
• Options to control what part of the message is logged
48
Log Component configuration options
Option Description
showAll Turns on all options, such as body, body type, headers, out message, strackTraces, etc.
showExchangeId Log the exchangeId
showBodyType Logs the Java type for the body of the In message
showBody Log the actual contents of the body
showHeaders Log all of the headers of the In message
See http://camel.apache.org/log.html for more
49
Log Examples
from(“direct:incoming”).to(“log:org.apache.camel?level=INFO”).to(“jms:outgoingQueue”);
from(“direct:incoming”) .log(LoggingLevel.INFO, “We received a body: ${body}”) .to(“jms:outgoingQueue”);
Log Component
Log DSL
from(“direct:incoming”) .to(“log:org.apache.camellevel=INFO&showBody=false&showHeaders=true”) .to(“jms:outgoingQueue”);
from(“direct:incoming”) .to(“log:org.apache.camel?level=INFO&multiline=true”).to(“jms:outgoingQueue”);
50
Log Examples
from(“direct:incoming”).to(“log:org.apache.camel?level=INFO”).to(“jms:outgoingQueue”);
Log Component
Exchange[ExchangePattern:InOut, Headers:{
breadcrumbId=ID-FusePostaMac-local-54392-1358803440276-0-9, CamelToEndpoint=bean://enrichService?method=getCreditScore, creditScore=400, LoanNumber=100001
}, BodyType:String, Body: <contents here … >
]
Log Output
51
Essential Components
• File (camel-core)
• Bean (camel-core)
• Log (camel-core)
• JMS (camel-jms)
• CXF (camel-cxf)
• Mock (camel-core)
52
JMS Component (camel-jms)
• Not part of camel-core, so must add additional maven dependency
<dependency>
<groupId>org.apache.camel<groupId>
<artifactId>camel-jms</groupId>
<version>${camel-version}</version>
</dependency>
53
JMS Component (camel-jms)
• Used to connect to JMS compliant message broker
• Endpoint URI Format• jms:[temp:][queue:|topic:]DestinationName[?options]
• Configure a JmsComponent bean in Spring XML
• Replace the jms: prefix with the bean name of the JmsComponent bean
<bean id=”activemq" class=”org.apache.activemq.camel.component.ActiveMQComponent” > <property name=“brokerURL” value=“tcp://localhost:61616” /></bean>
from(“activemq:incoming”).process(…).to(“activemq:outgoing”);
Example
54
JMS Component (camel-jms)
• Default destination is Queuefrom(“activemq:incoming”).process(…).to(“activemq:outgoing”);
• Specify explicitlyfrom(“activemq:queue:incoming”).process(…).to(“activemq:queue:outgoing”);
• Specify Topicfrom(“activemq:topic:incoming”).process(…).to(“activemq:topic:outgoing”);
55
JMS Component (camel-jms)
• Pool your connections, sessions, consumers
• Set up connection pool
<bean id="jms" class="org.apache.camel.component.jms.JmsComponent"> <property name="configuration" ref="jmsConfig" /> < /bean>
<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration" > <property name="connectionFactory" ref="jmsPooledConnectionFactory" /> <property name="cacheLevelName" value="CACHE_CONSUMER" /> < /bean>
<bean id="jmsPooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" init-method="start" destroy-method="stop" > <property name="maxConnections" value="2" /> <property name="connectionFactory" ref="jmsConnectionFactory" /> < /bean>
56
JMS Component configuration options
Option Default Description
asyncConsumer false Process messages asynchronously
concurrentConsumers 1 Number of concurrent consumers
cacheLevelName CACHE_AUTO
Determines what JMS objects to cache, Connections, Sessions, Consumers, None, Auto.
transacted true Use transacted sessions
clientId null Unique connection ID (used for duable sub)
durableSubscriptionName
null Subscriber name for durable subscriptions
disableReplyTo false Treat all messages as InOnly, ignore JMSReplyTo
replyTo null Default replyTo destination
Selector null Set the JMS selector
timeToLive null JMS time to live
See http://camel.apache.org/jms.html for more
57
JMS Examples
Multiple consumers
Durable Subscriber
from(“jms:incomingQueue?concurrentConsumers=5”).bean(someBean).to(“jms:outgoingQueue”);
from(“jms”topic:incoming?clientId=1&durableSubscriptionName=foo1”).bean(someBean);
Selectors
from(“jms:incomingQueue?selector=headerName %3D ‘somevalue’”).to(“jms:outgoingQueue”);
Request Reply
from(“direct:incoming”).inOut().to(“jms:outgoingQueue”).to(“bean:someBean”);
58
Essential Components
• File (camel-core)
• Bean (camel-core)
• Log (camel-core)
• JMS (camel-jms)
• CXF (camel-cxf)
• Mock (camel-core)
59
CXF Component (camel-cxf)
• Not part of camel-core, so must add additional maven dependency
<dependency>
<groupId>org.apache.camel<groupId>
<artifactId>camel-cxf</groupId>
<version>${camel-version}</version>
</dependency>
60
CXF Component (camel-cxf)
• Integrates with CXF web services framework
• Consumers (expose web service), Producers (consume web service)
• Endpoint URI format:• Address style: cxf:address[?options]
• Bean style: cxf:bean:beanName
• Address Style• No bean necessary, URI quite verbose, CXF config limited
• Bean Style• Compact, flexible config allows CXF interceptors, etc
61
CXF bean endpoint configuration
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/cxf" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd http://camel.apache.org/schema/cxf
http://camel.apache.org/schema/cxf/camel-cxf.xsd">
<cxf:cxfEndpoint id="helloWorldWS" wsdlURL="wsdl/HelloWorld.wsdl" serviceClass="org.apache.helloworld.HelloWorld" address=http://localhost:9090/helloworld
serviceName=“tns:HelloService” endpointName=“tns:SoapOverHttpEndpoint
> </cxf:cxfEndpoint>
Service Endpoint Interface
WSDL definition
Where to publish
from(“cxf:bean:helloWorldWS”).bean(“processWS”);
62
CXF bean endpoint configuration
• Setting the serviceClass attribute
• In CXF, we set serviceClass to • The JAX-WS interface in web client endpoints
• The JAX-WS implementation class in web service endpoints because we want CXF to dispatch requests to our code
• In Camel routes, we set serviceClass to the JAX-WS interface for producers or consumers because we want Camel to process to requests
• Using JAX-WS annotations
• Can omit the wsdlUrl, serviceName, and endpointName attributes from endpoint bean
63
CXF Address endpoint configuration
from(“cxf:http://localhost:9090/helloworld?
serviceClass=org.apache.helloworld.HelloWorld&wsldUrl=wsdl/
HelloWorld.wsdl&serviceName=tns:HelloService”).bean(“processW
S”);
• More verbose
• Clutters up the route by mixing details
• Useful for testing
• Cannot configure CXF details
64
CXF Payload
• The dataFormat option can have one of the following values:• POJO – arguments bound to plain old java objects
• PAYLOAD – message payload <soap:body>
• MESSAGE – raw message, as InputStream
Address style
Bean style
from(“cxf:http://localhost:8080/?dataFormat=PAYLOAD”).bean(“processWS”);
from(“cxf:bean:helloWorldWS”).bean(“processWS”);
<cxf:cxfEndpoint id=“helloWorldWS” … > <cxf:properties> <entry key=“dataFormat” value=“MESSAGE”/> </cxf:properties></cxf:cxfEndpoint>
65
Essential Components
• File (camel-core)
• Bean (camel-core)
• Log (camel-core)
• JMS (camel-jms)
• CXF (camel-cxf)
• Mock (camel-core)
66
Mock Component (camel-mock)
• Powerful way to test your Camel routes• http://camel.apache.org/mock.html
• Uses Mocks
• Mocks vs Stubs? • http://martinfowler.com/articles/mocksArentStubs.html
• Provides declarative testing mechanism
• Declare
• Test
• Assert
67
Mock Component (camel-mock)
• Endpoint URI format: mock:mockName[?options]
• Can use just the same as any endpoint:
from(“direct:incoming”)
.choice()
.when(header(“loanNumber”).isGreaterThan(12345))
.to(“mock:specialLoan”)
.when(header(“loanNumber”).isLessThan(12345))
.to(“mock:regularLoan”)
.to(“mock:outgoing”);
68
Mock Component declare, test, assert
// look up the endpoint MockEndpoint resultEndpoint = context.resolveEndpoint(“mock:outgoing”,
MockEndpoint.class);
// set expectations resultEndpoint.expectedMessageCount(2);
// send some messages ...
// assert expectations resultEndpoint.assertIsSatisfied();
69
Mock endpoint expectations
Expectation method Description
expectedMessageCount(int) The number of messages that must have come through this mock
expectedMinimumMessageCount(int) The minimum number of messages that must have come to this mock
expectedBodiesReceived(Object …) The list of bodies must have come through this mock
expectedHeadersReceived(Object …) The list of headers that must have come through this mock
expectsNoDuplicates(Expression) No duplicate messages based on the expression (usually a unique header)
See http://camel.apache.org/mock.html for more
70
Mock endpoint expectations
• Can also set expectations on individual messages
• mockEndpoint.message(int).body()…
MockEndpoint mockEndpoint = getMockEndpoint(“mock:sink”);
mockEndpoint.message(0).body(String.class).contains("John Doe");
sinkEndpoint.message(0).header("loanNumber").isEqualTo(123456);
71
More Information
72
DZone Refcardz
• Camel Essential Components • http://refcardz.dzone.com/refcardz/essential-camel-components
• Essential EIP with Apache Camel• http://refcardz.dzone.com/refcardz/enterprise-integration
REFCARDZ
73
Red Hat / Fuse Source
74
Apache Community
• http://camel.apache.org
• Mailing list: [email protected]
• Nabble Archive: http://camel.465427.n5.nabble.com/Camel-Users-f465428.html
• Source code: http://svn.apache.org/viewvc/camel/trunk/
• Blogs, Articles, Examples• http://camel.apache.org/articles.html
• http://camel.apache.org/user-stories.html
• http://camel.apache.org/user-stories.html
• http://www.davsclaus.com
• http://www.christianposta.com/blog
75
Camel In Action
• Published 2011
• Claus Ibsen and Jon Anstey
• Covers EIPs, DSL, Components, Transactions, Threading, Expressions, Error Handling, Monitoring, etc
• In depth, examples, source code
• Source code kept up to date!• http://code.google.com/p/camelinaction/
• http://www.davsclaus.com/2013/01/camel-in-action-2-years-later.html
76
Contact Me
Email: [email protected]
[email protected] Blog: http://www.christianposta.com/blog Twitter: @christianposta Google+
77
Q & A