Introduction to message middleware ActiveMQ

ef395f780af64c28b712784a59aedffa.gif1. Introduction to message middleware

 

introduce

​ Message queue refers to the use of efficient and reliable message delivery mechanism for platform-independent data exchange, and the integration of distributed systems based on data communication.

 

Features (function)

application decoupling

asynchronous communication

flow clipping

(Massive) log processing

newsletter

…...

Application Scenario

According to the characteristics of the message queue, many scenarios can be derived, or many scenarios can be used. Here are a few examples:

 

1) Asynchronous communication

 

​ SMS and email notifications during registration to reduce response time;

 

2) Application decoupling

 

​ The message sender and message receiver do not need to be coupled, such as calling a third party;

 

3) Flow peak clipping

 

​ For example, the seckill system;

 

2. Comparison of message middleware

1.ActiveMQ

Official website: activemq.apache.org/

 

Introduction:

 

ActiveMQ is the most popular and powerful open source message bus produced by Apache. ActiveMQ is a JMS Provider implementation that fully supports JMS1.1 and J2EE 1.4 specifications. Although JMS specifications have been released for a long time, JMS still plays a special role in today's J2EE applications.

 

Features:

 

Supports various cross-language clients and protocols from Java, C, C++, C#, Ruby, Perl, Python, PHP

 

Full support for enterprise integration patterns in JMS clients and Message Brokers

 

Supports many advanced features such as message groups, virtual destinations, wildcards and compound destinations

 

Full support for JMS 1.1 and J2EE 1.4, supporting transient, persistent, transactional and XA messages

 

Spring support so that ActiveMQ can be easily embedded in Spring applications and configured using Spring's XML configuration mechanism

 

Designed for high-performance cluster, client-server, peer-to-peer based communication

 

CXF and Axis support so that ActiveMQ can be easily dropped into these web service stacks to provide reliable messaging

 

Can be used as an in-memory JMS provider, great for unit testing JMS

 

Supports pluggable transport protocols such as in-VM, TCP, SSL, NIO, UDP, multicast, JGroups and JXTA transports

 

Supports very fast persistence using JDBC and high-performance logging

 

2.RabbitMQ

Official website: www.rabbitmq.com/

 

Introduction:

 

RabbitMQ is an open source implementation of AMQP developed in Erlang language. RabbitMQ is lightweight and easy to deploy in the cloud. It supports several messaging protocols. RabbitMQ can be deployed in distributed and federated configurations for high-scale, high-availability needs. RabbitMQ runs on many operating systems and cloud environments, and provides extensive development tools for most popular languages. (translated from the official website)

 

AMQP (Advanced MessageQueue): Advanced Message Queuing Protocol. It is an open standard of the application layer protocol, designed for message-oriented middleware, and the client and message middleware based on this protocol can transfer messages without being restricted by conditions such as products and development languages.

 

RabbitMQ was originally widely used in the financial industry. According to the official website description, it has the following characteristics:

 

Features:

 

Asynchronous messaging: supports multiple messaging protocols, message queues, delivery confirmations, flexible routing to queues, and multiple exchange types;

Supports almost all the most popular programming languages: Java, C, C++, C#, Ruby, Perl, Python, PHP and many more;

Can be deployed as a cluster for high availability and throughput; federation across multiple availability zones and zones;

Pluggable authentication, authorization, support for TLS and LDAP. ;

Provides an easy-to-use user interface that allows users to monitor and manage many aspects of the message broker;

Many plug-ins are provided to extend it in many ways, and you can also write your own plug-ins.

3. Kafka

Official website: kafka.apache.org/

 

Introduction:

 

Kafka is an open source stream processing platform developed by the Apache Software Foundation, written in Scala and Java. Kafka is a high-throughput distributed publish-subscribe messaging system that can handle all action streaming data in consumer-scale websites. Such actions (page browsing, searching, and other user actions) are a key factor in many social functions on the modern web. These data are usually addressed by processing logs and log aggregation due to throughput requirements. This is a viable solution for systems like Hadoop that log data and analyze offline, but require real-time processing constraints. The purpose of Kafka is to unify online and offline message processing through Hadoop's parallel loading mechanism, and to provide real-time messages through clusters.

 

Kafka is mainly used to process active streaming data, so Kafka is used more in big data systems.

 

Features:

 

Provides high throughput for both publish and subscribe. It is understood that Kafka can produce about 250,000 messages (50 MB) per second and process 550,000 messages (110 MB) per second.

 

Persistence is possible. Persist messages to disk, so can be used for batch consumption, such as ETL, as well as real-time applications. Prevent data loss by persisting data to hard disk and replication.

 

Distributed system, easy to scale out. There will be multiple producers, brokers, and consumers, all of which are distributed. Machines can be expanded without downtime.

 

The status of the message being processed is maintained on the consumer side, not on the server side. Can automatically balance when it fails.

 

Supports online and offline scenarios.

 

4. RocketMQ

Official website: rocketmq.apache.org/

 

Introduction:

 

RocketMQ is Alibaba's open source message middleware. It is currently incubated in Apache and developed in pure Java. It has the characteristics of high throughput, high availability, and is suitable for large-scale distributed system applications. The RocketMQ idea originated from Kafka, but it is not a simple copy. It optimizes the reliable transmission and transactional nature of messages. It is currently widely used in transactions, recharge, stream computing, message push, log streaming processing, and binglog in Ali Group. Distribution and other scenarios have supported Ali’s many Double Eleven activities.

 

Features:

 

Supports publish/subscribe (Pub/Sub) and peer-to-peer (P2P) messaging models

Reliable first-in-first-out (FIFO) and strict order delivery in a queue

Support pull (pull) and push (push) two message modes

Accumulation capacity of millions of messages in a single queue

Support multiple message protocols, such as JMS, MQTT, etc.

Distributed and highly available deployment architecture that satisfies at least one message delivery semantics

Provide docker images for isolation testing and cloud cluster deployment

Provides a feature-rich Dashboard for configuration, metrics, and monitoring

3. Installation of ActiveMQ

1. Installation steps

activemq has corresponding installation packages under each system. The following demonstrates the installation of activemq under the Linux system.

 

 

 

Enter the apache-activemq-5.15.8/bin directory and start activemq./activemq start

 

 

 

If the above information is output, the startup is successful.

 

2. Problems encountered in installation

During the installation process, by checking the running status of activemq,

 

 

 

shown above.

 

View the operation log through the ./bin/activemq console command:

 

 

 

The hostname contains illegal characters;

 

Then the solution is very simple, change the hostname:

 

1. Method 1: Use the hostnamectl command

 

hostnamectl set-hostname hostname

 

2. Method 2: Modify the configuration file /etc/hostname, save and exit

 

After the modification is complete, restart it. Here I use method 1:

 

hostnamectl set-hostname activemq

 

Check the running status:

 

 

 

Five, ActiveMQ page introduction

After ActiveMQ is installed and started, visit http://ip:8161/admin, the login name and password are both admin (can be modified in the configuration file), and enter the homepage of ActiveMQ:

 

 

 

The function of each menu is introduced as follows:

 

1. Queue message queue page

 

 

Name: The name of the message queue.

 

Number Of Pending Messages: The number of messages that have not been consumed.

 

Number Of Consumers: The number of consumers.

 

Messages Enqueued: messages entering the queue; the total number of messages entering the queue, including those that have been consumed and those that have not been consumed. This number will only increase.

 

Messages Dequeued: The messages out of the queue can be understood as the number of messages consumed. In Queues, it is equal to the total number of queues (because a message will only be successfully consumed once), if it is not waiting temporarily, it is because the consumer has not had time to consume.

 

2. Topic theme page

 

 

Name: topic name.

 

Number Of Pending Messages: The number of messages that have not been consumed.

 

Number Of Consumers: The number of consumers.

 

Messages Enqueued: messages entering the queue; the total number of messages entering the queue, including those that have been consumed and those that have not been consumed. This number will only increase.

 

Messages Dequeued: The messages out of the queue can be understood as the number of messages consumed. In Topics, due to multiple consumers, the number will be higher than the number of queues.

 

3.Subscribers view the subscriber page

 

 

View subscriber information, this page will only have data in the Topics message type.

 

4.Connections view the number of connections page

 

 

Six, easy to use

Import the jar package:

 

        <dependency>

            <groupId>org.apache.activemq</groupId>

            <artifactId>activemq-core</artifactId>

            <version>5.7.0</version>

        </dependency>

1. Peer-to-peer (P2P) model

​ The point-to-point model uses a queue (Queue) as a message carrier. In this mode, a message can only be consumed by one consumer, and those that have not been consumed can only be left in the queue, waiting to be consumed, or timed out. For example, if there are 10 messages in the queue and there are two consumers, one consumer consumes 5 messages, one for you and one for me. The following is a code demonstration.

 

Message publisher:

 

public static void main(String[] args) throws JMSException {

    /*

     * Implementation steps

     * 1. To create a ConnectionFactory factory object, you need to fill in the user name, password, and connection address (generally use the default, if not modified)

     * 2. Create a Connection connection through the ConnectionFactory object, and call the start method of the Connection to open the connection. The Connection method is closed by default

     * 3. Create a Session session (context environment object) through the Connection object to receive messages. Parameter 1 is whether to enable things, and parameter 2 is the sign-in mode, which is generally set to automatic sign-in

     * 4. Create a Destination object through the Session object, which refers to an object used by the client to formulate the production message target and consume the message source. In the PTP mode, the Destination is called a queue, and in the Pub/Sub mode, the Destination is called a topic (Topic)

     * 5. Create message sending and receiving objects (producers and consumers) through the Session object

     * 6. Set persistent or non-persistent characteristics for it through the setDeliverMode method of MessageProducer

     * 7. Use the TextMessage form of the JMS specification to create data (through the Session object), and use the send method of MessageProducer to send the data. The same goes for the client. remember to close

     */

    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,

            ActiveMQConnectionFactory.DEFAULT_PASSWORD,"tcp://94.191.49.192:61616");

    Connection connection = connectionFactory.createConnection();

    connection.start();

    Session session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);

    Destination destination = session.createQue

Guess you like

Origin blog.csdn.net/weixin_57763462/article/details/131949275