Simple “Hello World” application using Apache Camel

I’ve learned this API few months back, and the initial days with this API was a real struggle. Now I’ve better grip on this API, and thought of sharing my experience here so that anyone comes across same situations as mine can make use of this.

This article will cover preparing a simple “Hello World” application using Apache Camel. I’ve used logging messages from camel routes in this example.

Software/API Used:

  1. JDK 1.8
  2. Eclipse Luna
  3. Maven
  4. Camel 2.18.2
  5. Activemq 5.6.0

First add the required maven dependencies to your application.

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-core</artifactId>
  <version>2.18.2</version>
</dependency>
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-stream</artifactId>
  <version>2.18.2</version>
</dependency>
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-jms</artifactId>
  <version>2.18.2</version>
</dependency>
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-camel</artifactId>
  <version>5.6.0</version>
</dependency>

Below is the code snippet for a standalone java application with Camel. This program will read message from activemq and print the message in console after 5 seconds delay.

package com.jyothis.camel.samples;

import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.CamelContext;
import org.apache.camel.LoggingLevel;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class CamelSampleApplication {
    public static void main( String[] args ) throws Exception {
    	CamelContext context = new DefaultCamelContext();
    try {
      context.addComponent("activemq", ActiveMQComponent.activeMQComponent("vm://localhost?broker.persistent=false"));
      context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
          from("activemq:queue:myQueue")
            .log(LoggingLevel.INFO, "Received message from queue:myQueue...")
            .delay(5000)
            .log(LoggingLevel.INFO, "Message received is :")
            .to("stream:out");
        }
      });
      ProducerTemplate template = context.createProducerTemplate();
      context.start();
      template.sendBody("activemq:myQueue", "Hello World");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      context.stop();
    }
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *