Apache Camel ActiveMQ with Spring Boot Message Example
Overview
In this article, we'll explore how to integrate Apache Camel ActiveMQ with Spring Boot.
Apache Camel Tutorial :
- Apache Camel Overview and Architecture
- Install ActiveMQ And Start ActiveMQ Server on Windows
- Apache Camel with Spring Boot Simple Example
- Apache Camel Exception Handling Example
- Apache Camel ActiveMQ with Spring Boot Message Example
- Spring Boot + Apache Camel Quartz Example
- Apache Camel Interview Questions and Answers
What is a Apache Camel?
Apache Camel is an open-source integration framework for implementing various Enterprise Integration Patterns(EIPs), that allows you to quickly and easily connect different systems that consume or produce data.
It is easy to use Domain Specific Languages (DSLs) to wire EIPs and transports together
Camel receives messages from some endpoint and sends it to another one. The messages can be processed or simply routed to different endpoint depending on transformation logic applied in Camel.
It allows integration simple by providing connectivity to a wide variety of transports and APIs.
For example, you can easily route JMS to JSON, JSON to JMS, HTTP to JMS, FTP to JMS, even HTTP
to HTTP, and connectivity to Microservices.
COMPONENTS
Component references are used to locate a component within an assembly. Apache Component References provides a variety of references that provide services for messaging, transmitting data, notifications, and a variety of other services that can not only resolve simple messaging and data transfer but also provide data security.
CORE COMPONENTS
Total Core Components: 25 in 22 JAR artifacts. Some of them are Bean, log,Class, REST API, Timer, XSLT etc..NON-CORE COMPONENTS
There are total Non-Core Components: 320 in 247 JAR artifacts (1 deprecated). Some of them are activemq, amqp, atom, aws, aws lamda, kafka, aws mq, crypto etc..
Take a look at our suggested posts:
In this tutorial, will start with basic example of using simple constant message using core component called timer and print the log using Log core component and will send the same message to ActiveMQ queue.
It will generate messages in specified intervals using java.util.Timer.
Syntax for timer component:
timer:timerName
For log, Apache Camel utilizes sfl4j, which enables you to configure logging through, among Log4j,
Logback, Java Util Logging
Syntax for log component:
log:loggerName
Let's get started on the project now. In this tutorial will create two Spring Boot + Apache Camel ActiveMQ Message Producer Service boot Producer Service and Spring Boot + Apache Camel ActiveMQ Message Consumer Service
Spring Boot + Apache Camel ActiveMQ Message Producer Service
Project Structure
- Create Spring Boot Maven project with Apache Camel dependencies from spring initializr
- Apache Camel will have camel-spring-boot-starter dependency and
camel-activemq-starter for activemq dependency in pom.xml.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.techgeeknext</groupId> <artifactId>camel-sprint-boot-service-a</artifactId> <version>0.0.1-SNAPSHOT</version> <name>camel-sprint-boot-service-a</name> <description>Demo project for Spring Boot with Apache Camel</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-spring-boot-starter</artifactId> <version>3.9.0</version> </dependency> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-activemq-starter</artifactId> <version>3.9.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Active MQ configuration
Configuring the tcp activemq broker url in application.properties to make activemq connection.
##active mq broker connection
spring.activemq.broker-url=tcp://localhost:61616
Building a Route
A route in Apache Camel is a collection of steps that Camel performs in order to consume and process a message. A Camel route begins with a customer and progresses through a series of endpoints and processors.
Routes in Java are found within a RouteBuilder class, which has a configure() method where you can add your route code.
Create ActiveMQMessageProducerRoute class which extends RouteBuilder and override the configure method.package com.techgeeknext.camel.routes;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class ActiveMQMessageProducerRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
//generates an event/constant message every 60 seconds
from("timer:active-mq-timer?period=60000")
.transform().constant("Hello Message from Apache Camel - TechGeekNext")
.log("${body}")
//send this message to ActiveMQ queue
.to("activemq:my-activemq-queue");
}
}
from() Route With transform()
When working with Camel, a route receives parameters and then converts, transforms and process these parameters. After that, it sends these parameters to another route that forwards the result to the desired output (a file, a database, an SMTP server or a REST API response).
.to("activemq:my-activemq-queue")
to allows to send a message to a dynamic Endpoints. In our example, we are using ActiveMQ component to send the message to my-activemq-queue.Start the Spring Boot Application, and you can notice that route is generating Hello constant message in every 60 seconds and logs in the console as shown below.
[INFO] --- spring-boot-maven-plugin:2.4.5:run (default-cli) @ camel-sprint-boot-service-a ---
[INFO] Attaching agents: []
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.5)
10:11:52.748 INFO 9076 --- [ main] c.t.c.CamelSprintBootServiceAApplication : Starting CamelSprintBootServiceAApplication using Java 12.0.2 (D:\camel-sprint-boot-service-a\target\classes started by TechGeekNext User in D:\camel-sprint-boot-service-a)
10:11:52.750 INFO 9076 --- [ main] c.t.c.CamelSprintBootServiceAApplication : No active profile set, falling back to default profiles: default
10:11:53.894 INFO 9076 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
10:11:53.901 INFO 9076 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
10:11:53.901 INFO 9076 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.45]
10:11:54.075 INFO 9076 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
10:11:54.075 INFO 9076 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1284 ms
10:11:54.247 INFO 9076 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
10:11:54.632 INFO 9076 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
10:11:54.771 INFO 9076 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Routes startup summary (total:1 started:1)
10:11:54.772 INFO 9076 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Started route1 (timer://active-mq-timer)
10:11:54.772 INFO 9076 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Apache Camel 3.9.0 (camel-1) started in 142ms (build:25ms init:104ms start:13ms)
10:11:54.776 INFO 9076 --- [ main] c.t.c.CamelSprintBootServiceAApplication : Started CamelSprintBootServiceAApplication in 2.351 seconds (JVM running for 2.818)
10:11:55.784 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:12:55.789 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:13:55.791 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:14:55.801 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:15:55.808 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:16:55.816 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:17:55.823 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:18:55.826 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:19:55.828 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:20:55.841 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:21:55.848 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:22:55.870 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:23:55.883 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:24:55.891 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:25:55.905 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
Spring Boot + Apache Camel ActiveMQ Message Consumer Service
Project Structure
- Create Spring Boot Maven project with Apache Camel dependencies from spring initializr
- Apache Camel will have camel-spring-boot-starter dependency and
camel-activemq-starter for activemq dependency in pom.xml.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.techgeeknext</groupId> <artifactId>camel-sprint-boot-service-b</artifactId> <version>0.0.1-SNAPSHOT</version> <name>camel-sprint-boot-service-b</name> <description>Demo project for Spring Boot with Apache Camel</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-spring-boot-starter</artifactId> <version>3.9.0</version> </dependency> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-jackson-starter</artifactId> <version>3.9.0</version> </dependency> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-kafka-starter</artifactId> <version>3.9.0</version> </dependency> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-activemq-starter</artifactId> <version>3.9.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Active MQ configuration
Configuring the tcp activemq broker url in application.properties to make activemq connection.
##active mq broker connection
spring.activemq.broker-url=tcp://localhost:61616
Building a Route
A route in Apache Camel is a collection of steps that Camel performs in order to consume and process a message. A Camel route begins with a customer and progresses through a series of endpoints and processors.
Routes in Java are found within a RouteBuilder class, which has a configure() method where you can add your route code.
Create ActiveMQMsgConsumerRoute class which extends RouteBuilder and override the configure method.package com.techgeeknext.routes;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class ActiveMQMsgConsumerRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("activemq:my-activemq-queue")
.to("log:received-message-from-active-mq");
}
}
from() Route
When working with Camel, a route receives message from the activemq.Start the Consumer Spring Boot Application , and you can notice in console that consumer route is receiving Hello message from Producer Spring Boot Application.
[INFO] --- spring-boot-maven-plugin:2.4.5:run (default-cli) @ camel-sprint-boot-service-a ---
[INFO] Attaching agents: []
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.5)
10:11:52.748 INFO 9076 --- [ main] c.t.c.CamelSprintBootServiceAApplication : Starting CamelSprintBootServiceAApplication using Java 12.0.2 (D:\camel-sprint-boot-service-a\target\classes started by TechGeekNext User in D:\camel-sprint-boot-service-a)
10:11:52.750 INFO 9076 --- [ main] c.t.c.CamelSprintBootServiceAApplication : No active profile set, falling back to default profiles: default
10:11:53.894 INFO 9076 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
10:11:53.901 INFO 9076 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
10:11:53.901 INFO 9076 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.45]
10:11:54.075 INFO 9076 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
10:11:54.075 INFO 9076 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1284 ms
10:11:54.247 INFO 9076 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
10:11:54.632 INFO 9076 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
10:11:54.771 INFO 9076 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Routes startup summary (total:1 started:1)
10:11:54.772 INFO 9076 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Started route1 (timer://active-mq-timer)
10:11:54.772 INFO 9076 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Apache Camel 3.9.0 (camel-1) started in 142ms (build:25ms init:104ms start:13ms)
10:11:54.776 INFO 9076 --- [ main] c.t.c.CamelSprintBootServiceAApplication : Started CamelSprintBootServiceAApplication in 2.351 seconds (JVM running for 2.818)
10:11:55.784 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:12:55.789 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:13:55.791 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:14:55.801 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:15:55.808 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:16:55.816 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:17:55.823 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:18:55.826 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:19:55.828 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:20:55.841 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:21:55.848 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:22:55.870 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:23:55.883 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:24:55.891 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
10:25:55.905 INFO 9076 --- [active-mq-timer] route1 : Hello Message from Apache Camel - TechGeekNext
Monitoring in ActiveMQ UI
Go to Install ActiveMQ and Start ActiveMQ Server by following the steps.
After running the above applications, go to http://localhost:8161/admin/queues.jsp, you can see one queue automatically created with name my-activemq-queue, which we had configured in our Router class. You can refresh the page to see the message count.
Download Source Code
The full source code for this article can be found on below.Download it here - Apache Camel ActiveMQ + Spring Boot Message Producer Example Apache Camel ActiveMQ + Spring Boot Message Consumer Example