by BehindJava

How to make dynamic messages with spring beans using apache camel in Spring Boot

Home » springboot » How to make dynamic messages with spring beans using apache camel in Spring Boot

In the previous tutorial, We created a basic route that logs a timestamp to the console using transform().constant. Now we are going to learn about making the messages dynamic with spring beans using apache camel in Spring Boot.

In the below code, using spring beans we can create dynamic messages and log them to see the output in the console and we are using @Autowired annotation to wire the beans created and annotated with the @Component annotation, Instead you can do the same thing by creating the beans in different classes also.

import java.time.LocalDateTime;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class MySecondRoute extends RouteBuilder {

	@Autowired
	private GetCurrentTimeBean getCurrentTimeBean;

	@Autowired
	private LogComponent logComponent;

	@Override
	public void configure() throws Exception {

		 from("timer:first-timer")
        .log("${body}")
        .transform().constant("Hello World Message")
        .log("${body}")
        .bean(getCurrentTimeBean)
		.log("${body}")
        .bean(logComponent)
        .to("log:first-timer");

	}
}

@Component
class GetCurrentTimeBean {

	public String getCurrentTime() {
		return "Current time is:" + LocalDateTime.now();
	}
}

@Component
class LogComponent {

	private Logger logger = LoggerFactory.getLogger(LogComponent.class);

	public void process(String message) {
		logger.info("LogComponent  {}", message);
	}
}

Output:

route1                                   : null
route1                                   : Hello World Message
route1                                   : Current time is:2022-02-05T17:59:01.743697600
c.example.apachemsa.routes.LogComponent  : LogComponent  Current time is:2022-02-05T17:59:01.743697600
first-timer                              : Exchange[ExchangePattern: InOnly, BodyType: String, Body: Current time is:2022-02-05T17:59:01.743697600]

Two types of operations that you can do within your specific route

  1. Processing
  2. Transformation

What is the difference between processing and transformation of a route?

Let’s say we received a message and we want to do some operation or something which does not make a change on the body of the message itself is called processing.

In the above example you can see a logging class component to process the body. If we are doing anything that changes the body of the message is called transformation.

        // : Exchange[ExchangePattern: InOnly, BodyType: null, Body: [Body is null]]
		from("timer:first-timer")
		.transform().constant("Hello World Message")
		.to("log:first-timer");

In this case the body type which is initially null is transformed to the constant message “Hello World Message”.

Output:

2022-01-28 11:30:12.724  INFO 6804 --- [r://first-timer] route1                                   : null
2022-01-28 11:30:12.724  INFO 6804 --- [r://first-timer] route1                                   : Hello World Message
2022-01-28 11:30:12.724  INFO 6804 --- [r://first-timer] first-timer                              : Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello World Message]

Previous                                                                                                               Next