How to create a Java JMS Client for Artemis

This article will teach you how to write an example Java JMS Client for an Artemis MQ server. We will cover both the configuration of the address in Artemis and a simple Java Client.

When working with JMS, destinations such as queues and topics are typically located using JNDI (Java Naming and Directory Interface). The JNDI context environment can be configured with specific properties to locate these destinations. The property name should be in the format “queue.<jndi-binding>” or “topic.<jndi-binding>“, where <jndi-binding> is the binding of the destination in the JNDI tree.

The value of the property should be the name of the queue or topic hosted by the Apache ActiveMQ Artemis server.

For example, consider the following address Queue in broker.xml:

<address name="exampleQueue">
            <anycast>
               <queue name="exampleQueue" />
            </anycast>
</address>

In order to connect to the above Queue from a Java Client, we will set up a jndi.properties file:

java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
connectionFactory.ConnectionFactory=tcp://localhost:61616
queue.queue/exampleQueue=exampleQueue

You need to place the above file in the application’s classpath. For example, in a Maven project, into the resources folder.

Coding the JMS Java Client

Next, we will code a sample Java Client that uses the above binding to send a message to the exampleQueue:

public class QueueExample {

   public static void main(final String[] args) throws Exception {
      Connection connection = null;
      InitialContext initialContext = null;
      try {
         // Step 1. Create an initial context to perform the JNDI lookup.
         initialContext = new InitialContext();

         // Step 2. Perform a lookup on the queue
         Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");

         // Step 3. Perform a lookup on the Connection Factory
         ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");

         // Step 4.Create a JMS Connection
         connection = cf.createConnection();

         // Step 5. Create a JMS Session
         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

         // Step 6. Create a JMS Message Producer
         MessageProducer producer = session.createProducer(queue);

         // Step 7. Create a Text Message
         TextMessage message = session.createTextMessage("This is a text message");

         System.out.println("Sent message: " + message.getText());

         // Step 8. Send the Message
         producer.send(message);

         // Step 9. Create a JMS Message Consumer
         MessageConsumer messageConsumer = session.createConsumer(queue);

         // Step 10. Start the Connection
         connection.start();

         // Step 11. Receive the message
         TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);

         System.out.println("Received message: " + messageReceived.getText());
      } finally {
         // Step 12. Be sure to close our JMS resources!
         if (initialContext != null) {
            initialContext.close();
         }
         if (connection != null) {
            connection.close();
         }
      }
   }
}

To be able to build the above Java Client you need the following dependency in your Maven project:

   <dependencies>
      <dependency>
         <groupId>org.apache.activemq</groupId>
         <artifactId>artemis-jms-client-all</artifactId>
         <version>${project.version}</version>
      </dependency>
   </dependencies>

Conclusion

This article was a quick introduction to writing a Java JMS Client for a remote Artemis MQ Server. You can find the source code in the Artemis distribution, under the directory ARTEMIS_HOME/examples/features/standard/queue