This tutorial discusses how to send messages using the Camel JMS Component to a remote ArtemisMQ embedded in a WildFly distribution. This example will use a Camel standalone application. If you want to use a Spring Boot application, then check this article: JMS Messaging with Spring Boot and Artemis MQ
Setting up the Camel Route
Firstly, set up a basic Camel Project. Within this project, we will show how to send JMS Messages to a remote Artemis Broker that is running in WildFly.
package com.sample.jms; import javax.jms.ConnectionFactory; import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; import org.apache.camel.CamelContext; import org.apache.camel.ProducerTemplate; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.jms.JmsComponent; public class JMSDemo { public static void main(String args[]) throws Exception { // Create a JMS connection factory ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // Configure the JMS component in Camel to use the connection factory JmsComponent jmsComponent = JmsComponent.jmsComponent(connectionFactory); jmsComponent.setUsername("guest"); jmsComponent.setPassword("guest"); CamelContext context = new DefaultCamelContext(); context.addComponent("jms", jmsComponent); context.addRoutes(new RouteBuilder() { public void configure() { from("direct:start").to("jms:queue:demoQueue"); from("jms:queue:demoQueue").log("Received JMS Message: ${body}"); } }); context.start(); // start the route ProducerTemplate producer = context.createProducerTemplate(); // Send a message to the start endpoint producer.sendBody("direct:start", "This is an important message"); Thread.sleep(3000L); // let the route run for 30 seconds context.stop(); // stop the route } }
This Java code sets up a simple Apache Camel route that sends and receives messages from a JMS (Java Message Service) broker using ActiveMQ as the JMS provider.
The JMSDemo
class uses the ActiveMQConnectionFactory
class to create a connection factory that connects to the ActiveMQ broker on tcp://localhost:61616
. It then configures a JmsComponent
in Camel to use the connection factory, sets the username and password, and adds it to the Camel context.
Next, it creates a Camel route using the RouteBuilder
class that defines two endpoints: one that sends a message to the JMS queue named demoQueue
and another that logs the received message from the demoQueue
. The message sent to the queue is a simple string: “This is an important message”.
After configuring the route, it starts the Camel context, creates a ProducerTemplate
, and sends a message to the direct:start
endpoint. It then sleeps the main thread for 3 seconds to allow the Camel route to run and receive the message. Finally, it stops the Camel context to terminate the route.
To compile the above route, besides the Camel core dependencies, you will need the following JMS Dependencies for the jms component and for connecting to Artemis:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jms</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>artemis-jms-client</artifactId> <version>2.28.0</version> </dependency>
Configuring Artemis in WildFly
Next step, will be configuring WildFly.
Firstly, connect to WildFly CLI
$ ./jboss-cli.sh
Next, create a JMS remote acceptor which is bound to port 61616:
/socket-binding-group=standard-sockets/socket-binding=artemis-server:add(port=61616) /subsystem=messaging-activemq/server=default/remote-acceptor=artemis-acceptor:add(socket-binding=artemis-server)
Next, let’s create as well the JMS Queue named demoQueue from the CLI:
jms-queue add --queue-address=demoQueue --entries=queues/demoQueue
Finally, exit from the CLI. We need to create an user which is allowed to connect to the JMS server. Within our Camel configuration we have already configured it to be guest/guest therefore we will add this user with the shell script add-user.sh:
./add-user.sh -a -u guest -p guest -g guest
Running the application
As final step, run the Camel Main class and verify that the message you are sending is printed on the Console:
[ main] AbstractCamelContext INFO Apache Camel 3.7.1 (camel-1) is starting [ main] AbstractCamelContext INFO StreamCaching is not in use. If using streams then it's recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html [ main] InternalRouteStartupManager INFO Route: route1 started and consuming from: direct://start [ main] InternalRouteStartupManager INFO Route: route2 started and consuming from: jms://queue:demoQueue [ main] AbstractCamelContext INFO Total 2 routes, of which 2 are started [ main] AbstractCamelContext INFO Apache Camel 3.7.1 (camel-1) started in 238ms [ad #1 - JmsConsumer[demoQueue]] route2 INFO Received JMS Message: This is an important message [ main] AbstractCamelContext INFO Apache Camel 3.7.1 (camel-1) is shutting down [ main] DefaultShutdownStrategy INFO Starting to graceful shutdown 2 routes (timeout 45 seconds) [el-1) thread #2 - ShutdownTask] DefaultShutdownStrategy INFO Route: route2 shutdown complete, was consuming from: jms://queue:demoQueue [el-1) thread #2 - ShutdownTask] DefaultShutdownStrategy INFO Route: route1 shutdown complete, was consuming from: direct://start [ main] DefaultShutdownStrategy INFO Graceful shutdown of 2 routes completed in 31ms [ main] AbstractCamelContext INFO Apache Camel 3.7.1 (camel-1) uptime 3s376ms [ main] AbstractCamelContext INFO Apache Camel 3.7.1 (camel-1) is shutdown in 46msFound the article helpful? if so please follow us on Socials