Sunday 19 October 2014

A Fun Camel Ride With Apache Camel Part Three Understanding Your Project Layout.

This section is the final piece before we start deep diving into the code and techniques. I found that it helps that when following a tutorial it makes you familiar with navigating the tutorial in the IDE. Some of this might be basic knowledge for you folks out there.

The project explorer is a pretty standard view in eclipse so I am going to show you where the various pieces of the project lie in the in this view. When working with Apache Camel projects you will find you mostly work with source code, a xml route configuration file(unless you are doing it completely in Java) and Maven dependencies.

First lets look at the camel route definition/OSGI blue-print XML file. This can be found under the Camel Contexts folder and also under the src/main/resources/OSGI-INF/blueprint folder  in the project explorer view.

Camel Route Definitions


These two files contains the XML configuration for the route we will be modifying. If you click on either of these two files eclipse will open a graphical representation of this file. Here you can visually see your camel route. Hold on you say I did not define a route yet what is this? The camel archetype by default creates a example route and this is what you will see. We will modify this shortly. The following screenshot is an example of what you will see.

A Visual Representation Of The Camel Route In Eclipse
You can also view the xmlby clicking on the source tab and manually modify the xmlfile by hand. On the right there is a palette where you can find the various camel endpoints, routing, control flow and transformation components used in your camel routes. These can be dragged and dropped and then connected using the visual designer. You can view the properties of the various components by using the properties tab. The properties tab allows you to specify end point url for example.

Properties Of The Endpoint Component

Now onto Maven. The pom.xml can be found by using the project explorer. It also has a visual interface for the xml file or you could edit the xml by hand. The pom file contains a lot of information that can be very useful when you have to manages several camel routes.

The pom file allows you to add some pretty descriptive information which can be used to document and identify what a route does. I added some information this information will later be visible in your runtime container i.e. Apache Karaf and it can really help to manage things if you fill it in correctly

Adding Descriptive Information To Your Project Object Model
As for the source code your files can be found at the usual spot of the src/main/java folder. At this point you should have a good handle on how to edit the project. Now there is one last little hurdle to getting this tutorial into high gear.

Since this is not a normal Java application you cannot just run the route or debug it. You need to configure the debug and run steps. This is very simple all you have to do is create a Maven configuration/profile that can be used for both running and debugging.

In the project explorer right click on the project(any folder/node in the project should also work) then from the popup menu choose Debug As->Debug Configurations. This will bring a screen with various debug/run options. There will be a Maven section which you need to click/select. Then click the new button in the top menu and fill it in as follows:

  • Name: CamelMusic
  • Goals: camel:run
One last thing to do is to set the base directory which is done by clicking the browse workspace button and then choosing the camelmusic project as the base directory.  See the screenshot below for what it should look like.
Creating The Maven Run/Debug Configurations
Lets quickly test that the configuration is working. Click the debug button and Maven will kick in and download some dependencies. If this is your first time with the JBoss Fuse tooling plug-in it might take a while before the route starts running. If all goes well you will see something like the following lines appear in the output window of eclipse.


[INFO] Scanning for projects...
Downloading: http://repo.fusesource.com/nexus/content/groups/ea/org/apache/maven/plugins/maven-install-plugin/maven-metadata.xml
Downloading: http://repo.fusesource.com/nexus/content/repositories/snapshots/org/apache/maven/plugins/maven-install-plugin/maven-metadata.xml
Downloading: http://repo.fusesource.com/nexus/content/repositories/releases/org/apache/maven/plugins/maven-install-plugin/maven-metadata.xml
Downloading: http://repo.fusesource.com/nexus/content/repositories/releases/org/apache/maven/plugins/maven-surefire-plugin/maven-metadata.xml
Downloading: http://repo.fusesource.com/nexus/content/groups/ea/org/apache/maven/plugins/maven-surefire-plugin/maven-metadata.xml
Downloading: http://repo.fusesource.com/nexus/content/repositories/snapshots/org/apache/maven/plugins/maven-surefire-plugin/maven-metadata.xml
Downloading: http://repo.fusesource.com/nexus/content/repositories/releases/org/apache/maven/plugins/maven-deploy-plugin/maven-metadata.xml
Downloading: http://repo.fusesource.com/nexus/content/repositories/snapshots/org/apache/maven/plugins/maven-deploy-plugin/maven-metadata.xml
Downloading: http://repo.fusesource.com/nexus/content/groups/ea/org/apache/maven/plugins/maven-deploy-plugin/maven-metadata.xml
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building My Camel Music Route 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> camel-maven-plugin:2.12.0.redhat-610379:run (default-cli) @ camelmusic >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ camelmusic ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ camelmusic ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ camelmusic ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ camelmusic ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< camel-maven-plugin:2.12.0.redhat-610379:run (default-cli) @ camelmusic <<<
[INFO] 
[INFO] --- camel-maven-plugin:2.12.0.redhat-610379:run (default-cli) @ camelmusic ---
[INFO] Using org.apache.camel.test.blueprint.Main to initiate a CamelContext
[INFO] Starting Camel ...
[mel.test.blueprint.Main.main()] MainSupport          INFO  Apache Camel 2.12.0.redhat-610379 starting
[mel.test.blueprint.Main.main()] Activator            INFO  Camel activator starting
[mel.test.blueprint.Main.main()] Activator            INFO  Camel activator started
[mel.test.blueprint.Main.main()] BlueprintExtender    INFO  No quiesce support is available, so blueprint components .....
[Blueprint Extender: 1] BlueprintContainerImpl        INFO  Bundle camelmusic is waiting for namespace handlers [http://camel.apache.org/schema/blueprint]
[Blueprint Extender: 1] BlueprintCamelContext         INFO  Apache Camel 2.12.0.redhat-610379 (CamelContext: blueprintContext) is starting
[Blueprint Extender: 1] ManagedManagementStrategy     INFO  JMX is enabled
[Blueprint Extender: 1] BlueprintCamelContext         INFO  AllowUseOriginalMessage is enabled. If access to the original message.....
[Blueprint Extender: 1] BlueprintCamelContext         INFO  StreamCaching is not in use. If using streams then its recommended....
[Blueprint Extender: 1] BlueprintCamelContext         INFO  Route: timerToLog started and consuming from: Endpoint[timer://foo?period=5000]
[Blueprint Extender: 1] BlueprintCamelContext         INFO  Total 1 routes, of which 1 is started.
[Blueprint Extender: 1] BlueprintCamelContext         INFO  Apache Camel 2.12.0.redhat-610379 (CamelContext: blueprintContext) started in 0.521 seconds
[ntext) thread #0 - timer://foo] timerToLog                INFO  The message contains Hi from Camel at 2014-10-19 19:40:07
[ntext) thread #0 - timer://foo] timerToLog                INFO  The message contains Hi from Camel at 2014-10-19 19:40:11

At this point we are ready to start really working with Apache Camel in detail. The next few entries in the tutorial is going to be focussed on getting the basic route up and running. Then quickly move into the advanced techniques required to scale a Camel route to some serious performance.

No comments:

Post a Comment