How to code a remote JMS client for WildFly

In this tutorial we will learn how to create a remote JMS connection to a Queue which has been deployed on WildFly application server. 

In order to create a remote JMS Connection to an HornetQ server, we need to perform a set of steps. At first we need to deploy the Queue to the JMS server, next we need to create an application user with a Role that is enabled to send JMS messages. Last step, we need obviously create a JMS consumer and a JMS deployer.
Let’s start from the first step, we will deploy a Queue named demoQueue which is bound into the java:/jms/queue/demoQueue JNDI binding. From the CLI execute:

jms-queue add --queue-address= jms.queue.ExampleQueue --entries=java:/jms/queue/exampleQueue,java:/jboss/exported/jms/queue/exampleQueue

Please note that we had to include as well the JNDI binding “java:/jboss/exported/jms/queue/demoQueue” as remote JNDI bindings are only available if registered under the “java:/jboss/exported” namespace

Next, launch the to create an application user that belongs to the “guest” Role.

jms tutorial wildlfy application server

You can choose any other kind of role, as long as it’s compliant with the JMS security settings:

    <security-setting match="#">
        <permission type="send" roles="guest"/>
        <permission type="consume" roles="guest"/>
        <permission type="createNonDurableQueue" roles="guest"/>
        <permission type="deleteNonDurableQueue" roles="guest"/>

And now the code for the TestJMSClient:

package com.itbuzzpress.chapter12.test;

import static org.junit.Assert.assertEquals;

import java.util.Properties;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.junit.Test;

public class TestJMSClient {

	private String MESSAGE = "Hello, World!";
	private String CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
	private String DESTINATION = "jms/queue/exampleQueue";

	public void testSendReceive() throws Exception {

		Context namingContext = null;
		JMSContext context = null;

		try {

			// Set up the namingContext for the JNDI lookup
			final Properties env = new Properties();
			env.put(Context.PROVIDER_URL, "http-remoting://");
			env.put(Context.SECURITY_PRINCIPAL, "jmsuser");
			env.put(Context.SECURITY_CREDENTIALS, "Password1!");
			namingContext = new InitialContext(env);

			ConnectionFactory connectionFactory = (ConnectionFactory) namingContext
			System.out.println("Got ConnectionFactory " + CONNECTION_FACTORY);

			Destination destination = (Destination) namingContext
			System.out.println("Got JMS Endpoint " + DESTINATION);

			// Create the JMS context
			context = connectionFactory.createContext("jmsuser", "Password1!");

			context.createProducer().send(destination, MESSAGE);
			System.out.println("Sent message " + MESSAGE);

			// Create the JMS consumer
			JMSConsumer consumer = context.createConsumer(destination);
			// Then receive the same number of messages that were sent

			String text = consumer.receiveBody(String.class, 5000);
			if (text == null) System.out.println("No message Received! Maybe another Consumer listening on the Queue ??");
			System.out.println("Received message with content " + text);

		} catch (Exception e) {
			throw e;
		} finally {
			if (namingContext != null) {

			// closing the context takes care of consumer too
			if (context != null) {

As you can see, this code does not use anymore the AS 7 / EAP 6 Remoting port (port 4447) from the client side to connect to the server but the connection lands on port 8080 where it’s (behind the hoods) plumbed into HornetQ by the Undertow Web server.

private static final String PROVIDER_URL = "http-remoting://localhost:8080";

In order to compile and run the Test class, it’s recommended to use Maven as you will be able to import the JMS client BOM so that you can easily switch between different versions of WildFly by changing the Property version.wildfly :

<project xmlns="" xmlns:xsi=""

	<name>JMS Remote client example</name>
	<description>JMS Remote client example</description>

			<name>Apache License, Version 2.0</name>




		<!-- other plugin versions -->

		<!-- maven-compiler-plugin -->

			<id>JBoss Repository</id>


			<!-- WildFly plugin to deploy war -->

			<!-- Compiler plugin enforces Java 1.6 compatibility and activates annotation 
				processors -->

That’s all! You can run the code with:

$ mvn clean install test

As a result a message will be sent to the remote Queue and dequeued from the JMSConsumer:

Got ConnectionFactory jms/RemoteConnectionFactory
Got JMS Endpoint jms/queue/exampleQueue
Sent message Hello, World!
Received message with content Hello, World!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.137 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0