Stuart winchester in Java 15 minutes

Quick Example With Fuse7 Camel And Karaf2

This is just a quick blog post to show how to get up and running quickly with Fuse 7.2 using a slightly older archetype.

Generating a Fuse/Karaf2 Project

The latest Fuse archetype catalog can be found at maven.repository.redhat.com

I will always recommend starting a project by using an archetype, we will generate a project using the latest archetype as follows:

$ mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate \
  -DarchetypeCatalog=https://maven.repository.redhat.com/earlyaccess/all/io/fabric8/archetypes/archetypes-catalog/2.2.195.redhat-000026/archetypes-catalog-2.2.195.redhat-000026-archetype-catalog.xml \
  -DarchetypeGroupId=org.jboss.fuse.fis.archetypes \
  -DarchetypeArtifactId=karaf2-camel-amq-archetype \
  -DarchetypeVersion=2.2.195.redhat-000026 \
  -DgroupId=com.nullendpoint \
  -DartifactId=karaf2-camel-amq-example \
  -Dversion=1.0.0

Navigate into the karaf2-camel-amq-example directory and we can notice in the pom.xml that the dependency management is not up to date with the latest BOM from the migration guide, so this will need to be changed.

Migrating to Fuse 7.2 / Karaf2

The dependency management is now using a different BOM and we need to reflect this change in portions of the pom.xml. From this:

  <properties>
    <!-- configure the versions you want to use here -->
    <fabric8.version>2.2.170.redhat-000031</fabric8.version>
    <karaf.plugin.version>4.0.8.redhat-000050</karaf.plugin.version>
  </properties>


  <dependencyManagement>
    <dependencies>
      <!-- import fabric8 platform bom first -->
      <dependency>
        <groupId>io.fabric8</groupId>
        <artifactId>fabric8-project-bom-fuse-karaf</artifactId>
        <version>${fabric8.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

To this:

  <properties>
    <!-- configure the versions you want to use here -->
    <fuse.version>7.2.0.fuse-720020-redhat-00001</fuse.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.jboss.redhat-fuse</groupId>
        <artifactId>fuse-karaf-bom</artifactId>
        <version>${fuse.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

Also, the karaf-maven-plugin and fabric8-maven-plugin need to change in order to use the latest fuse.version. We will also replace the whole karaf-maven-plugin set to include a some extra features.

It is also worth noting that <feature>activemq-camel</feature> is now <feature>camel-activemq</feature> for AMQ6 communication.

From this:

      <!-- 2. create karaf assembly -->
      <!-- karaf-maven-plugin creates custom microservice distribution -->
      <plugin>
        <groupId>org.apache.karaf.tooling</groupId>
        <artifactId>karaf-maven-plugin</artifactId>
        <version>${karaf.plugin.version}</version>
        <extensions>true</extensions>

      <!-- OMMITTED -->

      <!-- 3. create docker image -->
      <plugin>
        <groupId>io.fabric8</groupId>
        <artifactId>fabric8-maven-plugin</artifactId>
        <version>${fabric8.maven.plugin.version}</version>
        <executions>

      <!-- OMMITTED -->     

To this:

      <!-- 2. create karaf assembly -->
      <!-- karaf-maven-plugin creates custom microservice distribution -->
      <plugin>
        <groupId>org.jboss.redhat-fuse</groupId>
        <artifactId>karaf-maven-plugin</artifactId>
        <version>${fuse.version}</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <id>karaf-assembly</id>
            <goals>
              <goal>assembly</goal>
            </goals>
            <phase>install</phase>
          </execution>
        </executions>
        <configuration>
          <javase>1.8</javase>
          <framework>framework</framework>
          <useReferenceUrls>true</useReferenceUrls>
          <archiveTarGz>false</archiveTarGz>
          <includeBuildOutputDirectory>false</includeBuildOutputDirectory>
          <startupFeatures>
            <feature>framework</feature>
            <feature>jaas</feature>
            <feature>log</feature>
            <feature>shell</feature>
            <feature>bundle</feature>
            <feature>feature</feature>
            <feature>system</feature>
            <feature>scr</feature>
            <feature>management</feature>
            <feature>aries-blueprint</feature>
            <feature>camel-blueprint</feature>
            <feature>camel-jms</feature>
            <feature>activemq-broker</feature>
            <feature>activemq-client</feature>
            <feature>camel-activemq</feature>
            <feature>kubernetes-client</feature>
            <feature>openshift-client</feature>
            <feature>fabric8-karaf-blueprint</feature>
            <feature>fabric8-karaf-checks</feature>
          </startupFeatures>
          <startupBundles>
            <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version};start-level=80</bundle>
          </startupBundles>
        </configuration>
      </plugin>

      <!-- OMMITTED -->

      <!-- 3. create docker image -->
      <plugin>
        <groupId>org.jboss.redhat-fuse</groupId>
        <artifactId>fabric8-maven-plugin</artifactId>
        <version>${fuse.version}</version>

      <!-- OMMITTED -->        

Once these changes are made you’ll be able to mvn clean package karaf:assembly the project. This will create a full karaf container in your target folder. There may be some different commands, so it’s worth looking at what commands have changed between karaf versions.

Running the Project

Here we navigate into the target directory and execute the karaf distribution which has been packages from the maven build.

cd target
[user@host]$ assembly/bin/karaf 
Unable to register security provider: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider
        __ __                  ____      
       / //_/____ __________ _/ __/      
      / ,<  / __ `/ ___/ __ `/ /_        
     / /| |/ /_/ / /  / /_/ / __/        
    /_/ |_|\__,_/_/   \__,_/_/         

  Apache Karaf (4.2.0.fuse-720061-redhat-00001)

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

karaf@root()>

Just to show that we have our application deployed:

karaf@root()> bundle:list
START LEVEL 100 , List Threshold: 50
 ID │ State  │ Lvl │ Version                         │ Name
────┼────────┼─────┼─────────────────────────────────┼──────────────────────────────────────────────────────────
123 │ Active │  50 │ 2.21.0.fuse-720050-redhat-00001 │ camel-blueprint
124 │ Active │  50 │ 2.21.0.fuse-720050-redhat-00001 │ camel-core
125 │ Active │  50 │ 2.21.0.fuse-720050-redhat-00001 │ camel-jms
126 │ Active │  60 │ 3.0.11.fuse-720027-redhat-00001 │ Fabric8 :: Karaf :: Core
127 │ Active │  65 │ 3.0.11.fuse-720027-redhat-00001 │ Fabric8 :: Karaf :: Blueprint
128 │ Active │  65 │ 3.0.11.fuse-720027-redhat-00001 │ Fabric8 :: Karaf :: Checks
129 │ Active │  80 │ 1.0.0                           │ Fabric8 :: Quickstarts :: Karaf 2 :: Camel and ActiveMQ

The full karaf2 console reference is available here: Apache Karaf Fuse 7.2 Console Guide

A working example of the above can be found on my GitHub

References:

. Fuse 7.2 Migration Guide