by BehindJava

How to create a route using Apache camel in Spring boot

Home » springboot » How to create a route using Apache camel in Spring boot

In this tutorial we are going to learn about creating a basic route with a timer that logs as the end points output.

Firstly, Go through this tutorial for Creating the Microservice applications using spring boot with Apache Camel integration framework.

Now create a new package under src/main/java with a package name of your choice and create a new class MyFirstRoute as shown in the below image.

pkexp

We are ready with all the initial setup and can proceed further and build our first camel route.

What is a Route?

A sequence of steps that involves in queuing of messages from a producer with a transformation or processing of the message if required and saving it to the database endpoint is called a Route.

We use a timer to schedule a log that runs at regular intervals of time.

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class MyFirstRoute extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		// from is the starting point of the route or producer
		// to is the end point
		// timer and log are the camel keywords
		from("timer:couter-timer").to("log:couter-timer");

	}
}

Output:

2022-02-05 12:37:54.838  INFO 7988 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-02-05 12:37:55.079  INFO 7988 --- [  restartedMain] o.a.c.impl.engine.AbstractCamelContext   : Routes startup (total:1 started:1)
2022-02-05 12:37:55.080  INFO 7988 --- [  restartedMain] o.a.c.impl.engine.AbstractCamelContext   :     Started route1 (timer://couter-timer)
2022-02-05 12:37:55.081  INFO 7988 --- [  restartedMain] o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.14.1 (camel-1) started in 232ms (build:37ms init:175ms start:20ms)
2022-02-05 12:37:55.088  INFO 7988 --- [  restartedMain] c.e.apachemsa.ApacheMsAApplication       : Started ApacheMsAApplication in 4.032 seconds (JVM running for 4.937)
2022-02-05 12:37:56.090  INFO 7988 --- [://couter-timer] couter-timer                             : Exchange[ExchangePattern: InOnly, BodyType: null, Body: [Body is null]]
2022-02-05 12:37:57.086  INFO 7988 --- [://couter-timer] couter-timer                             : Exchange[ExchangePattern: InOnly, BodyType: null, Body: [Body is null]]

In the above output, you can see the body of the route as Body: [Body is null]. Using tranform().constant(), We can have the content of the body as per our requirement. In the below code snippet we want to print the body of the route with the current timestamp.

import java.time.LocalDateTime;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class MyFirstRoute extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		// from is the starting point of the route or producer
		// to is the end point
		// timer and log are the camel keywords
		// transform() Adds a processor which sets the body on the OUT message
		from("timer:couter-timer").transform().constant("Current timestamp is:" + LocalDateTime.now())
				.to("log:couter-timer");
	}
}

Output:

couter-timer                             : Exchange[ExchangePattern: InOnly, BodyType: String, Body: Current time is:2022-02-05T12:55:18.906537400]

Previous                                                                                                               Next