MQ message queue

MQ message queue

Message Queuing Middleware is an important component in distributed system. It mainly solves the problems of application decoupling, asynchronous messages, traffic cutting and so on, and realizes the architecture of high performance, high availability, scalability and final consistency.

The usage scenarios mainly include asynchronous processing, application decoupling, traffic clipping and message communication

At present, ActiveMQ, RabbitMQ, ZeroMQ, Kafka, MetaMQ and RocketMQ are widely used message queues

JMS message service

JMS (JAVA Message Service) API is a standard / specification of message service, which allows application components to create, send, receive and read messages based on java EE platform. It makes the coupling degree of distributed communication lower, the message service more reliable and asynchronous.

Message model

Two message models, P2P (Point to Point), Publish/Subscribe(Pub/Sub)

P2P

Characteristics of P2P

  • Each message has only one Consumer (that is, once consumed, the message is no longer in the message queue)
  • There is no time dependency between the sender and the receiver, that is, after the sender sends the message, whether the receiver is running or not, it will not affect the message being sent to the queue
  • After successfully receiving the message, the receiver needs to reply to the queue

If you want every message sent to be processed successfully, you need P2P mode.

Pub/Sub mode

  • Each message can have multiple consumers
  • There is a time dependency between publishers and subscribers. For a subscriber of a Topic, it must create a subscriber before consuming the publisher's message
  • In order to consume messages, subscribers must remain running
  • To mitigate such strict temporal dependencies, JMS allows subscribers to create a persistent subscription. In this way, even if the subscriber is not activated (running), it can receive the publisher's message.
  • If the message you want to send can be processed without any processing, by only one messenger, or by multiple consumers, you can use the Pub/Sub model.

Message consumption

In JMS, the generation and consumption of messages are asynchronous. For consumption, JMS message providers can consume messages in two ways.

synchronization

The subscriber or receiver receives the message through the receive method, which will be blocked until the message is received (or timeout);

#####Asynchronous
A subscriber or recipient can register as a message listener. When the message arrives, the system automatically calls the onMessage method of the listener.

Common message oriented middleware

ActiveMQ

ActiveMQ is the most popular and powerful open source message bus produced by Apache. ActiveMQ is a fully supported JMS 1 1 and J2EE 1.4 specifications. Although the JMS specification has been issued for a long time, JMS still plays a special role in today's J2EE applications.

Kafka

Kafka is a high-throughput distributed publish subscribe message system, which can process all action flow data in consumer scale websites. Such actions (web browsing, search and other user actions) are a key factor in many social functions on modern networks. These data are usually solved by processing logs and log aggregation due to throughput requirements. This is a feasible solution for the log data and offline analysis system like Hadoop, but it requires real-time processing. Kafka aims to unify online and offline message processing through Hadoop's parallel loading mechanism, and also to provide real-time consumption through cluster machines.

Basic use of ActiveMQ

install

http://activemq.apache.org/download-archives.html

Because springboot5 Activemq5 is supported in 2.2 16, so install a similar version

After downloading, unzip and execute ActiveMQ Bat start starts. If you can successfully visit http://localhost:8161/admin . If the user name and password are admin by default, the startup is successful

Programming use

rely on

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

Core configuration application properties

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin

Add annotations on the main class to start JMS support

@EnableJms
@SpringBootApplication
public class Mq01Application {
    public static void main(String[] args) {
        SpringApplication.run(Mq01Application.class, args);
    }
}

Add a configuration class to define Queue and Topic objects

@Configuration
public class ActiveMqConfig {
    @Bean(name = "testQueue")
    public Queue queue() {
        return new ActiveMQQueue("test.myqueue");
    }
}

Defines the producer of the message

@SpringBootTest
class Mq01ApplicationTests {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Queue destination;
    @Test
    void contextLoads() {
        jmsMessagingTemplate.convertAndSend(destination, "message");
    }

}

Define the consumer of the message

  • There is no coupling between producers and consumers
  • Messages are received asynchronously
@Component
public class QueueConsumerListener {
    @JmsListener(destination="test.myqueue")
    public void readActiveQueue(String message) {
        System.out.println("queue Received:" + message);
    }
}

Producer of topic mode

//Parameter 1 is a Topic object
 jmsMessagingTemplate.convertAndSend(destination, "message===");

This topic object is defined in the configuration class

@Configuration
public class ActiveMqConfig {

    @Bean(name = "testTopic")
    public Topic topic() {
        return new ActiveMQTopic("test.mytopic");
    }
}

topic mode consumers

problem

Expiration Policies

Dead letter queue

Slow consumer strategy

Added by nokicky on Fri, 21 Jan 2022 05:38:50 +0200