Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Essential Camel Components

4,874 views

Published on

Webinar from http://blip.tv/dzone/apache-camel-essential-components-6511579

Published in: Technology
  • Be the first to comment

Essential Camel Components

  1. 1. Apache Camel Essential Components Presenter: Christian Posta Title: Senior Consultant Date: January 23, 2013
  2. 2. Agenda   What is Camel?  Camel Intro  Components  More Information  2 FuseSource/Red Hat Q&A
  3. 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   Production Support, Training, Consulting   Certified, hardened, based on open source Proven track record in mission-critical apps Acquired by Red Hat in September 2012  Two open source leaders in the market: comprehensive middleware solutions   3 Share a maniacal focus on community Offices in almost all time zones and over 30 countries
  4. 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   Community – need to re-register at redhat.com  4 JIRAS → redhat.com fusesource.com - maintained at least through 2013
  5. 5. About Me  Christian Posta  Senior Consultant  Committer at Apache on ActiveMQ, Apollo  Email: christian@redhat.com ceposta@apache.org   Twitter: @christianposta  5 Blog: http://www.christianposta.com/blog Google+
  6. 6. What is Camel? 6
  7. 7. What isn‟t Apache Camel • Enterprise Service Bus (ESB) • Container • ServiceMix/FuseESB • OSGi container • Tomcat, JBoss, Geronimo • Commercial App Servers • Proprietary, closed source 7
  8. 8. Integration Use Camel to Integrate disparate systems that talk different protocols and data formats. 8
  9. 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 9
  10. 10. Why integration is hard… • Platforms • Protocols • Data Formats • Timing • Organizational mismatch • Communication Hard, but not impossible, definitely not new… 10
  11. 11. Patterns! • Enterprise Integration Patterns (EIP) • Specific context • Forces at work • Concrete solution • Guidance for solutions to your problems • 65 patterns • • Splitter • Aggregator • 11 Content Based Router Filter
  12. 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) 12
  13. 13. Camel Intro 13
  14. 14. Quick Example File System 14 Message Oriented Middleware
  15. 15. Quick Example From A 15 Filter message Send to B
  16. 16. Quick Example from(A) 16 filter(predicate) to(B)
  17. 17. Quick Example from(A) 17 .filter(isWidget) .to(B)
  18. 18. Quick Example isWidget = xpath(“/quote/product = „widget‟”); from(A) .filter(isWidget). to(B) 18
  19. 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”); } } 19
  20. 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…) • 20 from(“..”).enrich(“…”).filter(“..”).to(“…”);
  21. 21. Pipes and Filters Architecture • Step by Step • Complex processing • Flexible • Testing • Reuse 21
  22. 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 • • 22 EIPs Tranform, redirect, enrich
  23. 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"); 23
  24. 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> 24
  25. 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 25
  26. 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 26
  27. 27. Components… http://camel.apache.org/components.html • • GMail • AMQP • HTTP • ATOM feeds • IRC • AWS (Amazon Web Services) • jclouds • Bean • JDBC • Cache (EHCache) • Jetty • CXF (JAX-WS, JAX-RS) • Twitter • EJB • MQTT • Drools • MyBatis • File • JPA • FTP • Spring Integration • 27 ActiveMQ, Websphere, Weblogic (JMS) Google App Engine • Spring Web Services To see list of all components!!
  28. 28. Essential Components 28
  29. 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 29
  30. 30. Essential Components • File (camel-core) • Bean (camel-core) • Log (camel-core) • JMS (camel-jms) • CXF (camel-cxf) • Mock (camel-core) 30
  31. 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? 31
  32. 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… from(“file:/location/path?move=.processed”).to(“jms:queueName”) • URI format: file:path[?options] 32
  33. 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] 33
  34. 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 34
  35. 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 35
  36. 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 36
  37. 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 37
  38. 38. Essential Components • File (camel-core) • Bean (camel-core) • Log (camel-core) • JMS (camel-jms) • CXF (camel-cxf) • Mock (camel-core) 38
  39. 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 C a m e l R o u t e 39
  40. 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 40
  41. 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(…) • 41 from(“direct:incoming”).beanRef(“enrichService”, “getCreditScore”)
  42. 42. Bean Examples Custom class public class CreditService { public int getCreditScore(@XPath("/Borrower/BorrowerId") long borrowerId) { … body of impl here … } } Spring bean definition <bean id="enrichService” class="com.christianposta.refcard.CreditService" /> Camel route using bean component from(“jms:incoming”).to(“bean:enrichService?method=getCreditScore”).to(“jms:outgoing”); 42
  43. 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) 43
  44. 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 44
  45. 45. Essential Components • File (camel-core) • Bean (camel-core) • Log (camel-core) • JMS (camel-jms) • CXF (camel-cxf) • Mock (camel-core) 45
  46. 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 • • 46 Headers Body
  47. 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] • • 47 category would be package name, eg. com.mycompanyname.project Options to control what part of the message is logged
  48. 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 48
  49. 49. Log Examples Log Component from(“direct:incoming”).to(“log:org.apache.camel?level=INFO”).to(“jms:outgoingQueue”); 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”); Log DSL from(“direct:incoming”) .log(LoggingLevel.INFO, “We received a body: ${body}”) .to(“jms:outgoingQueue”); 49
  50. 50. Log Examples Log Component from(“direct:incoming”).to(“log:org.apache.camel?level=INFO”).to(“jms:outgoingQueue”); Log Output 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 … > ] 50
  51. 51. Essential Components • File (camel-core) • Bean (camel-core) • Log (camel-core) • JMS (camel-jms) • CXF (camel-cxf) • Mock (camel-core) 51
  52. 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> 52
  53. 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 Example <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”); 53
  54. 54. JMS Component (camel-jms) • Default destination is Queue from(“activemq:incoming”).process(…).to(“activemq:outgoing”); • Specify explicitly from(“activemq:queue:incoming”).process(…).to(“activemq:queue:outgoing”); • Specify Topic from(“activemq:topic:incoming”).process(…).to(“activemq:topic:outgoing”); 54
  55. 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> 55
  56. 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) durableSubscriptionNa null me 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 56
  57. 57. JMS Examples Multiple consumers from(“jms:incomingQueue?concurrentConsumers=5”).bean(someBean).to(“jms:outgoingQueue”); Durable Subscriber 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”); 57
  58. 58. Essential Components • File (camel-core) • Bean (camel-core) • Log (camel-core) • JMS (camel-jms) • CXF (camel-cxf) • Mock (camel-core) 58
  59. 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> 59
  60. 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 • 60 Compact, flexible config allows CXF interceptors, etc
  61. 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"> Service Endpoint Interface <cxf:cxfEndpoint id="helloWorldWS" wsdlURL="wsdl/HelloWorld.wsdl" WSDL definition serviceClass="org.apache.helloworld.HelloWorld" address=http://localhost:9090/helloworld serviceName=“tns:HelloService” Where to publish endpointName=“tns:SoapOverHttpEndpoint > </cxf:cxfEndpoint> from(“cxf:bean:helloWorldWS”).bean(“processWS”); 61
  62. 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 • 62 Can omit the wsdlUrl, serviceName, and endpointName attributes from endpoint bean
  63. 63. CXF Address endpoint configuration • More verbose • Clutters up the route by mixing details • Useful for testing • Cannot configure CXF details from(“cxf:http://localhost:9090/helloworld?serviceClass=org.apache.hellowo rld.HelloWorld&wsldUrl=wsdl/HelloWorld.wsdl&serviceName=tns:HelloServi ce”).bean(“processWS”); 63
  64. 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 from(“cxf:http://localhost:8080/?dataFormat=PAYLOAD”).bean(“processWS”); Bean style from(“cxf:bean:helloWorldWS”).bean(“processWS”); <cxf:cxfEndpoint id=“helloWorldWS” … > <cxf:properties> <entry key=“dataFormat” value=“MESSAGE”/> </cxf:properties> </cxf:cxfEndpoint> 64
  65. 65. Essential Components • File (camel-core) • Bean (camel-core) • Log (camel-core) • JMS (camel-jms) • CXF (camel-cxf) • Mock (camel-core) 65
  66. 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 • • Test • 66 Declare Assert
  67. 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”); 67
  68. 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(); 68
  69. 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 69
  70. 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); 70
  71. 71. More Information 71
  72. 72. DZone Refcardz REFCARDZ • Camel Essential Components • http://refcardz.dzone.com/refcardz/essential-camel-components • Essential EIP with Apache Camel • 72 http://refcardz.dzone.com/refcardz/enterprise-integration
  73. 73. Red Hat / Fuse Source 73
  74. 74. Apache Community • http://camel.apache.org • Mailing list: users@camel.apache.org • 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/user-stories.html • http://camel.apache.org/user-stories.html • http://www.davsclaus.com • 74 http://camel.apache.org/articles.html http://www.christianposta.com/blog
  75. 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! • • 75 http://code.google.com/p/camelinaction/ http://www.davsclaus.com/2013/01/camel-in-action-2years-later.html
  76. 76. Contact Me  Email: christian@redhat.com ceposta@apache.org   Twitter: @christianposta  76 Blog: http://www.christianposta.com/blog Google+
  77. 77. Q&A 77

×