Setting up Apache Karaf with Camel and ActiveMQ-Camel

ActiveMQ / Camel / Karaf

In this post I’ll cover installing Apache Camel within the OSGi container Apache Karaf with the activemq-camel component.  I’m doing this setup in Windows, however the process should be similar on *nix, you’ll just need to setup your environment and path variables accordingly.  As this was my first time experimenting with Karaf, it took me an entire day to figure out how to get everything working smoothly, so I hope that this walk-through can save somebody else the time and headaches of troubleshooting and searching for the seemingly sparsely-documented process.

Getting Started

I’m assuming that you’ve got the Java 1.6 JDK installed.  You’ll want to set your “JAVA_HOME” environment variable to your Java JDK installation path.  My JAVA_HOME is set to C:\Program Files\Java\jdk1.6.0_26.  Note that Karaf does not seem to work with Java 1.7.

I’m also assuming that you’ve got Apache ActiveMQ up-and-running.  This walk-through is based on ActiveMQ 5.5.0 and Camel 2.8.0.

Apache Downloads

To get started, you’ll want to download the following binary distributions.  I installed them under C:Apache (e.g. C:Apacheapache-karaf-2.2.2).  Note that Karaf does not seem to like having spaces in its install path.

Environment Variables

  • Set the “M2_HOME” environment variable to your Maven install path.  Mine is set to “C:Apacheapache-maven-3.0.3″.
  • Set the “M2″ environment variable to your Maven bin path.  Mine is set to “%M2_HOME%\bin”.
  • Set the “KARAF_HOME” environment variable to your Karaf install path.  Mine is set to “C:Apacheapache-karaf-2.2.2″.
  • Add the “M2″ environment variable to your “Path” environment variable (e.g. add “;%M2%” to the end of your “Path” variable).

Starting Karaf

Open a new command prompt and enter “cd %KARAF_HOME%” and then “binkaraf.bat”.  Alternatively you can double-click the “karaf.bat” file in the bin directory from Windows explorer.  You may get a Windows Firewall prompt at this point; allow the operation.

At this point, you should see Karaf starting up.  The initial screen looks like this:

karaf.bat: Ignoring predefined value for KARAF_HOME
__ __                  ____
/ //_/____ __________ _/ __/
/ ,<  / __ `/ ___/ __ `/ /_
/ /| |/ /_/ / /  / /_/ / __/
/_/ |_|__,_/_/   __,_/_/

Apache Karaf (2.2.2)

Hit '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
Hit '<ctrl-d>' or 'osgi:shutdown' to shutdown Karaf.

karaf@root>

I prefer to use an SSH client such as PuTTY to access Karaf instead of the command prompt window as it seems a little bit touchy in Windows.  The following will tell you which port the SSHD is listening on:

karaf@root> sshd
SSH server listening on port 8101
karaf@root>

The default username is “karaf” and the password is also “karaf”.

Installing Camel

The following page describes some of this process: http://camel.apache.org/karaf.html.

First of all, the Camel project has prepared a special properties file for Camel to run in Karaf.  This file can be downloaded at http://svn.apache.org/repos/asf/camel/tags/camel-2.7.2/platforms/karaf/features/src/main/resources/config.properties.  Save this file as “camel.properties” and place it in your “%KARAF_HOME%\ext” directory.

Next, edit the “jre.properties” file in “%KARAF_HOME%\ext”.  Find the entries for “javax.activation” and “javax.annotation” and comment them out as follows:

# Standard package set.  Note that:
#   - javax.transaction* is exported with a mandatory attribute
jre-1.6=
 javax.accessibility,
# javax.activation,
 javax.activity,
# javax.annotation,

Instead, we’ll be exposed to these packages via geronimo.  Enter the following commands to install geronimo-annotation_1.1_spec and geronimo-activation_1.1_spec (note that your Bundle ID’s may be different than mine):

karaf@root> osgi:install -s mvn:org.apache.geronimo.specs/geronimo-annotation_1.1_spec/1.1-SNAPSHOT
Bundle ID: 69
karaf@root> osgi:install -s mvn:org.apache.geronimo.specs/geronimo-activation_1.1_spec/1.2-SNAPSHOT
Bundle ID: 70

Next, enter the following commands to install Camel:

karaf@root> features:addurl mvn:org.apache.camel.karaf/apache-camel/2.8.0/xml/features
karaf@root> features:install camel
karaf@root>

This should install the following Karaf features:

  • camel/2.8.0
  • camel-core/2.8.0
  • camel-spring/2.8.0
  • spring/3.0.5.RELEASE
  • spring-dm/1.2.1.RELEASE

You can verify that these features were installed by issuing the following command (I’ve filtered the list):

karaf@root> features:list
State         Version           Name                          Repository             Description
[installed  ] [2.8.0          ] camel                         camel-2.8.0
[installed  ] [2.8.0          ] camel-core                    camel-2.8.0
[installed  ] [2.8.0          ] camel-spring                  camel-2.8.0
[installed  ] [3.0.5.RELEASE  ] spring                        karaf-2.2.2
[installed  ] [1.2.1          ] spring-dm                     karaf-2.2.2

Installing ActiveMQ-Camel

The activemq-camel component is distributed with ActiveMQ, not camel, so we will need to add the ActiveMQ repository to the Karaf features.  The following page describes this process: http://activemq.apache.org/osgi-integration.html.  Note that ActiveMQ is a dependency, so we will also be installing the activemq feature.

In a nutshell, enter the following commands to install ActiveMQ-Camel:

karaf@root> features:addUrl mvn:org.apache.activemq/activemq-karaf/5.5.0/xml/features
karaf@root> features:install activemq
karaf@root> features:install camel-jms
karaf@root> osgi:install -s mvn:org.apache.activemq/activemq-camel/5.5.0
Bundle ID: 90

Now, you should be good to go!

Configuring Routes

I generally prefer to use Spring XML to configure routes, and it works quite well because Karaf automatically re-loads Camel configurations when changes are made.  Here is an example “camel.xml” file.  It should be placed in the “%KARAF_HOME%\deploy” folder.

    <beans
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  

       <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring" autoStartup="true">
            <route id="example-route" autoStartup="true">
                <description>Example Camel Route</description>
                <from uri="activemq:example.A"/>
                <to uri="activemq:example.B"/>
            </route>
        </camelContext>  

        <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="tcp://localhost:61616" />
        </bean>  

        <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
            <property name="maxConnections" value="8" />
            <property name="maximumActive" value="500" />
            <property name="connectionFactory" ref="jmsConnectionFactory" />
        </bean>  

        <bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
            <property name="connectionFactory" ref="pooledConnectionFactory"/>
            <property name="transacted" value="false"/>
            <property name="concurrentConsumers" value="10"/>
        </bean>  

        <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
            <property name="configuration" ref="jmsConfig"/>
        </bean>
    </beans>

This config creates a Camel route which consumes messages from the “example.A” queue and publishes them to the “example.B” queue (essentially just moving them).  Note the autoStartup=”true” attributes; when the Camel context(s) are loaded by Karaf, Camel does not automatically start them or their routes.  This attribute tells Camel to automatically start up the context and any routes with the attribute as well, which prevents you from having to use the Karaf console to start each route manually with any configuration changes.

Conclusion

We started with a fresh installation of Karaf, installed Camel, installed the ActiveMQ component for Camel and setup an example route.  Hopefully this process works as flawlessly for you as it does for me.  If you have any questions or run into any problems, feel free to post in the comments and I’ll try to see if I know what may have caused them.

Cheers!

Cyle


Programming enthusiast. I've been intrigued by computers since I was 12, staying in at recess to code QBASIC on the old Apple II. Background in the payment industry, particularly in card switching and related system architecture. Lover of high-performance distributed architecture. Huge fan of the new NoSQL wave. Open source fanatic.

2 Comments

  1. surya
    18 November 11, 6:16am

    thanks for your post. can you please help how do the same with camel-guice routes. Bascially want to do the same with guicecamelcontext. many thanks in adavane.

    • 14 December 11, 4:57pm

      Hi there!

      I’m sorry, but I have to be totally honest and say that I really know nothing about Guice as I’m more of a .NET junkie :-(

      You didn’t have any luck with http://camel.apache.org/guice.html?

Leave a Reply