Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Enterprise Integration Patterns & Camel By Rajesh Das

Steve Teo
December 12, 2014

Enterprise Integration Patterns & Camel By Rajesh Das

Apache Camel is a rule-based routing and mediation engine that offers multiple DSLs (Domain Specific Language) in regular programming languages such as Java, Scala, Groovy, and it also allows routing rules to be specified in XML. Camel offers higher-level abstractions that allow you to interact with various systems using the same API regardless of the protocol or data type the systems are using. Camel allows you to define your own routing rules, decide from which sources to accept messages, and determine how to process and send those messages to other destinations

Camel is heavily used in open source integration space due to the following features.
• Implementation of Enterprise integration patterns (EIPs)( http://camel.apache.org/enterprise-integration-patterns.html)
• Support for multiple Domain-specific languages (DSL http://camel.apache.org/dsl.html)
• Extensive component library ( http://camel.apache.org/components.html)
• Payload-agnostic router
• Modular and pluggable architecture
• POJO model
• Easy configuration
• Automatic type converters
• Lightweight core API.
• Test kit
• Vibrant community

Camel session will cover
1. Routing With Camel
2. Understanding endpoints
3. Enterprise integration patterns in Camel
4. Understanding components
5. Creating routes
6. Routing and EIP
7. Transforming data with Camel
8. Error Handling in Came
9. Testing With Camel

Steve Teo

December 12, 2014
Tweet

More Decks by Steve Teo

Other Decks in Technology

Transcript

  1. Disclaimer: The information contained in this document is intended only

    for use during the presentation. DBS Bank accepts no liability whatsoever with respect to the use of this document or its contents. Enterprise Integration Patterns & Camel Rajesh Ranjan Das 12 December 2014
  2. 2  Getting Started with Camel  Running and Deploying

    Camel  Understanding components  Enterprise Integration Patterns  Error Handling in Camel  Testing With Camel Agenda
  3. 3 Introduction to Camel  Why do we need integration?

     Critical for your business to integrate  Why Integration Framework?  Framework do the heavy lifting  You can focus on business problem  Not "reinventing the wheel"
  4. 5  Routing and mediation engine  Implementation of Enterprise

    integration patterns (EIPs) http://camel.apache.org/enterprise-integration-patterns.html  Support for multiple Domain-specific languages (DSL) http://camel.apache.org/dsl.html  Extensive component library http://camel.apache.org/components.html  Payload-agnostic router  Modular and pluggable architecture  POJO model Why Camel?
  5. 7 Deploying Camel  Deployment Strategy  No Container Dependency

     Lightweight & Embeddable Deployment Options  Standalone  WAR  Spring  JEE  OSGi  Cloud
  6. 9  Endpoint is an abstraction that models the end

    of a message channel through which a system can send or receive messages.  createProducer() will create a Producer for sending message exchanges to the endpoint  createConsumer() implements the Event Driven Consumer pattern for consuming message exchanges from the endpoint via a Processor when creating a Consumer Understanding Endpoints An endpoint acts as a neutral interface allowing systems to integrate
  7. 10  createPollingConsumer() implements the Polling Consumer pattern for consuming

    message exchanges from the endpoint via a Polling Consumer A polling consumer actively checks for new messages.  URIs to specify endpoints – camel-core provides • direct: seda: vm: timer: log: – camel-ftp provides • ftp: – camel-jms • jms: Understanding Endpoints
  8. 11 Understanding Components  FTP and FILE ftp://[username@]hostname[:port]/directoryname[?options] sftp://[username@]hostname[:port]/directoryname[?options] ftps://[username@]hostname[:port]/directoryname[?options]

     Example from("ftp://foo@myserver?password=secret&ftpClient.dataTimeout=30000") .to("bean:foo");  JMS from("jms:incoming.orders?transacted=true") .to("jms:topic:events?priority=7") .to("jms:queue:orders?timeToLive=1000");  SSH ssh:[username[:password]@]host[:port][?options]
  9. 13 Defining routes Defining routes in a Java RouteBuilder. 

    Extend org.apache.camel.builder.RouteBuilder  Implement the configure() method Defining routes in a Spring XML file
  10. 14  Runtime execution context – 'glue' for everything else

    • Registry, Type Converters, Components, Endpoints • Languages and Data Formats  Routes are added to the context as well • context allows for runtime control over Routes with start() and stop() methods Camel Context
  11. 16 Camel Context  In Java code  In a

    Spring XML file CamelContext context = new DefaultCamelContext(); context.addRoutes(new MyRouteBuilder()); context.start();
  12. 17 Camel API basics Let's look at a few basic

    Camel API interfaces  Message  Exchange  Processor  Message  Header  Attachment  Body  Exchange  Exchange Id  MEP  Exception  Properties  In message  Out Message
  13. 18 Processor  Interface to create an Exchange processor 

    for consuming/handling an Exchange  for message transformation  Processor Interface Implementation.
  14. 20 Enterprise Integration Patterns  Use the EIPs to implement

    2 Use Cases Use Case 1: Complex Order Processing  Read the filtered orders files from directory  Split the order in separate items.  Send the split items to country based processing engines.  Make sure that we also have a copy of the original file in an audit folder.  Combine several, related item messages into a single order message  Ensure the items are order based on item number.  Organize orders in separate folders by customer country  Look at some of the EIPs available in Camel  Goals and use cases Implementation in the Java DSL Implementation in the Spring XML DSL
  15. 21 EIP: Message Filter  How can you avoid receiving

    uninteresting messages  How can you avoid processing unwanted order files  Read the filtered orders files from directory. Only read and publish the xml files to the order processing queues.
  16. 22 EIP: Splitter  How can we process a message

    if it contains multiple elements, each of which may have to be processed in a different way?  All elements should process in separate threads.  Split the order in separate items.
  17. 23 EIP: Splitter • Properties on the Exchange related to

    the Splitter EIP • Exchange.SPLIT_INDEX • Exchange.SPLIT_SIZE • Exchange.SPLIT_COMPLETE
  18. 24 EIP: Wire Tap  Goal: Wire Tap allows you

    to route messages to a separate location while they are being forwarded to the ultimate destination.  Use case: To keep a copy of the original message in an audit folder.
  19. 25 EIP: Aggregator  Correlation identifier— An Expression that determines

    which incoming messages belong together  Completion condition— A Predicate or time-based condition that determines when the result message should be sent  Aggregation strategy— An Aggregation Strategy that specifies how to combine the messages into a single message  Use case: Combine several, related item messages into a single order message
  20. 28 EIP: Resequencer  allows you to reorganise messages based

    on some comparator  By default the Resequencer does not support duplicate messages and will only keep the last message, in case a message arrives with the same message expression. However in the batch mode you can enable it to allow duplicates.  Batch resequencing collects messages into a batch, sorts the messages and sends them to their output.  Stream resequencing re-orders (continuous) message streams based on the detection of gaps between messages
  21. 30 EIP: Content Based Router  Use Case : Organize

    orders in separate folders by customer country Enriched Order US Folder Processor SG Folder Processor
  22. 31 Loan Broker Use Case Use Case 2 : Loan

    Broker (EIP: Scatter-Gather)  The Client makes requests for loan quotes.  The Loan Broker acts as the central process manager and coordinates the communication between the credit bureau and the banks.  The Credit Bureau provides a service to the Loan Broker, computing customer's credit scores.  Each Bank receives a quote request from the Loan Broker and submits an interest rate quote according to the loan parameters.
  23. 32 EIP: Multicast How to route the same message to

    multiple recipients? Example: order information is sent to accounting as well as fulfilment department  Java DSL  Spring DSL
  24. 33 EIP: Multicast  Each Bank receives a quote request

    from the Loan Broker and submits an interest rate quote according to the loan parameters
  25. 34 EIP: Recipient List  allows you to route messages

    to a number of dynamically specified recipients.
  26. 35 Error Handling in Camel  DefaultErrorHandler  DeadLetterChannel 

    TransactionErrorHandler  NoErrorHandler  Logging Error Handler
  27. 36 Error Handling in Camel  Default Error Handler 

    No redelivery  Exceptions are propagated back to the caller
  28. 37 Error Handling in Camel  Dead Letter error handler

     It is the only error handler that supports moving failed messages to a dedicated error queue  The dead letter channel supports using the original input message when a message is moved to the dead letter queue
  29. 38 Things to Note About Error Handler  Redelivery Policies

    - define if redelivery should be attempted also define redelivery attempt and delays between attempts  Scope – Context or Route  Exception policies - Exception policies allow you to define special policies for specific exceptions.  Error handling- allows you to specify whether or not the error handler should handle the error. You can let the error handler deal with the error or leave it for the caller to handle.  Ignoring exceptions.
  30. 40 Error Handling in Camel  onWhen—Allows you to dictate

    when an exception policy is in use. onException(HttpOperationFailedException.class).onWhen(bean(MyHttpUtil.class, "isIllegalData")) .handled(true).to("file:/acme/files/illegal");  onRedeliver—Allows you to execute some code before the message is redelivered errorHandler(defaultErrorHandler() .maximumRedeliveries(3) .onRedeliver(new MyOnRedeliveryProcessor()); onException(IOException.class) .maximumRedeliveries(5) .onRedeliver(new MyOtherOnRedeliveryProcessor())  retryWhile—Allows you, at runtime, to determine whether or not to continue redelivery or to give up onException(IOException.class).retryWhile(bean(MyRetryRuletset.class));
  31. 41 Testing With Camel Classes to Note.  TestSupport 

    CamelTestSupport  CamelSpringTestSupport  Testing using multiple environments  Using mocks  Simulating real components  Simulating errors
  32. 42 Testing With Camel Method Description expectedMessageCount(int count) Specifies the

    expected number of messages arriving at the endpoint expectedMinimumMessageCount (int count) Specifies the expected minimum number of messages arriving on the endpoint expectedBodiesReceived (Object... bodies) Specifies the expected message bodies and their order arriving at the endpoint expectedBodiesReceivedInAnyOrder (Object... bodies) Specifies the expected message bodies arriving at the endpoint; ordering doesn’t matter assertIsSatisfied() Validates that all expectations set on the endpoint are satisfied message(int index) Defines an expectation on the n’th message received allMessages() Defines an expectation on all messages received expectsAscending(Expression expression) Expects messages to arrive in ascending order expectsDescending(Expression expression) Expects messages to arrive in descending order expectsDuplicates(Expression expression) Expects duplicate messages expectsNoDuplicates(Expression expression) Expects no duplicate messages expects(Runable runable) Defines a custom expectation
  33. 43 Testing With Camel • Some example on other methods

    commonly used on MockEndpoint •mock.allMessages().body().contains("Camel"); •mock.message(0).header("JMSPriority").isEqualTo(4); •mock.allMessages().body().regex("^.*Camel.*\\.$"); •mock.expectsAscending(header("Counter")); •mock.expectedBodiesReceivedInAnyOrder("Camel rocks", "Hello Camel"); • Simulating errors using interceptor. • intercept • interceptFromEndpoint • interceptSendToEndpoint
  34. 48  Concurrency and Scalability  Management and Monitoring 

    Load Balancing  Bean Routing and Remorting. What More Where do I get more information Try Camel Examples http://camel.apache.org/examples.html Read other blogs and articles http://camel.apache.org/articles.html Use the Camel Page http://camel.apache.org/
  35. 49