In this tutorial we will learn how to create a Red Hat Fuse application with Spring Boot which sends and consumes messages to Apache Artemis MQ.
A Red Hat Fuse application can be created using any of these technology stacks:
- Apache Karaf
- Spring Boot
- Fuse on EAP
Install and configure Apache Artemis MQ
We recommend reading this tutorial in order to get started with Apache Artemis MQ: http://www.mastertheboss.com/other/apache-activemq/getting-started-with-activemq-artemis
Creating the Red Hat Fuse project
In a shell prompt, enter the following the mvn command to create a Spring Boot project.
mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate \ -DarchetypeCatalog=https://maven.repository.redhat.com/ga/io/fabric8/archetypes/archetypes-catalog/2.2.0.fuse-760024-redhat-00001/archetypes-catalog-2.2.0.fuse-760024-redhat-00001-archetype-catalog.xml \ -DarchetypeGroupId=org.jboss.fuse.fis.archetypes \ -DarchetypeArtifactId=spring-boot-camel-xml-archetype \ -DarchetypeVersion=2.2.0.fuse-760024-redhat-00001
Our project will include a Spring Boot main class:
package com.sample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
The ArtemisMQ Connection factory configuration will be included in a @Configuration class which assumes that the AMQ server is running on tcp://localhost:61616 and is configured with the credentials “admin”/”admin”:
package com.sample; import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; import org.apache.camel.component.jms.JmsComponent; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public JmsComponent jmsComponent() throws Exception { // Create the connectionfactory which will be used to connect to Artemis ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(); cf.setBrokerURL("tcp://localhost:61616"); cf.setUser("admin"); cf.setPassword("admin"); // Create the Camel JMS component and wire it to our Artemis connectionfactory JmsComponent jms = new JmsComponent(); jms.setConnectionFactory(cf); return jms; } }
Finally, the Route Builder class which produces messages from a Timer component. The messages are transformed and send to the Queue named “QueueIN”. When messages are available on this Queue, they will be routed to the Queue “QueueOUT”:
package com.sample; import org.apache.camel.builder.RouteBuilder; import org.springframework.stereotype.Component; @Component public class CamelArtemisRouteBuilder extends RouteBuilder { public void configure() throws Exception { from("timer:mytimer?period=5000") .routeId("generate-route") .transform(constant("HELLO from Camel!")) .to("jms:queue:QueueIN"); from("jms:queue:QueueIN") .routeId("receive-route") .log("Received a message - ${body} - sending to outbound queue") .to("jms:queue:QueueOUT?exchangePattern=InOnly"); } }
You don’t need to create this destination on AMQ as they will be automatically created as you have an admin user.
That’s basically all.You can run the application in the standard way for Spring Boot:
$ mvn install spring-boot:run
You will see from the Console that, upon the application has started, the messages will display on the Console:
13:20:32.952 [Camel (camel-1) thread #1 - JmsConsumer[INCOMING]] INFO receive-route - Received a message - HELLO from Camel! - sending to outbound queue 13:20:37.935 [Camel (camel-1) thread #1 - JmsConsumer[INCOMING]] INFO receive-route - Received a message - HELLO from Camel! - sending to outbound queue 13:20:42.954 [Camel (camel-1) thread #1 - JmsConsumer[INCOMING]] INFO receive-route - Received a message - HELLO from Camel! - sending to outbound queue 13:20:47.966 [Camel (camel-1) thread #1 - JmsConsumer[INCOMING]] INFO receive-route - Received a message - HELLO from Camel! - sending to outbound queue
Also, if you run the command artemis address show, you will see that the destinations have been created when the example has started:
$ artemis address show Connection brokerURL = tcp://localhost:61616 activemq.management.1b4d0d91-9dcc-4f22-a1da-a8feb6911e11 activemq.notifications DLQ ExpiryQueue QueueIN QueueOUT
You can find the full source code for this Red Hat Fuse example here: https://github.com/fmarchioni/masterspringboot/tree/master/fuse/fuse-amq-artemis
Found the article helpful? if so please follow us on Socials