How Java implements message communication using Apache ActiveMQ
Apache ActiveMQ is an open source, multi language supported messaging middleware. It implements the specification of the Jakarta Messaging (JMS) API and provides the infrastructure of a distributed, message oriented system.
The advantages of ActiveMQ are as follows:
1. Reliability: ActiveMQ uses persistent message storage to ensure that messages are not lost in the event of downtime or network failure.
2. High performance: ActiveMQ uses asynchronous IO and highly optimized processes to achieve high throughput and low latency.
3. Multiple communication protocol support: ActiveMQ supports multiple communication protocols, including OpenWire, STOMP, AMQP, MQTT, etc.
4. Flexibility: ActiveMQ supports the creation of dynamic queues and topics, which can be dynamically adjusted according to system requirements.
5. Highly scalable: ActiveMQ can be deployed as a cluster to achieve high availability and load balancing.
The following is the complete sample code for sending and receiving messages using Apache ActiveMQ:
Firstly, it is necessary to add Apache ActiveMQ dependencies in pom.xml:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>5.16.0</version>
</dependency>
Example code for sending messages:
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class MessageProducer {
public static void main(String[] args) {
//Create Connection Factory
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
try {
//Create Connection
Connection connection = factory.createConnection();
connection.start();
//Create Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//Create Destination
Destination destination = session.createQueue("testQueue");
//Create Message Producer
MessageProducer producer = session.createProducer(destination);
//Create Message
TextMessage message = session.createTextMessage("Hello, ActiveMQ!");
//Sending messages
producer.send(message);
//Close Resources
producer.close();
session.close();
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
Example code for receiving messages:
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class MessageConsumer {
public static void main(String[] args) {
//Create Connection Factory
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
try {
//Create Connection
Connection connection = factory.createConnection();
connection.start();
//Create Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//Create Destination
Destination destination = session.createQueue("testQueue");
//Create message consumers
MessageConsumer consumer = session.createConsumer(destination);
//Set up message listeners
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("Received message: " + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
//Blocking waiting messages
System.in.read();
//Close Resources
consumer.close();
session.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sample configuration:
In the above code, the parameters for connecting to the factory are“ tcp://localhost:61616 Represents the address and port of the ActiveMQ server to which the connection is made.
Official website link: https://activemq.apache.org/