Introduction to ActiveMQ Artemis

ActiveMQ Artemis is a message broker. It originates from HornetQ messaging system which was donated to Apache in 2014. It retains compatibility with HornetQ while adding many interesting features. In this tutorial we will learn how to install it and start it.

Please don't confuse ActiveMQ Artemis with the ActiveMQ broker (http://activemq.apache.org/) which is the main messaging broker developed by Apache foundation, although it is possible that in the future ActiveMQ Artemis will replace the former ActiveMQ project. But at the time of writing this is not confirmed.

You can download ActiveMQ Artemis from here: https://activemq.apache.org/artemis/download.html  

Once unzipped the project move to the bin folder:

cd ~/apache-artemis-1.0.0/bin

The first step will be creating a new broker instance, choosing the "create" option followed by the storage path:

jboss@jbosseap ~/apache-artemis-1.0.0/bin $ ./artemis create /home/jboss/data

 Enter an username and password for the administration:

Creating ActiveMQ Artemis instance at: /home/jboss/data
Java HotSpot(TM) Client VM warning: You have loaded library /home/jboss/apache-artemis-1.0.0/bin/lib/linux-x86_64/libartemis-native-64.so which might have disabled stack guard. The VM will try to fix the stack guard now.
It's highly recommended that you fix the library with 'execstack -c <libfile>', or link it with '-z noexecstack'.

--user: is mandatory with this configuration:
Please provide the default username:
admin

--password: is mandatory with this configuration:
Please provide the default password:


--allow-anonymous: is mandatory with this configuration:
Allow anonymous access? (Y/N):
Y

You can now start the broker by executing:  

   "/home/jboss/data/bin/artemis" run

Or you can setup the broker as system service and run it in the background:

/home/jboss/data/bin/artemis-service
   /etc/init.d/artemis-service start

Now let's start the broker:

 /home/jboss/data/bin/artemis run   

Now you can run a test example on it. Start ActiveMQ Artemis first:

/home/jboss/data/bin/artemis run

artemis activemq tutorial

 Here is a simple JMS Client called ArtemisQueueDemo:

package org.apache.activemq.artemis.jms.example;

import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

 
 
public class ArtemisQueueDemo  
{
   public static void main(final String[] args) throws Exception
   {
      new ArtemisQueueDemo().runExample();
   }

 
   public boolean runExample() throws Exception
   {
      Connection connection = null;
      InitialContext initialContext = null;
      try
      {
         // Step 1. Create an initial context to perform the JNDI lookup.
         Properties p = new Properties();
         p.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
         p.put("connectionFactory.ConnectionFactory", "tcp://localhost:61616");
         p.put("queue.queue/exampleQueue", "exampleQueue");
          
         
         initialContext = new InitialContext(p);
          
       
         // lookup on the queue
         Queue queue = (Queue)initialContext.lookup("queue/exampleQueue");

         // lookup on the Connection Factory
         ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("ConnectionFactory");

         // Create an authenticated JMS Connection
         connection = cf.createConnection("admin","admin");

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

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

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

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

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

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

         // Start the Connection
         connection.start();

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

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

         return true;
      }
      finally
      {
         // Be sure to close our JMS resources!
         if (initialContext != null)
         {
            initialContext.close();
         }
         if (connection != null)
         {
            connection.close();
         }
      }
   }
}

In order to run the above example, you can either add the Maven dependencies as indicated in the examples (examples\jms\queue) or simply add all the JAR files contained in the ARTEMIS_HOME/lib folder. You need additionally the commons-collections-X.jar in your classpath.

Output:

run:
Sent message: This is a text message
Received message: This is a text message
BUILD SUCCESSFUL (total time: 2 seconds)

Advertisement

Cookie Alert