Successfully reported this slideshow.

Essential Camel Components



Loading in …3
1 of 77
1 of 77

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

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 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 transitioning to   Community – need to re-register at  4 JIRAS → - maintained at least through 2013
  5. 5. About Me  Christian Posta  Senior Consultant  Committer at Apache on ActiveMQ, Apollo  Email:   Twitter: @christianposta  5 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… • • 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) 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 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 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 • • • 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 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 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 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="" xmlns:xsi="" xmlns:cxf="" xsi:schemaLocation=""> 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 • • Uses Mocks • Mocks vs Stubs? • • 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 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 • • Essential EIP with Apache Camel • 72
  73. 73. Red Hat / Fuse Source 73
  74. 74. Apache Community • • Mailing list: • Nabble Archive: f465428.html • Source code: • Blogs, Articles, Examples • • • • • 74
  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
  76. 76. Contact Me  Email:   Twitter: @christianposta  76 Blog: Google+
  77. 77. Q&A 77

Editor's Notes

  • ESB product = something you buy and magically all of your enterprise systems can “integrate” with each otherCamel isn’t a container like tomcat, or websphere. You don’t deploy code to Camel. You deploy your code along with Camel to a Java container (or stand alone if desired)Proprietary, closed source = community driven… “free” if you will, but care must be taken to differentiate between “free as in beer” and “free as in speech” Liberal licensing, use, can package in your own products, can make changes without having to contribute back, input from everyone, etc
  • Integration is necessary in just about any IT shop
  • One thing any good IT department will tell you is that Integration is Hard!You have to deal with different “system vintages” both off the shelf and in house developed… Vintages = Mainframe, ETL, File based integration EAI = HubsIn house products = J2EE, EJB
  • Windows based (.NET), Java on Linux, Web based, Fat client, Mobile!!HTTP, messaging, CORBA, COM/DCOM, EJB, RMI, custom TCP/IPPlan XML or sub categories: MISMO, Accord, SOAPJSON, Protocol Buffers, Fixed-file formats (COBOL mainframe files), HL7, CSV, EDITiming = synchronous calls over a network… either party in the integration must be availableBut that becomes difficult because often times the systems to be integrated are aligned with business units or IT units.. You don’t have control over the systems you’re integrating with, so communication between groups is crucial.. And establishing coherent communication between groups always seems to be difficult to accomplish
  • Patterns to the rescue right?Well after the GoF book, seems like patterns are a “trend” or “hype” more than anything useful. If you do a search for software patterns on Amazon, you’re gonna come back with a large list of results, some useful some not. Patterns are useful if they can package up some useful concepts and convey them intelligently to other developers. That’s the role of patterns, to convey knowledge. Typically, they describe a specific context, go into the different forces that apply to any solution or alternative solutions, describe a concrete solution and what the trade-offs are, and are ultimately to be used for guidance. The EIP book does exactly that by formalizing 65 patterns often found when doing enterprise integration
  • Lightweight in that it’s not an overblown, overhyped, ESB product… it’s just a set of a few java jar files that can plug into any java projectOpen source! Apache Licensed, vibrant community 700 subscribers, 45 messages per day, 24 committers, excellent response time from JIRA and mailing lists.Mediation? Router?resolve/negotiate differences between the two so they can exchange dataGives you an out of the box implementation for the EIPs described in Gregor’s bookOver 130 pre-packaged “components” for interfacing with different protocols and third-party systemsRuns in any container!
  • Let’s take a look at a very simple integration scenario where you would like to read files from a file system and send them to a messaging system, but filter out only certain ones. Let’s only send Widget Quotes to the messaging system, and not Gadget quotes… What would something like that look like in Camel? Well, first let’s conceptualize…
  • In plan language, we’re going to be taking “From a file system”, Filtering out the Gadgets, and “Sending to a messaging system”
  • We can start to formalize it using some java methods… where we want to go from A and filter on a predicate, then send to B
  • Now we are pretty close to what Camel’s DSL looks like…
  • And now we have a camel route…The key here is that Camel routes are intended to be easy to read and comprehend. Integration is difficult, so let’s start by making certain things clear.
  • This is actual java code. Notice the readability of the code as it expresses exactly what it’s doing.
  • A DSL is a special purpose language for describing problem or solutions more clearly than a general purpose language (or programming language) using syntax and grammar applicable to a given domain.Examples include SQL, regex or Mathematica (for symbolic math), excel macros, etc
  • Camel routes are built using a “Pipes and Filter” model, where smaller, well defined “filters” are connected up by “pipes” to perform a sequence of processing steps according to the business rules.For example, consider a business that accepts new orders. A requirement of the order is that it’s encrypted so nobody else can snoop it, it must contain authentication credentials so that only approved customers can place orders, and it must also be able to track and eliminate duplicate orders. We could have one piece of code that does all of this, but that would be quite inflexible. What happens if there needs to be a change and we need to check inventory before sending to “clean orders”? Using pipes and filtesr allows us to keep the filters small for flexibility, easy testing, and potential reuse. Camel routes follow this mentality.
  • Routes are described as a series of steps that an incoming message can travel.There must an entry point to the route, and that’s where the notion of “consumers” comes in. Consumers are endpoints that Listen for incoming messages. This can be a file endpoint, jms, web service, etc. Then the message is processed by zero or more “filters”And if desired, the message can be sent along to another system using a Producer endpoint…Note the slight difference in terminology here… in web services, a provider is the web service and a consumer is the client invoking the web service. But in the case of Camel, a consumer is actually acting more like a “provider” of messages to a route. Wherease a producer is “sending messages”You can have any number of filters…
  • import org.apache.camel.builder.RouteBuilder;/** * A Camel Java DSL Router */public class MyRouteBuilder extends RouteBuilder { /** * Let&apos;s configure the Camel routing rules using Java code... */ public void configure() { // here is a sample which processes the input files // (leaving them in place - see the &apos;noop&apos; flag) // then performs content based routing on the message using XPath from(&quot;file:src/data?noop=true&quot;) .choice() .when(xpath(&quot;/person/city = &apos;London&apos;&quot;)) .to(&quot;file:target/messages/uk&quot;) .otherwise() .to(&quot;file:target/messages/others&quot;); }}
  • import org.apache.camel.builder.RouteBuilder;/** * A Camel Java DSL Router */public class MyRouteBuilder extends RouteBuilder { /** * Let&apos;s configure the Camel routing rules using Java code... */ public void configure() { // here is a sample which processes the input files // (leaving them in place - see the &apos;noop&apos; flag) // then performs content based routing on the message using XPath from(&quot;file:src/data?noop=true&quot;) .choice() .when(xpath(&quot;/person/city = &apos;London&apos;&quot;)) .to(&quot;file:target/messages/uk&quot;) .otherwise() .to(&quot;file:target/messages/others&quot;); }}
  • What is a component?A component is a prepackaged extension to camel that allows you to communicate with external systems. Primary extension pointYou use it by specifying which component to use, which context (consumer or producer) you want to use it in, and then configure it specifically for what your integration needs are. The remainder of these webinar will introduce 6 often-used components and give an idea of how to use them and configure them. Camel has over 130 components, some of which are listed on the next slide…
  • It’s a URI that describes an endpoint. Endpoints can be used to bring messages into a camel route (consumer) or send messages out from a camel route (producer). The URI has three parts a scheme which describes a component which is basically the “type” of endpointLocalpart is usually a name or path whos context (what it really means) is specific to the componentAnd options, which lets you configure a specific component.
  • This is by no means an exhaustive list… Please see the components URL
  • These are the components I’ll cover in this webinar.These are not listed in any kind of meaningful order. They are the components that I’ve seen used quite a bit in integration routes. There are plenty more that I wish we could have talked about, including jetty, http, twitter, drools, twitter, Amazon Web Services, etc.
  • Let’s start by taking a look at a component that allows you to integrate with file based solutions….
  • Interestingly enough as it is, file based integrations are still around, and will probably not go away for some time. One of the tenants of integration is that there will be different protocols and integration should be flexible enough to deal with them. This is a good example. Systems such as mainframes or other nightly batch applications tend to expect files as input. Well, there are many good file libraries out there, you could probably write a lot of file integration solutions yourself. But after doing this enough times, you’ll find that writing and reading files is largely very similar between different processes… wouldn’t you want something a little more “out of the box”? Well, that’s what you get with Camel’s components.
  • Think for a moment about how long it would take you to write an application that does the following…An hour? A few hours?At a recent gig I was on with a large financial company, the answer was an astounding 200+ hours (including unit testing, build processes, and management approval, etc). This simple route with camel gives an idea of how packaged components can provide out of the box functionality to make your development a little easier
  • So the File Component allows you to read or write files from and to the file system. You specify the endpoint using absolute or relative paths.Notice the URIs are as defined in the previous slide… the file component prefix followed by a path on the file system and followed by options to configure the specifics of the endpoint…
  • By default, the file component will read all files in a given path… for each file, it will create a new message exchange that will be processed by the camel route. Keep in mind that any file or directory starting with the “.” character are ignored!File component is very configurable, as we will see some options in the next slide.
  • A couple things to keep in mind when working with the file component are the default behaviors…
  • Read a relative location named “inbox”, move a file to the “inprogress’ folder, process the message (sends to activeMQ) then move to .done folderDirect invocation sending a message to a file named report.txtPick up files in data folder, exclude any file starting with a _ underscore… then send the message to handleData beanSort files
  • Let’s take a look at a component that allows you to work directly with Java Beans
  • The bean component allows you to connect in any Plain Old Java Object into your route processing… Specifically, this component implements the “Service Activator” pattern from the EIP book which allows you to connect existing services (usually synchronous) to an integration route (could be asynchronous or synchronous). If you’re not familiar with the SA pattern, I encourage you to look it up, or look up any of the patterns that you come across using Camel. This should give you a good foundation for how to use Camel’s different component and EIP implementations.
  • So specifically, the bean component allows you to invoke methods on your own, possibly existing, java beans. The endpoint definition follows the same pattern as all of the other components… bean: beanId and then some options.. Although not very many options for this componentCamel will automatically try to bind the parameters of the POJO method to elements of the message or message exchange…
  • You can invoke the bean component using it as an endpoint in the “to” method… or take advantage of a more expressive Method called “bean”Either you can have the bean available in the registry (spring, jndi, osgi, etc), or you can have the route lazy create it when the route is invoked. Note, this will not create a new bean each time the route is run.. It will create it once and cache it.
  • Continuing the example… You have a POJO class called CreditService… Camel will automatically bind the params, but in this case we’re being explicit by binding to a path in the XML payload. This expects the payload to be XML…Define the bean in spring with a bean ID of “enrich service”Then can use as an endpoint
  • What if you don’t specify a method name??Camel is smart enough to try and figure out which method to use… though it’s probably best to specify a method name…
  • How are the parameters bound? Magically of course, but there are some well-known types that will automatically be bound if used as parameters
  • These are not listed in any kind of meaningful order. They are the components that I’ve seen used quite a bit in integration routes. There are plenty more that I wish we could have talked about, including jetty, http, twitter, drools, twitter, Amazon Web Services, etc.
  • It’s a good practice to log your exchanges as they go through critical parts of a route. Under the covers, the Log Component uses SLF4J which itself delegates to log4j, java util logging, logback, or some other supported logging facility.Specifically, the log component logs interesting parts of an exchange, like headers, body, body type, properties, etc.
  • Some questions may come up about what to use as far as the Log Component or the Log DSL. Just like Bean Component could be used as an endpoint or in a DSL method, Log offers the same options. Use the log component to log coarse parts of an exchange, or the entire exchange.Use the log dsl method to customize the log method and include simple expressionsBoth allow yout specify the logging level
  • These are not listed in any kind of meaningful order. They are the components that I’ve seen used quite a bit in integration routes. There are plenty more that I wish we could have talked about, including jetty, http, twitter, drools, twitter, Amazon Web Services, etc.
  • The JMS component does not belong to camel-core.. The reason why all components aren’t included is because each of these external components depends on other libraries and the classpath would become quite polluted with all of these extra dependencies and transitive dependencies…But it’s simple to add the components that you’re interested in using
  • You can use JMS component to connect to JMS providers such as ActiveMQ, HornetQ, or commercial providers such as Websphere or Weblogic. For AMQP or MQTT protocols, there are corresponding camel components. You could connect up to ActiveMQ with either of those components as well.The URI will be just like other components, except that when you declare the JMS provider you’re going to use, you might want to use a “component” name that matches your provider. Doing so is as simple as naming your spring bean with the name you want to use in your route. For example…
  • By default, you don’t need to specify whether you’re listening or sending to a Queue because that’s implicit if it’s left out.If you want to send to a topic, you specify that after the component name
  • One of the things to note when using the JMS component is to cache your connections, and consumers where possible. Doing so for durable subscribers is not recommended because the JMS spec disallows multiple connections with the same clientId and durable subscription name…But to set it up correctly, instantiate a new PooledConnection Factory… by default the cache strategy will be CACHE_AUTO which would be fine because it will select CONSUMER under the covers, but I like to be explicit with this setting to make sure there is no confusion. Also, when switching to use transactions, the TX manager will use its own cache setting, and this way it will be set correctly.
  • Please please take a look at the component documentation for JMS… there are many configuration options for tweaking the endpoint…
  • The JMS component does not belong to camel-core.. The reason why all components aren’t included is because each of these external components depends on other libraries and the classpath would become quite polluted with all of these extra dependencies and transitive dependencies…But it’s simple to add the components that you’re interested in using
  • Dealing with SOAP web services can be a pain in the ass, but CXF really simplifies this task. Furthermore, integrating with CXF becomes much easier with Camel. Just take note of the nomenclature differencesThere are two different ways to use the CXF component: bean or address style
  • Let’s take a look at how to configure the cxf endpoint for the bean style of integrating with CXFAs consumer Factor out how message payloads are received into soap or rest.
  • CXF uses WSDL to abstract away service details.Note the difference in what the “serviceClass” signifies… In CXF it depends on whether we’re a client or server.In Camel, it’s always the interface. This is because when running as a “server” or exposing it, we want camel route to provide the implementation, not a CXF impl classUsing the JAX-WS annotations helps reduce some of the XML properties that need to be configured.
  • You could also use the address style of configuring the CXF component, but there are some drawbacks.You can already see it’s more verbose, kinda clutters up the rotue, and cannot configure CXF bus or interceptor details this way.Use for simple routes or for testing…
  • Cant talk about CXF integration without mentioning how the payloads of a SOAP message are dealt with. For small payloads, you can consider using the POJO approach where Camel can automatically convert to your domain objects.But often times you’ll need to operate on the xml in a less resource-intenstive way… use PAYLOAD to access the soap body, or use MESSAGE for the raw input stream. You could use something like MESSAGE to send large messages, or messages with attachments. None of the extra CXF processing happens on MESSAGE data types.Note the difference in configuration…
  • ×