topic topic storage, used for subscription/consumption model. The message in the topic will be sent to the consumer for processing at the same time, and only used in scenarios where the message can be processed repeatedly
queue Queue storage, often used in point-to-point models. By default, it can only be processed by only one consumer. Once the consumer message is deleted
Simple comparison
1
Topic
Queue
Publish/Subsribe Message
point to point
Status
The default data is not landing, it is stateless
Queue data is generally stored in the form of files on the server, and can also be configured as DB storage
Integrity guarantee
It is not guaranteed that every piece of publish data will be received by subscribe
Ensure that every piece of data can be received without timeout
Whether the message will be lost
Generally speaking, when a publish message arrives on a topic, only the listening sub can receive it. If there is no listening, the topic will be lost.
The sender sends the message to the queue, and the reciever can receive the message asynchronously, and the message will not be lost if the time is occupied. If the message does not time out
Message publishing and receiving strategy
One-to-many message publishing strategy, multiple subs listening to the same topic address can receive messages
One-to-one publishing strategy, a message published by a sender can only be consumed by one reciever, and MQ is notified to delete or perform other operations after consumption.
Steps to send a message
Get the connection factory of activityMQ
Get an MQ connection
Get session
Find the destination consumer end, and consume messages from this place
Create a message, write a message to the destination
Close the session
The JMS message consists of the following three parts
Message header
Each message header field has corresponding getter and setter methods
Message attribute
If you need a value other than the message header, you can use the message attribute
The message types defined by the message body JMS are TextMessage, MapMessage, BytesMessage, StreamMessage and ObjectMessage.
TextMessage text message
The form of MapMessage K/V
BytesMessage byte stream
StreamMessage Java primitive data stream
ObjectMessage serialized Java object
Application scenario
Asynchronous communication
Some businesses don’t want and don’t need to process messages immediately. Message queues provide an asynchronous processing mechanism that allows users to put a message into the queue but does not process it immediately. Put as much as you want in the queue, and then when you need it again Deal with
Buffering
in any important system requires elements of different processing time. Message queues use a buffer layer to help perform tasks with the highest efficiency. The buffer helps to control and optimize the speed of data flow through the system to adjust the system response time
Decoupling
reduces the degree of strong dependence between projects and adapts to heterogeneous systems. At the beginning of the project, it is extremely difficult to predict what needs the project will encounter in the future . Through the message system, an implicit data-based interface layer is inserted in the middle of the processing. The processing on both sides must implement this interface. When the application changes, the processing on both sides can be independently extended or modified, as long as you ensure They follow the same interface constraints. Message communication of heterogeneous systems
Redundancy In
some cases, the process of processing data will fail. Unless the data is persisted, it will be lost. The message queue persists the data until they have been completely processed, avoiding the risk of data loss in this way. Insert obtain delete paradigm many message queue used, the put a before the message is deleted from the queue, you need to clear out your processing system that the message has been processed to ensure that your data is securely saved straight to your use complete.
Scalability
Because the message queue decouples the processing process, it is easy to increase the frequency of message enqueuing and processing. You only need to increase the processing process. There is no need to change the code or adjust the parameters, which is convenient for distributed expansion. , That is, horizontal expansion