Apache Camel with Spring Boot (Bonus: ActiveMQ)

Thiago E. Cabral
3 min readSep 7, 2020

Hi guys!

How we learned on previousily article, Apache Camel is a powerfull tool that help us to integrates systems. Even if standalone way, there are already many resources, but we can go further by combining it with Spring Boot.

With this combination we can use the Apache Camel’s power inside the Spring Boot’s ecosystem and build even more powerful integration applications.

As we can expect, when it comes to Spring Boot, the configuration is pretty simple, as we’ll see below.

Prerequisites

We just need a new Spring Boot application already on our IDE.

Set the starter

Like almost everything in Spring Boot, let’s add the Apache Camel starter as dependency on our pom.xml file:

<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>

And, as our route will get some files from a path and put in a queue (ActiveMQ), we need add the ActiveMQ starter as dependency too:

<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-activemq-starter</artifactId>
<version>3.4.3</version>
</dependency>

Config the application.yml (properties)

To keep the main thread blocked so that Camel stays up, and run the route, we need add this parameter to the application file:

camel:
springboot:
main-run-controller: true

Set the main class

Inside our main class, let’s to inject the CamelContext and add activemq component to this context using the method init():

...@Autowired
private CamelContext context;
@PostConstruct
public void init() throws Exception {
context.addComponent("activemq", ActiveMQComponent.activeMQComponent("tcp://localhost:61616"));
}
...

Create our route

Here, we’ll create our RouteBuilder inside a package called route:

@Component
public class OrderRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("file:orders?noops=true").
to("activemq:queue:orders");
}
}

Note: The parameter “noops=true” is used to keep the files on the source after the processing, in our learning context allow that we continues running the application, for tests, reusing these same files.

Note 2: Put any XML file into the folder “orders”, just for run our application. There is a good one here.

ActiveMQ

How we know, our code will send a message to a Active MQ Queue, and, to achieve this, we need to set it, let’s go:

1) On this link, there are 3 options, one for each type of enviromment: Windows/Linux/Mac, and for each one there are 3 another options, just click on the .zip/.tar regarding your enviroment and go ahead.

2) After downloading, extract the content;

3) Open the Terminal (or PowerShell in PC), and navigate to extracted folder.

4) To start our broker, we need run this command, inside that:

bin/activemq console 

5) Now, open the link: http://localhost:8161/admin/

6) It will ask you to type the basic credentials, is simple:

Username: “admin”
Password: “admin”

7) Now, on menubar click in “Queues”;

8) Then, on “Queue Name” type “orders” and click on “Create”;

9) Immediately, we’ll see the queue listed below;

10) That’s it, we are ready to run our application!

Note: The address tcp://localhost:61616 that we put in our code is the default to send a message to broker.

Run as

After that, we need just run as Spring Boot App and we’ll see the magic.

Now, reload (F5) ActiveMQ’s admin page, and see that there is one message on column Number Of Pending Messages of our Queue orders.

If you put another file in the folder “orders”, the Apache Camel will get this one and send to queue immediately. That’s happens because the component “file” is making continuous polling on source.

Conclusion

As we have seen, Apache Camel helps us a lot, and we prove that it is easier to use its DSL (API Fluent) than to implement it using pure Java.

The source code is on Github: https://github.com/thiagoericson/camel-project.

If you have any question, please, let me know!

Regards!

--

--