Willkommen bei bytebang » The blog about all and nothing » Helloworld with Apache Camel using Ivy and Ant

Helloworld with Apache Camel using Ivy and Ant

Jul 18 2016

The Problem

Apache camel is a prefect approach to build any kind of interfaces between two distinct software packages - like an PLM and an ERP system. It can act as glue-code between these systems in order to transfer data between them.

However, if you have never heard of it, then you want most probably to get this thing up and running with as little effort as possible. This article shows you how to avoid maven by using Apache Ivy for the dependency management and how to build the project with Apache ant.

Ingredients

The following recipe has been tested under Eclipse Mars, running on my Ubuntu 15.04 box.

Camel

Wikipedia states: "Apache Camel is a rule-based routing and mediation engine that provides a Java object-based implementation of the Enterprise Integration Patterns using an API (or declarative Java Domain Specific Language) to configure routing and mediation rules.".

In general the camel framework comes as a base component (which has some dependencies to different jar files) and optional other endpoint components.

Without going into deep, we can state that an endpoint is a special component that reads or writes data. This data is processed in something called 'route'. A route can be (depending on your use case) very simple or rather sophisticated - but in general it follows the pattern: Read - Process - Write. This example illustrates what I mean: It generates every two seconds an output message on System.out.

package at.bb.hellocamel;


import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class Startup
{
public static void main(String[] args) throws Exception
{
  CamelContext context = new DefaultCamelContext();
 try
 {
   context.addRoutes(new RouteBuilder()
  {
   @Override
   public void configure() throws Exception
   {
    
    // Say hello
    from("timer://myTimer?period=2000").setBody()
      .simple("Hello World Camel fired at ${header.firedTime}").to("stream:out");
   }
  });
   context.start();
   Thread.sleep(10000);
 }
 finally
 {
   context.stop();
 }
}
}

The framework comes as a bunch of jar files, with various dependencies to other files. Those of you who are Java developers which are using external libraries know about the pain of dependency management. This is why people have invented Maven - which is in my opinion a perfect dependency management tool (meaning it is able to resolve all dependent jar files in the correct version) but it is for my taste powered by too much black magic. I prefer another one: Ivy.

Ivy

Apache Ivy is Maven without the black magic. It can be used to resolve dependencies - and that's it. Therefore it utilizes two files:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ivysettings>
<ivysettings>
<settings defaultResolver="default-chain"/>
<resolvers>
 <ibiblio name="jboss" root="http://repository.jboss.org/" m2compatible="true"/>
 <ibiblio name="ibilio" m2compatible="true"/>
 <chain name="default-chain">
  <resolver ref="jboss"/>
  <resolver ref="ibilio"/>
 </chain>
</resolvers>
</ivysettings>

The first one is called ivysettings.xml which looks like the example above. It tells ivy from where it can fetch the jar files. In general it is not necessary to tweak this one. The example above shows that in addition to the standard maven repository ibiblio another repository named jboss is used. (This serves just as example - for our project it is not necessary to use the jboss repo)

The second file for ivy is the one where we tell it what to fetch: ivy.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
   
   <info organisation="bytebang" module="HelloCamel" status="integration">
</info>

<dependencies>
 <!-- camel stuff -->
 <dependency org="org.apache.camel"  name="camel-core"   rev="2.17.2"/>
 <dependency org="org.apache.camel"  name="camel-stream"  rev="2.17.2"/>
 <dependency org="org.slf4j"   name="slf4j-simple"  rev="1.7.12"/>
</dependencies>
</ivy-module>

It tells ivy something about the project (named HelloCamel, written by bytebang) and the required dependencies. Here we are in a lucky situation: Maven dependencies can (after some reformatting) be used within Ivy as well. The resolver mechanism is the same.

Eclipse plugin

To use Ivy within eclipse, you have to install the Ivy plugin. This is a simple task:

ivy-eclipse.png

This plugin makes eclipse aware of the presence of an ivysettings.xml and of a ivy.xml file. Whenever you change something in one of these files eclipse will try to fetch the dependencies form the internet and to include them into your project build-path.

Ant

Apache Ant is a famous Java build tool which can be used to build projects. I will just leave the result here:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!-- Compiles all the needed Classes -->
<project basedir="." xmlns:ivy="antlib:org.apache.ivy.ant" default="run" name="HelloCamel">
   <property environment="env"/>
   <property name="debuglevel" value="source,lines,vars"/>
   <property name="target" value="1.8"/>
   <property name="source" value="1.8"/>


<property name="version" value="0.1"/>       <!-- Versionnumber -->
<property name="lib.dir" value="vendor"/>        <!-- General library folder -->
<property name="ivy.dir" value="${lib.dir}\ivy"/>     <!-- Directory where ivy should place its dependencies -->
<property name="bin.dir" value="bin"/>       <!-- Compile directory -->
<property name="jar.dir" value="jar"/>        <!-- Export dir for JAR File -->
<property name="main.class" value="at.bb.hellocamel.Startup"/>  <!-- Startclass -->
 
<path id="project.classpath">
       <pathelement location="${bin.dir}"/>
       <pathelement location="${lib.dir}"/>
 <fileset dir="${ivy.dir}">
  <include name="**/*.jar" />
 </fileset>
   </path>
 
<!-- Creates Directories -->
<target name="init">
       <mkdir dir="${bin.dir}"/>
 <mkdir dir="${jar.dir}"/>
       <copy includeemptydirs="false" todir="${bin.dir}">
           <fileset dir="src">
               <exclude name="**/*.launch"/>
               <exclude name="**/*.java"/>
           </fileset>
       </copy>
 
 <!-- Fetches dependencies via ivy -->
 <ivy:settings file="ivysettings.xml" />
 <ivy:retrieve pattern="${ivy.dir}/[type]s/[artifact]-[revision](-[classifier]).[ext]" sync="true"/>
   </target>

<!-- Deletes all the garbage -->
<target name="clean">
       <delete dir="${bin.dir}"/>
 <delete dir="${jar.dir}"/>
 <delete dir="${ivy.dir}"/>
 <!--ivy:cleancache /-->
   </target>
 
<!-- builds the project -->
<target depends="init" name="build">
       <echo message="${ant.project.name}: ${ant.file}"/>
       <javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}" includeantruntime="false">
           <src path="src"/>
           <classpath refid="project.classpath"/>
       </javac>
   </target>
   
<!-- creates a jar file -->
<target name="create_jar" depends="build">
 <buildnumber/>
       <jar destfile="${jar.dir}/${ant.project.name}-${version}.${build.number}.jar" filesetmanifest="mergewithoutmain">
           <manifest>
               <attribute name="Main-Class" value="${main.class}"/>
               <attribute name="Class-Path" value="."/>
           </manifest>
           <fileset dir="${bin.dir}"/>
        <zipgroupfileset dir="${lib.dir}" includes="**/*.jar" />
        <zipgroupfileset dir="${ivy.dir}" includes="**/*.jar" />
       </jar>
   </target>
        
<!-- Calles the created jar file -->
   <target name="run" depends="create_jar">
       <java jar="${jar.dir}/${ant.project.name}-${version}.${build.number}.jar" failonerror="true" fork="yes">
        <arg value="--help"/>
        <classpath refid="project.classpath"/>
       </java>
   </target>
</project>

The syntax of the build.xml file is not that hard to understand. Once you have installed ant (and also the ivy plugin) on you machine Just type ant into the commandline and the project will build itself.

gue@gue-thinkpad:~/HelloCamel$ ant
Buildfile: /home/gue/HelloCamel/build.xml

init:
[ivy:retrieve] :: Apache Ivy 2.4.0 - 20141213170938 :: http://ant.apache.org/ivy/ ::
[ivy:retrieve] :: loading settings :: file = /home/gue/HelloCamel/ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: bytebang#HelloCamel;working@gue-thinkpad
[ivy:retrieve]  confs: [default]
[ivy:retrieve]  found org.apache.camel#camel-core;2.17.2 in ibilio
[ivy:retrieve]  found org.slf4j#slf4j-api;1.7.21 in ibilio
[ivy:retrieve]  found com.sun.xml.bind#jaxb-core;2.2.11 in ibilio
[ivy:retrieve]  found com.sun.xml.bind#jaxb-impl;2.2.11 in ibilio
[ivy:retrieve]  found org.apache.camel#apt;2.17.2 in ibilio
[ivy:retrieve]  found org.apache.camel#spi-annotations;2.17.2 in ibilio
[ivy:retrieve]  found org.osgi#org.osgi.core;4.3.1 in ibilio
[ivy:retrieve]  found org.apache.camel#camel-stream;2.17.2 in ibilio
[ivy:retrieve]  found org.slf4j#slf4j-simple;1.7.12 in ibilio
[ivy:retrieve] :: resolution report :: resolve 384ms :: artifacts dl 14ms
[ivy:retrieve]  :: evicted modules:
[ivy:retrieve]  org.slf4j#slf4j-api;1.7.12 by [org.slf4j#slf4j-api;1.7.21] in [default]
 ---------------------------------------------------------------------
 |                  |            modules            ||   artifacts   |
 |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
 ---------------------------------------------------------------------
 |      default     |   10  |   0   |   0   |   1   ||   15  |   0   |
 ---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: bytebang#HelloCamel [sync]
[ivy:retrieve]  confs: [default]
[ivy:retrieve]  0 artifacts copied, 15 already retrieved (0kB/12ms)

build:
     [echo] HelloCamel: /home/gue/HelloCamel/build.xml

create_jar:
      [jar] Building jar: /home/gue/HelloCamel/jar/HelloCamel-0.1.1.jar

run:
     [java] 2016-07-18 22:20:10:772 +0200 [main] INFO DefaultCamelContext - Apache Camel 2.17.2 (CamelContext: camel-1) is starting
     [java] 2016-07-18 22:20:10:774 +0200 [main] INFO ManagedManagementStrategy - JMX is enabled
     [java] 2016-07-18 22:20:10:960 +0200 [main] INFO DefaultTypeConverter - Loaded 184 type converters
     [java] 2016-07-18 22:20:10:983 +0200 [main] INFO DefaultRuntimeEndpointRegistry - Runtime endpoint registry is in extended mode gathering usage statistics of all incoming and outgoing endpoints (cache limit: 1000)
     [java] 2016-07-18 22:20:11:083 +0200 [main] INFO DefaultCamelContext - AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
     [java] 2016-07-18 22:20:11:083 +0200 [main] INFO DefaultCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
     [java] 2016-07-18 22:20:11:134 +0200 [main] INFO DefaultCamelContext - Route: route1 started and consuming from: Endpoint[timer://myTimer?period=2000]
     [java] 2016-07-18 22:20:11:135 +0200 [main] INFO DefaultCamelContext - Total 1 routes, of which 1 are started.
     [java] 2016-07-18 22:20:11:137 +0200 [main] INFO DefaultCamelContext - Apache Camel 2.17.2 (CamelContext: camel-1) started in 0.363 seconds
     [java] Hello World Camel fired at Mon Jul 18 22:20:12 CEST 2016
     [java] Hello World Camel fired at Mon Jul 18 22:20:14 CEST 2016
     [java] Hello World Camel fired at Mon Jul 18 22:20:16 CEST 2016
     [java] Hello World Camel fired at Mon Jul 18 22:20:18 CEST 2016
     [java] Hello World Camel fired at Mon Jul 18 22:20:20 CEST 2016
     [java] 2016-07-18 22:20:21:138 +0200 [main] INFO DefaultCamelContext - Apache Camel 2.17.2 (CamelContext: camel-1) is shutting down
     [java] 2016-07-18 22:20:21:139 +0200 [main] INFO DefaultShutdownStrategy - Starting to graceful shutdown 1 routes (timeout 300 seconds)
     [java] 2016-07-18 22:20:21:146 +0200 [Camel (camel-1) thread #1 - ShutdownTask] INFO DefaultShutdownStrategy - Route: route1 shutdown complete, was consuming from: Endpoint[timer://myTimer?period=2000]
     [java] 2016-07-18 22:20:21:147 +0200 [main] INFO DefaultShutdownStrategy - Graceful shutdown of 1 routes completed in 0 seconds
     [java] 2016-07-18 22:20:21:155 +0200 [main] INFO DefaultCamelContext - Apache Camel 2.17.2 (CamelContext: camel-1) uptime 10.383 seconds
     [java] 2016-07-18 22:20:21:155 +0200 [main] INFO DefaultCamelContext - Apache Camel 2.17.2 (CamelContext: camel-1) is shutdown in 0.017 seconds

BUILD SUCCESSFUL
Total time: 19 seconds

Thats it !

Get Social


(c) 2017, by bytebang e.U. - Impressum - Datenschutz / Nutzungsbedingungen
-