CodingFrog
CodingFrog

Reputation: 1655

How to install Apache Camel?

How do you install Apache-Camel?

I've just started reading 'Camel in Action' 2nd ed. In section 1.2.1, is suggests downloading the binary distribution. I found this link to from the releases page: Release 2.24.1 But I can only see a source-code download. I've tried to compile from the source, but that always encounters an error. How do you just install the binary on either Ubuntu/Redhat/Fedora? Or have I misunderstood Apache-Camel as being a library that you can just install? Must it always be compiled with maven?

Upvotes: 0

Views: 2509

Answers (2)

Pasi Österman
Pasi Österman

Reputation: 2187

Camel, Java-projects and build-tools

Apache Camel works like any other Java library or framework, meaning that in order to use the framework you'll have to include its java binaries (i.e .jar files) to your project as dependencies. Now to make things simpler most developers use either Maven or Gradle to create, manage and build their java projects.

From these two I would recommend Maven as it seems to be the preferred option for Camel developers with most examples (including ones in official camel site) using it. To install maven follow the official how to install maven guide.

Maven archetypes

To create example camel project you can use maven archetypes which are basically project templates that can be used to create various types of new projects. If you're reading Camel in Action you might be better off using Camel version 2.x.x in your projects with Java Development Kit version 8. Camel 3.x.x is pretty similar so it should be fairly easy to learn that after learning the basics with 2.x.x.

After installing maven you can use your Java IDE (i.e IntelliJ, VSCode, Eclipse or Netbeans) to create project from maven archetype with groupId: org.apache.camel.archetypes artifactId: camel-archetype-java and version: 2.25.4

Or use maven command line command:

# Create new camel java project for Camel version 2.25.4
mvn archetype:generate -DarchetypeGroupId="org.apache.camel.archetypes" -DarchetypeArtifactId=camel-archetype-java -DarchetypeVersion="2.25.4"

Camel project

The new project should contain project file pom.xml where you can specify all the dependencies for your project. The the camel-archetype-java should have the following dependencies listed in the dependencies section of pom.xml.

<dependencies>

    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
    </dependency>

    <!-- logging -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-slf4j-impl</artifactId>
      <scope>runtime</scope>
    </dependency>

    <!-- testing -->
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

The example route in camel-archetype-java archetypes Myroutebuilder.java is pretty complex for beginners. Hello world on a timer is generally a more simpler test on to see if things work.

package com.example;

import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder extends RouteBuilder {

    public void configure() {

        // from("file:src/data?noop=true")
        //     .choice()
        //         .when(xpath("/person/city = 'London'"))
        //             .log("UK message")
        //             .to("file:target/messages/uk")
        //         .otherwise()
        //             .log("Other message")
        //             .to("file:target/messages/others");

        from("timer:timerName?period=3000")
            .routeId("helloWorldTimer")
            .log(LoggingLevel.INFO, "Hello world");
    }
}

The project generated from archetype comes with exec-maven-plugin which allows you to run the project with mvn exec:java

Java development kit - JDK

If you're using JDK 11 instead of JDK 8 you'll have to modify maven-compiler-plugin configuration a bit.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <!-- 
        <source>1.8</source>
        <target>1.8</target> 
        -->
        <source>11</source>
        <target>11</target>
    </configuration>
</plugin>

If you've multiple versions of JDK installed you'll have to configure your IDE to use the correct one for the project. With IntelliJ you can configure JDK used by the project from project structure settings.

With VSCode you'll need both JDK 11 and JDK 8 as VSCode Java extensions require JDK 11 to run.

Example settings.json entries for OpenJDK:

"java.configuration.runtimes": [
    {
        "name": "JavaSE-11",
        "path": "C:\\Program Files\\AdoptOpenJDK\\jdk-11.x.x.xxx-hotspot",
        "default": true
    },
    {
        "name": "JavaSE-1.8",
        "path": "C:\\Program Files\\RedHat\\java-1.8.0-openjdk-1.8.0.xxx-x",
    }
],
"java.home": "C:\\Program Files\\AdoptOpenJDK\\jdk-11.x.x.xxx-hotspot"

You might also want to setup your path variable so that java -version returns correct version from the command-line.

Upvotes: 1

Naresh Chaurasia
Naresh Chaurasia

Reputation: 469

What you really need is download the binaries for Apache Camel. The best way to get them is make use of maven (https://maven.apache.org/install.html), and existing project from GitHub and get started.

You can go to the following link: https://github.com/dilipsundarraj1/TeachApacheCamel. You can either download the project as zip file or clone the project (you need to have git installed on your machine).

After you downloaded / cloned the project use go to one of the projects: learncamel-simple-file and open the folder in command prompt.

In the command prompt run the command mvn dependency:resolve. (I am assuming you have maven and java installed on your machine). This command will download all the required binaries in the folder: c:\user\<userid>\.m2\repository, where userid is specific to you machine.

Hope it helps.

Upvotes: 1

Related Questions