Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

2  Getting Started with Camel  Running and Deploying Camel  Understanding components  Enterprise Integration Patterns  Error Handling in Camel  Testing With Camel Agenda

Slide 3

Slide 3 text

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"

Slide 4

Slide 4 text

4  Quote from the website What is Camel?

Slide 5

Slide 5 text

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?

Slide 6

Slide 6 text

6 File Copier Example

Slide 7

Slide 7 text

7 Deploying Camel  Deployment Strategy  No Container Dependency  Lightweight & Embeddable Deployment Options  Standalone  WAR  Spring  JEE  OSGi  Cloud

Slide 8

Slide 8 text

8 Understanding Components  How Endpoint works  Relation between Components and endpoints

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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]

Slide 12

Slide 12 text

12 Components in Camel Find more at http://camel.apache.org/components.html

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

15 Camel’s Architecture

Slide 16

Slide 16 text

16 Camel Context  In Java code  In a Spring XML file CamelContext context = new DefaultCamelContext(); context.addRoutes(new MyRouteBuilder()); context.start();

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

18 Processor  Interface to create an Exchange processor  for consuming/handling an Exchange  for message transformation  Processor Interface Implementation.

Slide 19

Slide 19 text

19 EIPs in action

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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.

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

23 EIP: Splitter • Properties on the Exchange related to the Splitter EIP • Exchange.SPLIT_INDEX • Exchange.SPLIT_SIZE • Exchange.SPLIT_COMPLETE

Slide 24

Slide 24 text

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.

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

26 EIP: Aggregator  Java DSL

Slide 27

Slide 27 text

27 EIP: Aggregator  Spring DSL

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

29 EIP: Resequencer  Use Case: Ensure the items are order based on item number.

Slide 30

Slide 30 text

30 EIP: Content Based Router  Use Case : Organize orders in separate folders by customer country Enriched Order US Folder Processor SG Folder Processor

Slide 31

Slide 31 text

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.

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

33 EIP: Multicast  Each Bank receives a quote request from the Loan Broker and submits an interest rate quote according to the loan parameters

Slide 34

Slide 34 text

34 EIP: Recipient List  allows you to route messages to a number of dynamically specified recipients.

Slide 35

Slide 35 text

35 Error Handling in Camel  DefaultErrorHandler  DeadLetterChannel  TransactionErrorHandler  NoErrorHandler  Logging Error Handler

Slide 36

Slide 36 text

36 Error Handling in Camel  Default Error Handler  No redelivery  Exceptions are propagated back to the caller

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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.

Slide 39

Slide 39 text

39 Error Handling in Camel

Slide 40

Slide 40 text

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));

Slide 41

Slide 41 text

41 Testing With Camel Classes to Note.  TestSupport  CamelTestSupport  CamelSpringTestSupport  Testing using multiple environments  Using mocks  Simulating real components  Simulating errors

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

44 Testing With Camel interceptSendToEndpoint("http://rider.com/rider") .skipSendToOriginalEndpoint() .proces(new SimulateHttpErrorProcessor());

Slide 45

Slide 45 text

45 Testing using multiple environments

Slide 46

Slide 46 text

46 Using mocks

Slide 47

Slide 47 text

47 Testing With Camel

Slide 48

Slide 48 text

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/

Slide 49

Slide 49 text

49

Slide 50

Slide 50 text

50 Any Questions ? ● Contact ● Email: [email protected]