Comparison of the "Message Quest Client" framework in the Java class library comparison
Comparison of the "Message Quest Client" framework in the Java class library comparison
Message Queue is a common communication model that is used to transmit and receive messages in a distributed system.The Java class library provides many message queue client frameworks to simplify the process of developers using the message queue.This article will compare several commonly used Java message queue client frameworks, including Apache Kafka, Rabbitmq and ActiveMQ, and provide related Java code examples.
1. Apache Kafka:
Apache Kafka is a high -performance, distributed message queue system that supports massive real -time data transmission.It uses a distributed log in to store messages, which has the characteristics of high throughput and low latency.Below is an example code of Apache Kafka producers and consumers.
Example code:
// producer
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
public class KafkaProducerExample {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("bootstrap.servers", "localhost:9092");
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String, String> producer = new KafkaProducer<>(properties);
String topic = "my-topic";
String key = "key1";
String value = "Hello, Kafka!";
ProducerRecord<String, String> record = new ProducerRecord<>(topic, key, value);
producer.send(record);
producer.close();
}
}
// Consumers
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import java.util.Arrays;
import java.util.Properties;
public class KafkaConsumerExample {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("bootstrap.servers", "localhost:9092");
properties.put("group.id", "my-group");
properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);
consumer.subscribe(Arrays.asList("my-topic"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
System.out.println("Received message: key = " + record.key() + ", value = " + record.value());
}
}
consumer.close();
}
}
2. RabbitMQ:
Rabbitmq is an open source, reliable message queue system that focuses on supporting high availability and scalability.It uses AMQP (senior message queue protocol) to transmit messages, providing rich functions, such as durable message, message route and message confirmation.Below is an example code using Rabbitmq producers and consumers.
Example code:
// producer
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
public class RabbitMQProducerExample {
private final static String QUEUE_NAME = "my-queue";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello, RabbitMQ!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
System.out.println("Sent message: " + message);
}
}
}
// Consumers
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
public class RabbitMQConsumerExample {
private final static String QUEUE_NAME = "my-queue";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println("Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println("Received message: " + message);
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {});
}
}
3. ActiveMQ:
ActiveMQ is a popular open source message middleware that supports a variety of message protocols.It provides reliable message transmission, persistent storage messages, and has scalability and high availability.The following is an example code using ActiveMQ producers and consumers.
Example code:
// producer
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class ActiveMQProducerExample {
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue");
MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("Hello, ActiveMQ!");
producer.send(message);
System.out.println("Sent message: " + message.getText());
connection.close();
}
}
// Consumers
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class ActiveMQConsumerExample {
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue");
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(message -> {
try {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message: " + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
});
System.out.println("Waiting for messages...");
Thread.sleep(5000);
connection.close();
}
}
By comparing several commonly used Java message queue client frameworks such as Apache Kafka, Rabbitmq and ActiveMQ, we can choose the framework that suits our project needs.No matter which framework is selected, it is necessary to comprehensively consider according to the specific business scenario and performance needs.I hope this article will provide you with some references and help when choosing a message queue client framework.
Note: The example code of this article is based on Java 8, which requires the corresponding Java class library and dependencies.