jboss camel - red hatpeople.redhat.com/tgubelig/fuseesb/camel-exercisebook-v1.3.pdf · in this...

61
Camel Development with JBoss Fuse V1.3 August 2013 Red Hat Corporation Developer Training for JBoss Camel Exercise Handbook JBoss Camel

Upload: others

Post on 24-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Red Hat Corporation

Developer Training for JBoss Camel

Exercise Handbook

JBoss Camel

Page 2: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Red Hat, Inc. and/or its subsidiaries may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this publication. Red Hat, FuseSource, Orbix, High Performance Integration, Artix, FUSE, and Making Software Work Together are trademarks or registered trademarks of Progress Software Corporation and/or its subsidiaries in the U.S. and other countries. Java and J2EE are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. CORBA is a trademark or registered trademark of the Object Management Group, Inc. in the United States and other countries. All other trademarks that appear herein are the property of their respective owners. While the information in this publication is believed to be accurate Red Hat, Inc. makes no warranty of any kind to this material including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. Red Hat, Inc. shall not be liable for errors contained herein, or for incidental or consequential damages in connection with the furnishing, performance or use of this material. No part of this publication may be reproduced, stored in a retrieval system or transmitted, in any form or by any means, photocopying, recording or otherwise, without prior written consent of Red Hat, Inc. No third party intellectual property right liability is assumed with respect to the use of the information contained herein. Red Hat, Inc. assumes no responsibility for errors or omissions contained in this book. This publication and features described herein are subject to change without notice. Copyright © 2013 Red Hat, Inc. All products or services mentioned in this manual are covered by the trademarks, service marks, or product names as designated by the companies who market those products. Updated: V1.3 August 2013

Page 3: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 3

Developer Training for JBoss Camel 1   Introduction to the Exercise System ......................................................................... 5  Locating the exercises ......................................................................................................... 5  

2   Understanding the Exercise System .......................................................................... 6  Overview ............................................................................................................................. 6  Review of POM files .......................................................................................................... 6  Building the exercises ......................................................................................................... 8  Review of the latest Instructions ......................................................................................... 8  Philosophy of Executing the Camel Exercises ................................................................... 8  File Input and Output Folders ............................................................................................. 8  Sample data ......................................................................................................................... 9  

3   Content Based Router (CBR) .................................................................................. 10  Overview ........................................................................................................................... 10  Review of Solution ........................................................................................................... 10  Building and Running the Solution ................................................................................... 11  Validating the Solution ..................................................................................................... 11  

4   Content Based Router - Using @Endpoints ........................................................... 13  Review of Solution ........................................................................................................... 13  Building and Running the Solution ................................................................................... 14  Validating the Solution ..................................................................................................... 14  

5   Testing Routes ........................................................................................................... 16  Review of Solution ........................................................................................................... 16  Building and Running the Solution ................................................................................... 17  Validating the Solution ..................................................................................................... 17  

6   Camel Exceptions ...................................................................................................... 19  Review of Solution ........................................................................................................... 20  Building and Running the Solution ................................................................................... 20  Validating the Solution ..................................................................................................... 21  1) Process a EU Payment File ........................................................................................... 21  2) Activating the “direct-error-handler-with-exception” ................................................. 21  3) Process a US Payment File ........................................................................................... 22  4) Invoking the direct-error-handler ................................................................................. 22  

7   Route CSV files from FTP ....................................................................................... 24  Review of Solution ........................................................................................................... 24  Configuring for the FTP Server ........................................................................................ 25  Building and Running the Solution ................................................................................... 26  Validating the Solution ..................................................................................................... 26  

8   Using the Splitter ....................................................................................................... 27  Review of Solution ........................................................................................................... 27  Building and Running the Solution ................................................................................... 28  Validating the Solution ..................................................................................................... 28  

9   Using the Aggregator ................................................................................................ 30  

Page 4: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 4

Review of Solution ........................................................................................................... 30  Building and Running the Solution ................................................................................... 31  Validating the Solution ..................................................................................................... 31  Further Notes on the Solution ........................................................................................... 32  

10   Reliable message processing with JMS flows ......................................................... 33  Review of Solution ........................................................................................................... 33  Building and Running the Solution ................................................................................... 35  Validating the Solution ..................................................................................................... 35  

11   Creating and deploying a custom component. ....................................................... 37  Review of Solution ........................................................................................................... 37  Building and Running the Solution ................................................................................... 39  Validating the Solution ..................................................................................................... 39  

12   Exposing a WebService ............................................................................................ 40  Review of Solution ........................................................................................................... 41  Building and Running the Solution ................................................................................... 42  Further Notes on the Solution ........................................................................................... 43  

Appendix A:   Software requirements ......................................................................... 46  Software requirements ...................................................................................................... 46  

Appendix B:   Additional Software Tool Notes ........................................................... 47  Using jconsole to query JMS queues .......................................................................... 47  Using JBoss Fuse webconsole .................................................................................... 47  

Appendix C:   Using JBoss Fuse for JBoss Camel Exercises .................................... 49  File Input and Output Folders ........................................................................................... 49  Review of the JBoss Fuse ‘features’ mechanism ............................................................. 49  Sample data ....................................................................................................................... 50  

Appendix D:   Content Based Router ........................................................................... 51  

Appendix E:   Content Based Router - Endpoints ...................................................... 53  

Appendix F:   Camel Exceptions .................................................................................. 55  

Appendix G:   Route CSV files from FTP ................................................................... 56  

Appendix H:   Using the Splitter .................................................................................. 57  Setting up the Broker ........................................................................................................ 57  

Appendix I:   Using the Aggregator ............................................................................. 58  

Appendix J:   Reliable message processing with JMS flows ...................................... 59  

Appendix K:   Creating and deploying a custom component. ................................... 60  

Appendix L:   Exposing a WebService ......................................................................... 61  

Page 5: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 5

1 Introduction to the Exercise System

Overview This exercise system has been designed with a number of objectives in mind:

• Provide a comprehensive set of demonstration code that illustrates how to perform common tasks with the product (Camel).

• Demonstrate best practices in terms of naming schemes, code, and build environment. • Make it easy both to run/view the exercises but also to provide a production-like

deployment inside an ESB: JBoss Fuse. Each exercise is complete in terms of code; developers in the course are invited to extend or modify the solutions as they see fit, or indeed use the demonstrations as starting points for their own solutions. For each exercise, the student must:

• Understand the use-cases implemented by the exercise • Review key aspects of the solution – including code, build system and deployment. • Build and deploy the solution – first from the command-line and then, optionally, later

using the ESB container JBoss Fuse. • Validate that the solution works through informal test procedures.

Locating the exercises The exercises have been pre-installed and available within your student VM in the Virtual Training classroom. You will find all the Camel exercises stored in the folder

~/JB421/labs/all-exercises-camel/camel-exercises

Page 6: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 6

2 Understanding the Exercise System

Overview Each folder of the Camel exercise system contains a fully functioning demonstration Enterprise Integration Pattern (EIP) flow implemented using the Camel system. The Maven POM , or ‘project object model’, defines common dependencies, repositories, and plug-ins used by Maven to build each solution. The directory where you extract the exercises will contain a top-level parent pom.xml, along with a camel-exercises directory. Inside the camel-exercises directory there is another POM, along with directories that hold each of the exercises. Each exercise directory contains a POM with information specific to that exercise. The following shows the structure: all-exercises-camel 1. |--pom.xml |--camel-exercises 2. |-- pom.xml |-- cbr 3. |-- pom.xml |-- etc,etc... As shown in the directory structure above, the exercises demonstrate a multi-module project that contains a top-level parent POM (1), along with a sub-project POM (2). Each exercise has its own POM (3) that lives at a deeper level, inside each individual exercise directory. In this exercise you will review the POM files in order to understand the structure, and to see the resources required to build each solution.

Review of POM files Use the table below to guide your study of the pom.xml. The pom.xml found in each exercise module, inherits from this top-level (parent) pom.xml.

Page 7: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 7

File Note Check Top-level parent POM (1)

Locate the <repositories> section. (Do you understand why each repository is required?

O

Look at the properties section. Can you determine where these properties are used? Hint: check descendent POM files.

O

What is the purpose of using the maven-compiler-plugin?

O

File Note Check Sub-project POM (2)

Note how this POM indicates its parent POM in the <parent> section.

O

Note the <modules> section: this is how Maven keeps track of the directories of its modules (children POMs) i.e. the exercise projects

O

Look at each of the dependencies in the <dependencyManagement> section. Do you understand why each is required?

O

File Note Check Exercise-specific POM (3)

Note how this POM indicates its parent POM in the <parent> section.

O

Look at each of the dependencies in the <dependencies> section. Do you understand why each is required and how this works with respect to the <dependencyManagement> section of its parent POM.

O

Page 8: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 8

Building the exercises You may build all the exercises in one operation by going into the ‘all-exercises-camel’ directory containing the root POM file and entering

mvn -Dmaven.test.skip install This will compile and package each of the exercise solutions, and install the resulting artifacts (typically bundles) in your machine’s local Maven repository.

Review of the latest Instructions All of the exercises have a ‘Readme.txt’ in the root folder of the exercise. Please read this file in order to get the latest instructions as well as typing instructions for the command line so that command line typos can be avoided.

Philosophy of Executing the Camel Exercises In order to focus on the mechanics and operation of the Camel routes, we will first execute almost all the exercises in a standalone environment using the simple maven command

mvn camel:run to run each lab exercise, from within the exercise folder. The exceptions are the test exercise (using mvn test) and starting a JMS broker (mvn activemq:run). After you have become familiar with the JBoss Fuse product, we will then show you how to execute the very same Camel exercises but from within the JBoss Fuse container. For simplicity of documenting the exercises, only the use of the Maven Camel plugin will be used in the body of this exercise book, while the different instructions for the use of JBoss Fuse to install/deploy/execute each the exercises will be supplied in Appendix chapters. Note that in order to support this dual execution environment, the project level POM file defines the separate Camel context files to be used for either case. In particular, the mvn camel:run execution uses the file located at ./src/test/resources/spring-camel-context.xml of the project folder tree, while the JBoss Fuse-based execution looks for the Camel context defined in ./src/main/resources/META-INF/spring/camelContext.xml

File Input and Output Folders If the exercises are run standalone (without deployment to JBoss Fuse) , i.e. by typing ‘mvn camel:run’, then the input data file folders used in most of the exercises are in the ‘./target/test-classes/camel/’ folder within the project folder structure. These

Page 9: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 9

folders are created and populated automatically during the build/install step providing the necessary files to run each exercise.

Sample data Throughout the exercises we will drive data files through our integration flows; the supplied initial input data files are located in the ./src/test/resources/camel/in folder of each project. These files are the ones used to populate the various input folders as mentioned in the last section.

Page 10: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 10

3 Content Based Router (CBR)

Overview In this exercise, we will build and deploy two content-based routers. The first router uses an XPath query based on the XML payload of the arriving message to perform routing decisions. The second router uses a header of the message (containing the name of the file that initiated the route) to perform routing.

Review of Solution The files for this exercise are located in the directory: ~/JB421/labs/all-exercises-camel/camel-exercises/cbr Before beginning the exercise, locate and open each of the files in the table below and preview the file as instructed.

./target/test-classes/in/xml

Payments.XML.EU

Payments.XML.US

Payments.UNKNOWN

XPath Router

./target/test-classes/in/xml

Payments.CSV.EU Payments.CSV.US

Payments.UNKNOWNNNN

Header-based Router

Page 11: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 11

File Note Check pom.xml Look at each of the dependencies in

the <dependencies> section. Do you understand why each is required? Note: some of the dependencies are inherited from the parent pom.

O

Examine the configuration instructions for the maven-bundle-plugin. Do you understand how the OSGi bundle is being created?

O

RouteByCurrencyRouter.java Examine the router; do you understand the flow of control?

O

Why are the InitializingBean and DisposableBean interfaces implemented?

O

RouteByFileNameRouter.java Examine the router; do you understand the flow of control?

O

spring-camel-context.xml Can you see how all the input and output folder definitions as presented?

bundle-context.xml For JBoss Fuse execution only: Do you understand how each route is being initialized?

O

camelContext.xml For JBoss Fuse execution only: Note how this file simply includes the bundle-context.xml file. This is to allow the camel:run plugin to re-use Spring configuration while running outside of an OSGi container.

O

Building and Running the Solution To run the solution from the command-line , use

> cd ~/JB421/labs/all-exercises-camel/camel-exercises/cbr > mvn camel:run

Validating the Solution For execution using ‘mvn camel:run’ the routes will begin by looking for test files in ./target/test-classes/camel/in. Since the build/install step pre-populates these input folders, you don’t need to copy any files to start the validation process. The execution will be logged into the terminal window as files are processed by the two different routes. The resulting output files will be stored in the folders under ./target/test-classes/camel/out directory within the exercise folders for this exercise. This is true for

Page 12: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 12

both routers (XPath and header-based). Note also that typically files that are processed from the in folders will by default be copied into a subfolder named .camel under the in folder. Also the output files are created by appending all the input files for a given currency type for a given day with a single file appropriately named; e.g. euro-20130113.xml.

Page 13: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 13

4 Content Based Router - Using @Endpoints

Overview This exercise uses the Injection mechanism of Apache Camel to configure the @Endpoints used in the same Camel routes used in the previous CBR exercise.. The RouteBuilder class no longer implements the Spring interfaces. Instead, they will configure each Endpoint individually using the following annotation and Java Field. For example, @EndpointInject(ref = "sourceDirectoryXml") Endpoint sourceUri; The Endpoint bean is instantiated by Spring and added to the Camel Context like that <endpoint id="sourceDirectoryXml"

uri="file:./target/test-classes/camel/in/xml"/> For JBoss Fuse execution, we use the endpoint definitions as in: <endpoint id="sourceDirectoryXml" uri="${sourceDirectoryXmlUri}"/> and combined this with a property placeholder or using Config Admin of JBoss Fuse externalizes the configuration of the endpoint in a property file (e.g. camel_cbr.cfg), such as

sourceDirectoryXmlUri=file:/camel-exercises/cbr/in/xml This approach allows for the decoupling of the specification of message sources/sinks into configuration files which are independent of the application code.

Review of Solution The files for this exercise are located in the directory: ~/JB421/labs/all-exercises-camel/camel-exercises/cbr-endpoints Before beginning the exercise, locate and open each of the files in the table below and preview the file as instructed.

Page 14: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 14

File Note Check pom.xml Look at each of the dependencies in

the <dependencies> section. Do you understand why each is required? Note: some of the dependencies are inherited from the parent pom.

O

Examine the configuration instructions for the maven-bundle-plugin. Do you understand how the OSGi bundle is being created?

O

RouteByCurrencyRouter.java Examine the router; do you understand the flow of control?

O

@Endpoint injection is used to configure the camel endpoint

O

RouteByFileNameRouter.java Examine the router; do you understand the flow of control?

O

spring-camelContext.xml Definition of routes for mvn camel:run execution

O

bundle-context.xml Do you understand how each route is being initialized? Used only for JBoss Fuse execution

O

camel-cbr.cfg Contain definition of the camel URI for the file endpoint polling directory containing Xml files (IN) - used only for JBoss Fuse execution

O

Building and Running the Solution To run the solution from the command-line , use

> cd ~/JB421/labs/all-exercises-camel/camel-exercises/cbr-endpoints > mvn camel:run

Validating the Solution As with the previous exercise illustrating the CBR: For execution using ‘mvn camel:run’ the routes will begin by looking for test files in ./target/test-classes/camel/in. Since the build/install step pre-populates these input folders, you don’t need to copy any files to start the validation process. The execution will be logged into the terminal window as files are processed by the two different routes. The resulting output files will be stored in the folders under ./target/test-classes/camel/out directory within the exercise folders for this exercise. This is true for both routers (XPath and header-based). Note also that typically files

Page 15: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 15

that are processed from the in folders will by default be copied into a subfolder named .camel under the in folder. Also the output files are created by appending all the input files for a given currency type for a given day with a single file appropriately named; e.g. euro-20130113.xml.

Page 16: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 16

5 Testing Routes

Overview JBoss Camel provides the concept of “mock” endpoints that allow you to test routing and business logic of your Camel routes in a unit-test environment. In this exercise, we draw your attention to a set of tests used to test the Content-Based Router.

This exercise contains 2 types of Unit Test; a simple one and another extending CamelTestSupport class. The second case allows you to simplify the code to be written as it will instantiate for you the CamelContext.

Review of Solution The files for this exercise are located in the directory: ~/JB421/labs/all-exercises-camel/camel-exercises/cbr Before beginning the exercise, locate and open each of the files in the table below and preview the file as instructed.

target/test-classes/camel/in/xml

mock:eu

mock:us

mock:unknown

XPath Router Test

Page 17: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 17

File Note Check RouteByCurrency NRouterTest.java

Examine the setUp() method; note the use of the JUnit 4 @Before annotation to mark this method to be invoked before each test.

O

Note how in the setUp() method we: • Create a CamelContext • Initialize the router and inject “mock”

URIs to replace the physical endpoint URIs.

• Create mock endpoints. • Add the router to the CamelContext. • Start the CamelContext. • Create a message producer.

O

Note that the teardown() method, annotated with @After, shuts-down the CamelContext after every test.

O

Examine each of the tests, annotated with the JUnit 4 @Test annotation. Note the pattern: set expectations on the mock object, run the test, assert expectations were met.

O

RouteByCurrency NRouterTest2.java

Examine that we have removed the @After, @Before method and we have simply @Override the RouteBuilder class to configure the endpoint and use mock and direct

O

Building and Running the Solution To run the solution from the command-line , use

> cd ~/JB421/labs/all-exercises-camel/camel-exercises/cbr > mvn test

Validating the Solution You’ll see that the tests are run automatically as part of the build process, and should all pass. Now let us break one of the tests. For example, edit the shouldRouteUsPaymentsToUsQueue() method and change the expected message count on the mockUs endpoint to 0 or 2. Rerun the tests using ‘mvn test’ and see what happens. You should see that the build system compiles the project, runs the tests, and reports the

Page 18: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 18

failure. The failure out can be found under ‘target/surefire-reports/TEST-<full qualified Test Class Name>’

Page 19: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 19

6 Camel Exceptions

Overview In this exercise we show how to configure some of the ErrorHandlers proposed by JBoss Camel to have a better understanding and how the exceptions are managed by Camel and propagated to the error handler. We will use the DefaultErrorHandler and DeadLetterChannel in combination with an onException strategy. The following code in the RouteByCurrencyRouter.java file illustrates two different exception handling approaches, depending on the type of payment. onException(MyFunctionalException.class).routeId("myfunctional-exception") .maximumRedeliveries(0) .handled(true) .log(LoggingLevel.INFO, "%%% MyFunctional Exception handled."); from(sourceUri).routeId("cbr") .convertBodyTo(String.class) .log(LoggingLevel.INFO, "Message to be handled: ${file:onlyname}, body: ${body}") .choice() .when( xpath("/pay:Payments/pay:Currency = 'EUR'") .namespace("pay", "http://www.fusesource.com/training/payment")) .log(LoggingLevel.INFO, "This is an Euro XML Payment: ${file:onlyname}") .setHeader("Payment").simple("EUR") .to(directErrorHandlerWithException) .when( xpath("/pay:Payments/pay:Currency = 'USD'") .namespace("pay", "http://www.fusesource.com/training/payment")) .log(LoggingLevel.INFO, "This is an USD XML Payment: ${file:onlyname}" ) .setHeader("Payment").simple("USD") .to( directErrorHandler ) .otherwise() .log(LoggingLevel.INFO, "This is an Other Currency XML Payment: ${file:onlyname}" ) .to( moneyUriXml ); from(directErrorHandlerWithException).routeId("direct-error-handler-with-exception") // DefaultErroHandler will be used as default .log("Message will be processed only 1 time.") .beanRef("myBeanErrorException"); from(directErrorHandler) .routeId("direct-error-handler") .errorHandler(deadLetterChannel(directDLQError).maximumRedeliveries(2)) .log("Message will be processed 2 times.") .beanRef("myBeanError"); from(directDLQError).routeId("DLQ") .log(">>> Info sent to DLQ");

Page 20: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 20

Review of Solution The files for this exercise are located in the directory: ~/JB421/labs/all-exercises-camel/camel-exercises/exception Before beginning the exercise, locate and open each of the files in the table below and preview the file as instructed. File Note Check pom.xml Look at each of the dependencies in

the <dependencies> section. Do you understand why each is required? Note: some of the dependencies are inherited from the parent pom.

O

Examine the configuration instructions for the maven-bundle-plugin. Do you understand how the OSGi bundle is being created?

O

RouteByCurrencyRouter.java Examine the router; do you understand the flow of control?

O

@Endpoint injection is used to configure the camel endpoint

O

spring-camelContext.xml Definition of routes for mvn camel:run execution

O

bundle-context.xml For JBoss Fuse-based execution only: Do you understand how each route is being initialized?

O

camelContext.xml For JBoss Fuse-based execution only: Note how this file simply includes the bundle-context.xml file. This is to allow the camel:run plugin to re-use Spring configuration while running outside of an OSGi container.

O

Building and Running the Solution To run the solution from the command-line , use

> cd ~/JB421/labs/all-exercises-camel/camel-exercises/exception > mvn camel:run

Page 21: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 21

Validating the Solution For command line execution using ‘mvn camel:run’ when started the routes will automatically look for (pre-populated) test files in./target/test-classes/camel/in. You don’t need to copy any files to start the validation process.

1) Process a EU Payment File Camel (camel-1) thread #0 - file://./target/test-classes/camel/in/xml INFO [cbr] - Message to be handled: EUPayments.xml, body: <?xml version="1.0" encoding="UTF-8"?> <tns:Payments xmlns:tns="http://www.fusesource.com/training/payment" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fusesource.com/training/payment xsd/Payment.xsd "> <tns:Currency>EUR</tns:Currency> <tns:Payment> <tns:from>ade</tns:from> <tns:to>jack</tns:to> <tns:amount>1000000.0</tns:amount> </tns:Payment> <tns:Payment> <tns:from>jack</tns:from> <tns:to>jill</tns:to> <tns:amount>20.0</tns:amount> </tns:Payment> <tns:Payment> <tns:from>ade</tns:from> <tns:to>jill</tns:to> <tns:amount>42.0</tns:amount> </tns:Payment> </tns:Payments> Camel (camel-1) thread #0 - file://./target/test-classes/camel/in/xml INFO [cbr] - This is an Euro XML Payment: EUPayments.xml

2) Activating the “direct-error-handler-with-exception” This is where we generate a MyFunctionalException which will be intercepted by onException. Exception is only processed one time in this case (counter = 1) Camel (camel-1) thread #0 - file://./target/test-classes/camel/in/xml INFO [direct-error-handler-with-exception] - Message will be processed only 1 time.

Page 22: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 22

Camel (camel-1) thread #0 - file://./target/test-classes/camel/in/xml INFO [com.fusesource.camel.exercises.exception.MyBean] - >>>> Exception created for : EUR, counter = 1 Camel (camel-1) thread #0 - file://./target/test-classes/camel/in/xml INFO [direct-error-handler-with-exception] - %%% MyFunctional Exception handled.

3) Process a US Payment File Camel (camel-1) thread #0 - file://./target/test-classes/camel/in/xml INFO [cbr] - Message to be handled: USPayments.xml, body: <?xml version="1.0" encoding="UTF-8"?> <tns:Payments xmlns:tns="http://www.fusesource.com/training/payment" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fusesource.com/training/payment ../xsd/Payment.xsd "> <tns:Currency>USD</tns:Currency> <tns:Payment> <tns:from>paul</tns:from> <tns:to>ade</tns:to> <tns:amount>1000000.0</tns:amount> </tns:Payment> <tns:Payment> <tns:from>daan</tns:from> <tns:to>jack</tns:to> <tns:amount>78.0</tns:amount> </tns:Payment> <tns:Payment> <tns:from>pat</tns:from> <tns:to>jill</tns:to> <tns:amount>13.0</tns:amount> </tns:Payment> </tns:Payments> Camel (camel-1) thread #0 - file://./target/test-classes/camel/in/xml INFO [cbr] - This is an USD XML Payment: USPayments.xml

4) Invoking the direct-error-handler Where a Dead Letter channel is configured. Exception is retried twice and sends to a DLQ channel (log a message). Camel (camel-1) thread #0 - file://./target/test-classes/camel/in/xml INFO [direct-error-handler] - Message will be processed 2 times. Camel (camel-1) thread #0 - file://./target/test-classes/camel/in/xml INFO [com.fusesource.camel.exercises.exception.MyBean] - >>>> Exception created for : USD, counter = 1

Page 23: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 23

Camel (camel-1) thread #0 - file://./target/test-classes/camel/in/xml INFO [com.fusesource.camel.exercises.exception.MyBean] - >>>> Exception created for : USD, counter = 2 Camel (camel-1) thread #0 - file://./target/test-classes/camel/in/xml INFO [com.fusesource.camel.exercises.exception.MyBean] - >>>> Exception created for : USD, counter = 3 Camel (camel-1) thread #0 - file://./target/test-classes/camel/in/xml INFO [DLQ] - >>> Info send to DLQ

Page 24: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 24

7 Route CSV files from FTP

Overview In this exercise we show how to configure a route that listens for CSV (Comma-Separated Value) files on an FTP server and processes them within the Camel Processor Pipeline. The route uses a CSV marshaller to unmarshal the data.

Review of Solution The files for this exercise are located in the directory: ~/JB421/labs/all-exercises-camel/camel-exercises/ftp Before beginning the exercise, locate and open each of the files in the table below and preview the file as instructed.

Message Translator

CSV Files from FTP

Page 25: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 25

File Note Check pom.xml Look at the dependencies in the POM.

These are required for the FTP and CSV marshalling functionality.

O

PaymentAcmount NExtractor.java

Examine the afterPropertiesSet() method: note how the FTP URI is constructed. Note how camel ftp component can be configured using options on the endpoint, e.g. delay, delete, etc.

O

Examine the configure() method, and note:

• How the CSV file content is marshaled to lists-of-lists-of-strings.

• How we iterate through the file contents.

O

spring-camelContext.xml

Definition of routes for mvn camel:run execution

O

bundle-context.xml For JBoss Fuse-based execution only: Note the settings used to configure the FTPRouter. You may need to modify these settings if you wish to run the exercise against your own FTP server.

O

Configuring for the FTP Server Since this exercise will immediately attempt to connect to an FTP server: you must have access to an FTP server to complete this exercise. The ‘camel:run’ test version will read a files from a folder and push them out to the FTP server during initialization so that there is no need to drop a file onto the FTP server. See src/test/resources/spring-camel-context.xml for the context file used to configure the route. 1) <bean id="amountExtractor" 2) class="com.fusesource.camel.exercises.ftp.PaymentAmountExtractor"> 3) <property name="ftpHostOrIPAddress" value="localhost"/> 4) <property name="ftpUserName" value="student"/> 5) <property name="ftpPassword" value="student"/> 6) <property name="ftpDirectoryWithEndingSlash" value="ftp/"/> 7) </bean> You will need to edit this file according to the directions below:

Page 26: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 26

Building and Running the Solution To run the solution from the command-line , use

> cd ~/JB421/labs/all-exercises-camel/camel-exercises/ftp > mvn camel:run

Validating the Solution The ftp component in the route is configured not to delete the file from the FTP server once the file is processed. The FTP component within Camel will recognize duplicate files and not process them repeatedly. This ability to recognize duplicate files is provided by the idempotentConsumer component within the Java route. This pattern is often required because seldom will your route be given ‘delete’ privileges on the FTP server. Verify that the file(s) has (have) been processed by examining the logs; amidst much logging information,you should see a log statement for each line of data in each data file. For example: Processing payment from '12345467' to '43211234', amount: 10000.0 Processing payment from '34563456' to '87989877', amount: 10000.0 As an optional exercise, see if you can modify the appropriate option passed to the Camel FTP component (within PaymentAmountExtractor.java) such that Camel deletes the file from the FTP server on processing. Recompile and redeploy.

Page 27: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 27

8 Using the Splitter

Overview In this exercise we show how to use a splitter to break up an incoming XML message into a number of parts. Our router will read a message from an incoming folder, split it into single payment messages, and store these on an output folder.

Review of Solution The files for this exercise are located in the directory: ~/JB421/labs/all-exercises-camel/camel-exercises/splitter Before beginning the exercise, locate and open each of the files in the table below and preview the file as instructed.

IndividualPayments.XML Payments.XML Splitter

Splitting an XML Payload

Page 28: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 28

File Note Check pom.xml Look at the dependencies in the

POM. O

SplitIntoSingle NPaymentsRouter.java

Examine the configure() method, and note how the splitter is configured to use a namespace-aware XPath.

O

spring-camel-context.xml Notice how initial data files are copied from in folder to the FTP site using an XML defined route.

O

bundle-context.xml Used for JBoss Fuse execution only: Note the settings used to configure the splitter.

O

Building and Running the Solution To run the solution from the command-line , use

> cd ~/JB421/labs/all-exercises-camel/camel-exercises/splitter > mvn camel:run

The command line version using ‘mvn camel:run’ will populate the incoming queue with a Payments instance read from the file system. See src/test/resources/spring-camel-context.xml for the Spring configuration that drives the standalone version. Notice, for testing purposes, this configuration demonstrates the concept of ‘chaining’ integration flows by having the end (i.e. producer endpoint) of one route also be the start (consumer endpoint) of another flow. The primary route is chained to a supporting route that writes the results to the file system. Also, the standalone version uses the “seda” component endpoint to link the routes. Seda is a lightweight in-memory asynchronous communication mechanism.

Validating the Solution For exercise execution from the command line (mvn camel:run) the route is triggered by files located in the directory./target/test-classes/camel/in. When this file is processed you will see the multiple (split) messages have been appended to the file at ‘./target/test-classes/camel/out/ single.payment.list.in.xml.snippets.txt’. An example of this output file is shown below: <tns:Payment xmlns:tns="http://www.fusesource.com/training/payment"> <tns:from>ade</tns:from> <tns:to>jack</tns:to>

Page 29: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 29

<tns:amount>1000000.0</tns:amount> </tns:Payment> <tns:Payment xmlns:tns="http://www.fusesource.com/training/payment"> <tns:from>jack</tns:from> <tns:to>jill</tns:to> <tns:amount>20.0</tns:amount> </tns:Payment> <tns:Payment xmlns:tns="http://www.fusesource.com/training/payment"> <tns:from>ade</tns:from> <tns:to>jill</tns:to> <tns:amount>42.0</tns:amount> </tns:Payment> <tns:Payment xmlns:tns="http://www.fusesource.com/training/payment"> <tns:from>paul</tns:from> <tns:to>ade</tns:to> <tns:amount>1000000.0</tns:amount> </tns:Payment>

If you inspect the Spring context file at (src/test/resources/spring-camel-context.xml) along with the code (see SplitIntoSinglePaymentsRouter.java), you will see that there is an optional way to trigger the route. A “helper” route defined here will move the contents of any file you drop in ‘./target/test-classes/camel/in onto the Payments.XML seda endpoint (in-memory) queue to begin the processing. Results from splitting the message will be placed onto IndividualPayments.XML seda queue. Providing an ‘alternative’ entry point into a route for ease of testing (such as file) is a common pattern when implementing Camel routes.

Page 30: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 30

9 Using the Aggregator

Overview In this exercise we show how to use the Aggregator EIP, by creating a route that:

• Has a file consumer poll the “in” directory. • Splits up an incoming XML message into individual XML payments • Processes each payment message individually, logging a debug message. • Aggregates the payment messages into messages with the same value for “To” • Writes aggregated messages to “out” directory.

The example is a little contrived; however, it does demonstrate the ‘time-based’ nature of the aggregation strategy and how to combine aggregated messages into a single message.

Review of Solution The files for this exercise are located in the directory: ~/JB421/labs/all-exercises-camel/camel-exercises/aggregator

<<camel:route>> :Logger

<<camel:route>> :Logger

Splitter

Aggregator

Aggregating XML Message Payloads

File: ./target/test-classes/camel/in

File: ./target/test-classes/camel/out

Page 31: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 31

Before beginning the exercise, locate and open each of the files in the table below and preview the file as instructed. File Note Check pom.xml Look at the dependencies in the

POM. O

PaymentsByReceiver NAggregator.java

Note how we split the incoming message (somewhat contrived for demo purposes) and then feed to the aggregator.

O

Note how the aggregator is configured:

• Messages are grouped for aggregation based on the “To” field of the payments.

• We use a custom aggregation strategy that simply appends the message bodies together.

• The aggregator is configured to ‘time-out’ and send whatever it has aggregated every five seconds.

O

At the end of the route, the aggregated content is printed to the screen and written to file.

O

spring-camelContext.xml Definition of routes for mvn camel:run execution

O

bundle-context.xml For JBoss Fuse-based execution only: Note the settings used to configure the aggregator.

O

Building and Running the Solution To run the solution from the command-line , use

> cd ~/JB421/labs/all-exercises-camel/camel-exercises/aggregator > mvn camel:run

Validating the Solution To validate the solution, first ensure that the aggregator routes are running. Also, ensure that the logging level has been set to DEBUG for the com.fusesource logger. The logging configuration is read from src/main/resources/log4j.properties.

Page 32: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 32

Further Notes on the Solution The aggregator is configured via the correlation expression (determines which messages to aggregate together) and the aggregation strategy (how to aggregate messages.) For the correlation expression, we base it on the content of the message. In this case the xpath expression that specifies the “To” field of the message. So all messages that belong to “Jack” will be correlated together, “all messages to “Jill”, etc. The xpath expression used in the route as correlation expression will return a new instance of DOM NodeList for every new message. So we need to define an expression that evaluates to the same object (or key) at runtime. The trick is to select a proper XPath expression and to cast the evaluation result to a String. If you look at the code in PaymentsByReceiverAggregator.java you will see that when the correlation expression we perform this cast to a String. This is done by passing the String.class as second argument to the xpath() call. The need to do this cast forced us to move the XPath expression after the aggregate() because of the additional String.class argument.

Page 33: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 33

10 Reliable message processing with JMS flows

Overview In this exercise we show how to implement a reliable message flow, using JMS transactions. If something should go wrong in the Camel flow, the JMS transaction that initiated the flow is rolled back, and the message is redelivered. The flow in this exercise also shows how to unmarshal the incoming payload using JAX-B; in fact, we use methods from JAX-B Java objects to examine the incoming data and determine whether to reject the message. This solution consists of three flows. The first flow simply listens on a file directory and sends any message (file) found there to the incomingPayments queue. The second flow listens for Payments messages on the incomingPayments queue, and routes the messages to the outgoingPayments queue if the payments currency is recognized. If the currency is not recognized, the payment is rolled-back. The JMS broker will attempt to redeliver the message in this case, and if subsequent redeliveries fail the message will be placed on a dead-letter queue. A third flow waits on the outgoingPayments queue and dumps messages to another file directory.

Review of Solution The files for this exercise are located in the directory: ~/JB421/labs/all-exercises-camel/camel-exercises/jms-transaction Before beginning the exercise, locate and open each of the files in the table below and preview the file as instructed.

incomingPayments outgoingPayments Transactional Consumer

JMS Transaction

Page 34: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 34

File 10.1.1 Note

Check

pom.xml

10.1.2 Notice the plugin used to generate JAXB code from the XSD using the xjc compiler.

O

spring-camelContext.xml Definition of routes for mvn camel:run execution

O

bundle-context.xml Notice how we create the JmsTransactionManager, and how it is injected into the JmsComponent’s connectionFactory. Also, notice that we set the transacted attribute of the JMSComponent to true. It also contains two additional routes to read from files into the incoming ActiveMQ queue and write to files from the outgoing queue.

O

PaymentCurrency NTestRouter.java

Notice in the configure() method that we’re disabling the built-in error handler, so that the backing system (JMS) will do the retries.

O

Note how in the configure() method we use JAXB unmarshalling to parse the XML payload and bind to a Java object, in this case, using the Payment class.

O

Note how we marshal the body back to XML, before placing it on the outgoing queue. It is arguably wasteful that we unmarshal just to marshal again: can you suggest an alternative approach to allow us to access the currency without having to unmarshal and marshal the payload? The important thing to note here is how we can easily use JAXB to perform parsing and generation of XML content: far easier and less error-prone than hand-written XML processing code.

O

In the afterPropertiesSet() method, note how the JaxbDataFormat is

Page 35: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 35

File 10.1.1 Note

Check

initialized with the name of the package containing the ObjectFactory for unmarshalling the payload.

Building and Running the Solution This solution requires auto-generation of JAVA code from the schema file “Payment.xsd”. This step will happen automatically when you run ‘mvn install.’ Alternatively you can run this directly by executing “mvn generate-sources”.

> cd ~/JB421/labs/all-exercises-camel/camel-exercises/jms-transaction

> mvn install

This exercise requires an active JMS broker instance. You can create one by starting up another terminal window, going to the jms-transaction folder and typing the command:

> mvn activemq:run To run the solution from the command-line, use:

> mvn camel:run

Validating the Solution When deployed in standalone mode (using ‘mvn camel:run’) the processing works as shown below. You don’t need to copy any files explicitly (the input folder is pre-populated) and the output directory is ./target/test-classes/camel/out directory. The input folder is populated using a Camel route which copies the file ./src/test/resources/camel/in/VariousUSPayments.xml to the ./target/test-classes/camel/in directory. It will be consumed up by the route and processed successfully, placing the content of the file on the outgoingPayments queue. Examine the log messages produced by the route to verify that the processor was executed appropriately. After the processing the output of the route will be into files inside the ./target/test-classes/camel/out directory.

Page 36: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 36

Do the same for the VariousEUPayments.xml file; again, it should be processed successfully. Now try the VariousUnknownPayments.xml, which has a currency value of ‘???’. The processing code in the route will reject the payment, forcing a JMS rollback and consequent redelivery. After the specified number of retries, the message will be moved to the dead-letter-queue, ‘ActiveMQ.DLQ’. You may use the jConsole JMX client to access/view/review the queues used in this exercise. The JMX URL needed to view the operations of this broker is:

service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi and no username nor password is required. Using the client, you can see messages being written to and read from various queues, as well as unprocessed messages (those causing the exceptions) written to the ActiveMQ.DLQ.

Page 37: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 37

11 Creating and deploying a custom component.

Overview In this exercise, we show how to create a custom component (in the custom-file-component directory), and how to use this component in a route. The component is similar to the Camel file: component; however, it uses a different prefix, filecomp:.

Review of Solution The files for this exercise are located in the directory: ~/JB421/labs/all-exercises-camel/camel-exercises/custom-file-component

Page 38: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 38

File Note Check

FileComponent.java Note how this class allows you to create FileEndpoints, using information passed from the endpoint URI.

O

FileEndpoint.java Note how this class allows you to create FileProducer and FileConsumer endpoints.

O

FileMessage.java This class extends the DefaultMessage class, adding a field to hold the current File being processed. You do not have to extend the DefaultMessage if you do not need to.

O

FileProducer.java This class extends the Camel DefaultProducer class. The key area to note is the process() method, which is called by the route when a message must be processed. It treats the incoming message body as an InputStream and writes it to a file.

O

FileConsumer.java This class is used when the file component is used in the from() element of a route. It has been implemented as a ScheduledPollConsumer, a base class which periodically calls the poll() method.

O

filecomp This file, in META-INF/services/org/apache/camel/component, is pivotal in how Camel loads the component. If Camel encounters a URI prefix that it does not recognize, it will look for a file with the same name of the prefix in this directory and instantiate the class within.

O

spring-camel-context.xml

This Spring configuration file supplies the Camel flow for the maven camel plugin deployment. Note the URI for the "from" and "to" route elements.

O

file.comp.spring-Ncontext.xml

This Spring configuration file shows how the custom file component would be used within the Spring DSL. It supplies the route which powers this exercise for the JBoss Fuse based deployment.

O

Page 39: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 39

Building and Running the Solution To run the solution from the command-line , use

> cd ~/JB421/labs/all-exercises-camel/camel-exercises/custom-file-component > mvn camel:run

Validating the Solution The validation of the solution using mvn camel:run is done automatically by having the sample files already in the ./target/test-classes/camel/in directory and the output directory would be in ./target/test-classes/camel/out directory. The processed input files will be renamed with "_" prepended to the original file name, while the output folder will contain files named _out_<n> where <n> increments for each processed file.

Page 40: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 40

12 Exposing a WebService

Overview In this exercise we show how to use Apache CXF technology with a Camel route, by providing 2 routes that performs the following steps:

• Route 1: o Has a file consumer poll the “/camel-exercises/webservices” directory. o Creates a SOAP request using SOAP DataFormat from the String contained in

the file consumed (the customer name) o Invokes WebService request using the SOAP request message, which ultimately

calls upon the second apache Camel route which exposes/implements the Web Service

o The WebService sends a response to the client which is logged on the console

• Route 2: o Implements the invoking of the following Web Service operations “saveCustomer,

getCustomerByName, getAllCustomers” o This route supports operation invocations from the first route as well as those

using a SOAPUI Client. <route> <from uri="file: /camel-exercises/webservices?noop=true"/> <convertBodyTo type="String"/> <log message="Name of the client to be created received : ${body}"/> <bean ref="enrich" method="createCustomer"/> <marshal ref="soapFormat"/> <setHeader headerName="SOAPAction"> <constant>http://training.fusesource.com/saveCustomer</constant> </setHeader> <!-- Call Web Service to save the client --> <to uri="http://localhost:9090/training/WebService"/> <log message="SOAP Response received : ${body}"/> </route>

<route id="cxf-to-queue" streamCache="true"> <from uri="cxf:bean:WS?dataFormat=MESSAGE"/> <convertBodyTo type="String"/> <log message="SOAP message received : ${body} and operation type : ${in.header.SOAPAction}"/> <unmarshal ref="soapFormat"/>

Page 41: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 41

<when> <simple>${in.header.SOAPAction} contains 'saveCustomer'</simple> <bean ref="enrich" method="saveCustomer"/> <marshal ref="soapFormat"/> </when> <when> <simple>${in.header.SOAPAction} contains 'getCustomerByName' </simple> <bean ref="enrich" method="getCustomerByName"/> <marshal ref="soapFormat"/> </when> <when> <simple>${in.header.SOAPAction} contains 'getAllCustomers'</simple> <bean ref="enrich" method="getCustomers"/> <marshal ref="soapFormat"/> </when> <log message="SOAP message returned : ${body} and operation type : ${in.header.SOAPAction}"/> </route>

Review of Solution The files for this exercise are located in the directory: ~/JB421/labs/all-exercises-camel/camel-exercises/webservice Before beginning the exercise, locate and open each of the files in the table below and preview the file as instructed.

Page 42: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 42

File Note Check pom.xml Look at the dependencies in the

POM. Maven-cxf-plugin has been added to generate from SWDL file the Java code (=wsdl2java)

O

Enrich.java Simple POJP / Bean class which is used to create / manipulate objects to save or get Customer

O

Camelcontext.xml See how the CXF endpoint is created and called by the camel-cxf endpoint. Spring will instantiate 2 beans to log the SOAP messages (loggingOutInterceptor, loggingInInterceptor). They are used by CXF during processing of the incoming SOAP envelope or when it creates the SOAP envelope for the client

O

wsdl/CustomerService.wsdl Wsdl file used by CXF to generate the code : - JAXB classes mapping XML (= SOAP Body) with POJO - Interface and Java Class annoted ( @WebService) exposing the WebService The code generated can be find under target/generated/src/main/java

O

Building and Running the Solution To run the solution from the command-line , use

> cd ~/JB421/labs/all-exercises-camel/camel-exercises/webservice > mvn camel:run

To validate the solution, first ensure that the webservice routes are running. Next copy the file customer.text from the location ./target/classes/data to /camel-exercises/webservices. Then verify on the console that you receive the following response from the Web Service called :

Page 43: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 43

Camel (camel-1) thread #1 - file:///camel-exercises/webservices INFO [route1] - SOAP Response received : <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:Envelope xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns3="http://training.fusesource.com/"> <ns2:Body> <ns3:customer> <name>Fuse Students</name> <address>FuseSource Office</address> <numOrders>28</numOrders> <revenue>1977.0</revenue> <test>100.0</test> <type>PRIVATE</type> </ns3:customer> </ns2:Body> </ns2:Envelope>

You may wish to create additional text files in the /camel-services/webservices folder, each of which need only contain a single customer name. Using different file names for each of these files will result in the creation of a new customer records and can be later retrieved using the getAllCustomer or getCustomerByName requests. As each file is created/copied, the terminal window will show the resulting activities within the Camel environment.

Further Notes on the Solution The exposed web service can be verified in a web browser: http://localhost:9090/training/WebService?wsdl Entering this URL in your web browser will result in the display of the WSDL file that is operational for this web service. It will be returned only if the web service is active and awaiting request messages. You can launch SOAP UI with the desktop icon. You can use the following request SOAP messages to save a new customer or get a list of all Customers. Your instructor can supply more details about how to configure SOAPUI to create a SOAPUI project from this lab and to execute these Web Service requests. The web service is accessible at http://localhost:9090/training/WebService?wsdl Save a Customer Request

Page 44: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 44

<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tra="http://training.fusesource.com/"> <soapenv:Body> <tra:saveCustomer> <customer> <name>charles88</name> <address>yyyyy</address> <birthDate>12/12/12</birthDate> </customer> </tra:saveCustomer> </soapenv:Body> </soapenv:Envelope> Response received <ns2:Envelope xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns3="http://training.fusesource.com/"> <ns2:Body> <ns3:customer> <name>charles</name> <address>yyyyy</address> <numOrders>86</numOrders> <revenue>4393.0</revenue> <test>100.0</test> <type>PRIVATE</type> </ns3:customer> </ns2:Body> </ns2:Envelope> Remark : numOrders, revenue values are random values for all customers Get all Customers Request <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tra="http://training.fusesource.com/"> <soapenv:Header/> <soapenv:Body> <tra:getAllCustomers/> </soapenv:Body> </soapenv:Envelope> Response received

Page 45: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 45

<ns2:Envelope xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns3="http://training.fusesource.com/"> <ns2:Body> <ns3:getAllCustomersResponse> <return> <name>Fuse Students</name> <address>FuseSource Office</address> <numOrders>69</numOrders> <revenue>2789.0</revenue> <test>100.0</test> <type>PRIVATE</type> </return> <return> <name>FuseSource</name> <address>yyyyy</address> <numOrders>86</numOrders> <revenue>4393.0</revenue> <test>100.0</test> <type>PRIVATE</type> </return> <return> <name>RedHat</name> <address>yyyyy</address> <numOrders>81</numOrders> <revenue>6269.0</revenue> <test>100.0</test> <type>PRIVATE</type> </return> <return> </ns3:getAllCustomersResponse> </ns2:Body> </ns2:Envelope>

Page 46: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 46

Appendix A: Software requirements

Software requirements Software required for the Camel exercise system are listed below and they are already installed and available from within the Red Hat Classroom Environment. The student does NOT have to download/install any of these for the class labs. However, a student may wish to acquire some of these software products for further prototyping, development and deployment. Software Version URL Notes JDK 1.6 or higher http://java.sun.com Required. Set

JAVA_HOME to point to the installation directory.

Apache Maven 2.2.0 or higher

http://maven.apache.org Required. Add bin/ directory to your PATH. You will need internet access to use Maven; configure proxy settings if necessary for your network.

FUSE ESB Enterprise

7.1.0-fuse-047

http://fusesource.com Required. Archive can be downloaded and extracted. Make sure installation directory does not have a space in the path.

FileZilla FTP Latest FileZilla Server

http://sourceforge.net/projects/filezilla

Optional. Could be used for running the FTP Exercise.

Page 47: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 47

Appendix B: Additional Software Tool Notes

Using jconsole to query JMS queues Some of the solutions make use of JMS queues; you will need to be able to view the contents of these queues to verify that the integration flow has done what you thought it should. When using the embedded ActiveMQ within JBoss Fuse , you use a tool such as the JDK jconsole to connect to your ActiveMQ queues using JMX. There are a number of ways you can connect using jconsole:

• ‘Remote’: You may connect to JBoss Fuse using JMX on port 1099 with username ‘smx’ and password ‘smx’.

• ‘Local’: jconsole can detect JVMs on the local machine; JBoss Fuse should be listed as org.apache.Fuse ESB.kernel.main.Main.

• ‘Advanced’: You can use the following JMX URI to connect to ESB 4, with username ‘smx’ and password ‘smx’. service:jmx:rmi:///jndi/rmi://localhost:1099/karaf-root

When you are connected via JMX, you can examine queues by looking under the org.apache.activemq MBeans. For example, to see the queue ‘Foo’, look under org.apache.activemq → default → Queue → Foo. You can then examine the attributes of the queue, for example, queueSize, or, alternatively, use some of the operations on the queue, for example, purge(). One of the operations available on a queue or topic is to browseMessages which provides access to the contents of all the messages on a the given queue or topic. However, the display of the message contents is a bit primitive, so you may want to consider using the open source JMS viewer HermesJMS described later in this chapter.

Using JBoss Fuse webconsole In addition to the command-line console for JBoss Fuse administration, there is also a browser-based tool. The web console is not installed by default. To install it, run the following from the JBoss Fuse prompt: features:install webconsole To access the console for an instance of JBoss Fuse running locally, enter the following address in your web browser:

Page 48: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 48

http://localhost:8181/system/console Enter default user ‘smx’ password ‘smx’.

Page 49: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 49

Appendix C: Using JBoss Fuse for JBoss Camel Exercises

File Input and Output Folders When the exercises are deployed inside JBoss Fuse, the exercises will automatically create a directory hierarchy ‘/camel-exercises/[exercise shortcut like ‘cbr’]’. This is done so that the various exercises in/out folders do not interfere with each other. In case of problems this entire directory can be deleted after JBoss Fuse is shut down.

Review of the JBoss Fuse ‘features’ mechanism If you intend to deploy the solutions in JBoss Fuse, then you should take some time to understand the ‘features’ mechanism in JBoss Fuse. A ‘feature’ is a set of OSGi bundles that are deployed together to form part of a solution; features can themselves reference or ‘include’ other features. Features are described using a simple XML file format. This is referred to as a feature descriptor. A good example of a maven project that creates a features descriptor that represents this exercise system is provided in the “features” directory. You can use this pattern when creating features that represent your own applications. If you have built the exercises, the feature descriptor will already have been created by maven, and installed into your local maven repo. If you are going to install the solutions into JBoss , then you will need to add the resulting feature to the JBoss runtime. Do this using the ‘features’ command in the JBoss shell. karaf@root> features:addurl Nmvn:com.fusesource.training/features/2010.07.12/xml/features Once installed into JBoss Fuse, you can see the features file used to describe the solutions in this exercise system. Use the following command to see the feature descriptor: karaf@root> cat Nmvn:com.fusesource.training/features/2010.07.12/xml/features You can also list the features using ‘features:list’; as there may be many features now installed you should consider filtering the results by piping to ‘grep camel-exercises’. karaf@root> features:list | grep camel-exercises [installed ] [0.0.0 ] camel-exercises-cbr repo-0 [installed ] [0.0.0 ] camel-exercises-ftp repo-0 [installed ] [0.0.0 ] camel-exercises-splitter repo-0 …

We will use the ‘features’ command to deploy exercise solutions into JBoss throughout the remainder of this course.

Page 50: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 50

Sample data Throughout the exercises we will drive some files through our integration flows; these initial data files are located in the src/test/resources/camel/in folder of each project. These files are copied to the ./target/test-classes/camel/in folder during build/install step.

Page 51: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 51

Appendix D: Content Based Router

Deploying the Solution in JBoss To deploy the solution into JBoss Fuse, install the ‘cbr’ OSGI bundle by entering: > cd ~/JB421 Make sure JBoss Fuse is running. If not, you can launch it with > cd ~/JB421/software/jboss-fuse* > bin/fuse karaf@root> osgi:install –s mvn:com.fusesource.training/cbr/2010.07.12

Validating the Solution For the JBoss Fuse deployment, the XPath router listens on the directory /camel-exercises/cbr/in/xml for XML files. • Copy one or more sample files from the ./target/test-classes/camel/in/xml

directory into /camel-exercises/cbr/in/xml. • They will ‘disappear’ after a short interval, and be processed by the route and new files

should appear in /camel-exercises/cbr/out/xml after a few seconds. Also for the JBoss Fuse deployment, the header-based router listens on the directory /camel-exercises/cbr/in/csv. • Copy one or more sample files from the ./target/test-classes/camel/in/csv

directory. • Again, they will ‘disappear’ after a short interval, and be processed by the route and new

files should appear in /camel-exercises/cbr/out/csv after a few seconds. You may also wish to view what is being logged in the JBoss Fuse log files by entering the command

log:display -n 10. This will display that last 10 messages logged. You may also enter the command

log:tail -n 10

Page 52: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 52

which will display the last 10 messages logged AND also continue to monitor the log file and display as messages are received. To cancel the tail command (which will take over your JBoss Fuse command window while waiting for log file additions, simply enter CTRL+C.

Page 53: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 53

Appendix E: Content Based Router - Endpoints

Overview This exercise uses Injection mechanism of Apache Camel to configure the @Endpoints used in the Camel route. The RouteBuilder class no longer implements the Spring interfaces. Instead, it will configure each Endpoint individually using the following annotation and Java Field. @EndpointInject(ref = "sourceDirectoryXml") Endpoint sourceUri; The Endpoint bean is instantiated by Spring and added to the Camel Context like that

<endpoint id="sourceDirectoryXml" uri="${sourceDirectoryXmlUri}"/>

Combining this with a property placeholder or using Config Admin of JBoss Fuse externalizes the configuration of the endpoint in a property file sourceDirectoryXmlUri=file:/camel-exercises/cbr/in/xml

Review of Solution

Deploying the Solution in JBoss To deploy the solution into JBoss , enter the following command: karaf@root> features:install camel-exercises-cbr-endpoints AND copy the file cbr-endpoints/src/main/resources/camel-cbr.cfg under etc directory of JBoss Fuse

Validating the Solution For the JBoss Fuse deployment, the XPath router listens on the directory /camel-exercises/cbr/in/xml for XML files. • Copy one or more sample files from the ./target/test-classes/camel/in/xml

directory into /camel-exercises/cbr/in/xml. • They should ‘disappear’ after a short interval, and be processed by the route and new files

should appear in /camel-exercises/cbr/out/xml after a few seconds.

Page 54: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 54

Also for the JBoss Fuse deployment, the header-based router listens on the directory /camel-exercises/cbr/in/csv.

• Copy one or more sample files from the ./target/test-classes/camel/in/csv directory.

• They will ‘disappear’ after a short interval, and be processed by the route and new files should appear in /camel-exercises/cbr/out/csv after a few seconds.

Page 55: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 55

Appendix F: Camel Exceptions

Deploying the Solution in JBoss To deploy the solution into JBoss , enter the following command: karaf@root> features:install camel-exercises-exception

Validating the Solution For the JBoss Fuse deployment, the XPath router listens on the directory /camel-exercises/exception/in/xml for XML files. • Copy one or more sample files from the ./target/test-classes/camel/in/xml

directory into /camel-exercises/exception/in/xml. • They will ‘disappear’ after a short interval and processing will occur as discussed earlier

this document with the maven camel plug approach.

Page 56: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 56

Appendix G: Route CSV files from FTP

Deploying the Solution in JBoss To deploy the solution into JBoss , install the ‘ftp’ feature from the JBoss Fuse shell. karaf@root> features:install camel-exercises-ftp

Validating the Solution For the JBoss Fuse deployment, the instructor will provide the steps necessary to validate the solution. You will need to upload the test files directly into the FTP server using the ftp client and entering the following commands: cd <camel exercise install folder>/ftp/src/test/camel/in

lftp -u amelia,amelia instructor.example.com cd station<N> <<<<<< Be sure to do this step <<<<<< where "N" is your student # put EUPayments.txt put USPayments.txt exit Once the files have been uploaded, your route will find them and process as shown below. Alternatively, you may have the instructor place the appropriate test files into the appropriate FTP server folders so that they can be accessed by your specific instance of the JBoss Fuse installation. Verify that the file(s) has been processed by examining the logs; you should see a log statement for each line of data in the file, example: Processing payment from '12345467' to '43211234', amount: 10000.0 Processing payment from '34563456' to '87989877', amount: 10000.0 The log will be found in the JBoss Fuse log which can be displayed using: log:display –n 10 from within the Karaf/ JBoss Fuse terminal window. As an optional exercise, see if you can modify the appropriate option passed to the Camel FTP component (within PaymentAmountExtractor.java) such that Camel deletes the file from the FTP server on processing. Recompile and redeploy.

Page 57: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 57

Appendix H: Using the Splitter

Setting up the Broker A JMS broker is required for this exercise. See the maven plugin version earlier in this document as to starting up an ActiveMQ broker instance.

Deploying the Solution in JBoss To deploy the solution into JBoss , install the ‘splitter’ feature from the JBoss Fuse shell. karaf@root> features:install camel-exercises-splitter

Validating the Solution When the route is deployed into JBoss Fuse, we have two options for triggering the camel splitter route. One option is to publish a message to the Payments.XML queue. This could be done using JConsole or JBoss Fuse Web Console. Your lab instructor can you show you how to publish/view messages to ActiveMQ using one of these tools. If you inspect the Spring context file at (src/main/resources/META-INF/spring/bundle-context.xml) along with the code (see SplitIntoSinglePaymentsRouter.java), you will see that there is an optional way to trigger the route. A “helper” route defined here will move the contents of any file you drop in ‘/camel-exercises/splitter/in’ onto the Payments.XML queue to begin the processing. Results from splitting the message will be placed onto IndividualPayments.XML queue. You can validate by observing the message count on that queue. Providing an ‘alternative’ entry point into a route for ease of testing (such as file) is a common pattern when implementing Camel routes.

Page 58: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 58

Appendix I: Using the Aggregator

Deploying the Solution in JBoss To deploy the solution into JBoss , install the ‘aggregator’ feature from the JBoss Fuse shell. karaf@root> features:install camel-exercises-aggregator

Validating the Solution To validate the solution, first ensure that the aggregator routes are running. Also, ensure that the logging level has been set to DEBUG for the com.source logger.

• From within JBoss Fuse, then set the log level from the console using: karaf@root> log:set DEBUG com.fusesource

When deployed in JBoss Fuse all payments XML files should be copied into /camel-exercises/aggregator/in directory so that the aggregator can pick them up. At the end of the route the resulting messages are printed to the log as well as written to the file system. Each aggregated set of messages is combined into a single output message. There will be three sets of aggregated messages, thus you should find 3 files (containing multiple messages each) in /camel-exercises/aggregator/out directory. The produced files are named output-<UUID>-<n>.xml where <UUID> is a universal unique identifier and <n> is a sequentially increasing integer.

Page 59: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 59

Appendix J: Reliable message processing with JMS flows

Initializing a JMS Broker

This exercise does require an active JMS broker. See the earlier section on how to start up an ActiveMQ broker instance.

Deploying the Solution in JBoss Fuse To deploy the solution into JBoss Fuse, install the ‘jms-transaction’ feature from the JBoss Fuse shell. karaf@root> features:install camel-exercises-jms-transaction

Validating the Solution For validation of route deployed inside JBoss Fuse copy the file ./src/test/resources/camel/in/VariousUSPayments.xml to the /camel-exercises/jms-transactions/in directory. It will be picked up by the route and processed, placing the content of the file on the outgoingPayments queue. Examine the log messages produced by the route to verify that the processor was executed appropriately. After the processing the output of the route will be into files inside the /camel-exercises/jms-transactions/out directory. Do the same for the VariousEUPayments.xml file; again, it should be processed successfully. Now try the VariousUnknownPayments.xml, which has a currency value of ‘???’. The processing code in the route will reject the payment, forcing a JMS rollback and consequent redelivery. After the specified number of retries, the message will be moved to the dead-letter-queue, ‘ActiveMQ.DLQ’.

Page 60: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 60

Appendix K: Creating and deploying a custom component.

Deploying the Solution in JBoss Fuse To deploy the solution into JBoss Fuse, install the ‘custom-file-component’ feature from the JBoss Fuse shell. karaf@root> features:install camel-exercises-custom-file-component After that we need to deploy a route to test the custom file component. In order to deploy a route copy the ‘file.comp.spring-context.xml’ from the ‘src/test/resources/deploy’ directory into the JBoss Fuse’s deploy directory.

Validating the Solution When deployed inside JBoss Fuse, to validate the solution, copy one of the sample files from ./target/test-classes/camel/in directory into the /camel-exercises/file.comp/in directory. You should see that it will be renamed with an underscore prefix; the filecomp: component will ignore files beginning with an underscore so the file is only processed once. The FileProducer at the end of the route will create a new copy of the original file and put it into this directory: /camel-exercises/file.comp/out/ using a file name of the form _out_<n>.

Page 61: JBoss Camel - Red Hatpeople.redhat.com/tgubelig/fuseEsb/Camel-ExerciseBook-V1.3.pdf · In this exercise you will review the POM files in order to understand the structure, and to

Camel Development with JBoss Fuse V1.3 August 2013

Copyright © 2013 Red Hat, Inc. All rights reserved 61

Appendix L: Exposing a WebService

Deploying the Solution in JBoss Fuse To deploy the solution into JBoss Fuse, install the ‘webservice feature from the JBoss Fuse shell. karaf@root> features:install camel-exercises-webservice

Validating the Solution To validate the solution, first ensure that the webservice routes are running. Next copy the file customer.text from the location ./target/classes/data to /camel-exercises/webservice. Then verify on the console that you receive the following response from the Web Service called : Camel (camel-1) thread #1 - file:///camel-exercises/webservices INFO [route1] - SOAP Response received : <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:Envelope xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns3="http://training.fusesource.com/"> <ns2:Body> <ns3:customer> <name>Fuse Students</name> <address>FuseSource Office</address> <numOrders>28</numOrders> <revenue>1977.0</revenue> <test>100.0</test> <type>PRIVATE</type> </ns3:customer> </ns2:Body> </ns2:Envelope>

When deployed in JBoss Fuse, you can verify the process as we have done when running the camel routes using the maven plugin.