Apache ActiveMQ Eclipse Implementation(2024) Example | CodeUsingJava






















Apache ActiveMQ Eclipse Implementation

In this tutorial, you will learn how to use ActiveMQ and implement the same in Eclipse.

Introduction

ActiveMQ is an open-source, MOM (Message Oriented Middleware) that implements the JMS API. It is lightweight and open source. For this example, we will discuss how to use ActiveMQ as a JMS provider.

How to Set Up ActiveMQ

  • Download ActiveMQ from Download ActiveMQ.
  • Extract the ActiveMQ downloaded zip file to any location on your computer.
  • Open command prompt (cmd) and go to the bin directory of extracted ActiveMQ folder.
  • Type activemq.bat start, to start ActiveMQ.
  • The command prompt will state that ActiveMQ has been started by this statement- INFO | Apache ActiveMQ 5.14.5 (localhost, ID:XXXX) started (please refer to screenshot). Please refer to the ActiveMQ image below.
    ActiveMQ
  • You can additionally open the ActiveMQ console using the link "http://localhost:8161/admin/" with 'admin'/'admin' as username and password. It should open as below.
    ActiveMQ
  • The ActiveMQ console gives us info on the queue and topic which can be seen during the exchange of messages.

Implementation

  • We can now create a dynamic web project in eclipse and create the classes MessageSender and MessageReceiver to see how a message is exchanged using ActiveMQ.
  • Copy the jar files which have been from extracted ActiveMQ/lib folder in your system to the lib folder of the dynamic web project created in eclipse software.
  • We have to create two classes, one that will send the message (MessageSender.java) and another that will receive the message (MessageReceiver.java). Please refer code snippets below.
Our project would look something like this:
Project
This class sends a text message to the queue. The JMS class MessageProducer.java has a method called send(), which will be used in this case to send the message.
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
 
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
 
public class MessageSender {
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
     
    private static String subject = "JCG_QUEUE"; 
     
    public static void main(String[] args) throws JMSException {     
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection myConnection = connectionFactory.createConnection();
        myConnection.start();
        Session mySession = myConnection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);  
        Destination destination = mySession.createQueue(subject); 
        MessageProducer producer = mySession.createProducer(destination);
         
        TextMessage myMessage = mySession
                .createTextMessage("Welcome to JavaInUse.");
        producer.send(myMessage);
         
        System.out.println("JCG printing@@ '" + myMessage.getText() + "'");
        myConnection.close();
    }
}
The next class receives the text message from the queue. The JMS class MessageConsumer.java has a method called receive(), which is used here to receive the message.
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
 
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
 
public class MessageReceiver {
 
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

    private static String subject = "JCG_QUEUE";
 
    public static void main(String[] args) throws JMSException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection myConnection = connectionFactory.createConnection();
        myConnection.start();
 
        Session session = myConnection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);
 
        Destination myDestination = session.createQueue(subject);
 
        MessageConsumer sendToConsumer = session.createConsumer(myDestination);
 
        Message myMessage = sendToConsumer.receive();
 
        if (myMessage instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) myMessage;
            System.out.println("Received message '" + textMessage.getText() + "'");
        }
        myConnection.close();
    }
}
So now we have the following things:
  • A JMS provider is executing (Running ActiveMQ as displayed in the image above.)
  • We have our MessageSender and MessageReceiver classes.
  • Next, we will discuss how a message is exchanged utilizing Active MQ but before that, here is a brief about the classes that we have used in our code.
Connection: It expresses the connection with the JMS provider - ActiveMQ. It is not a database connection.
Destination: It expresses a destination on our message provider ActiveMQ i.e. it denotes a queue where we will be sending our messages. In this example, we are sending it to queue "JCG_QUEUE" which will be created automatically once the MessageSender class is executed.
MessageProducer: It signifies a producer to send a message to the queue.
MessageConsumer: It signifies a receiver receiving a message from the queue.

Output

We will now execute our java classes written above to check how the message is exchanged.
Please follow the steps below:
  • In eclipse, right-click on MessageSender.java -> Run As->Java Application to see if the message is sent to the queue. The 'Hello' message after successfully being transferred to the queue will be printed in the eclipse output console.
    ActiveMQ Output
  • We can also review the ActiveMQ console->Queues tab, to view the number of pending messages in our queue after we run the MessageSender.java
    ActiveMQ Output
    The producer task is done. It will just form a queue and send a message to it. It will not be mentioned in the producer who reads the message.
    Next will be our MessageReceiver class which has a connection and a destination same as was explained before. Here we also mention the same queue name that has been used in the MessageSender class to know that we are going to select the message from the same queue. The consumer.receive() receives the message. Once the class is run, the message will be received from the queue and is printed in the eclipse output console when we will execute the class.
  • In eclipse, right-click on MessageReceiver.java -> Run As->Java Application to check if the message has been received from the queue. The 'hello' message after it is successfully received, gets printed in the eclipse output console.
    ActiveMQ Output
  • We can also examine our ActiveMQ console->Queues tab, to see the number of dequeued messages from our queue after executing the MessageReceiver.java
    ActiveMQ Output