JMS specifications and message characteristics (1)

What is the JMS specification

它是JavaEE体系中的一项Message Service

Comparison of common message middleware

JMS composition and characteristics

JMS provider

实现jms接口的消息中间件

JMS Producer 、 JMS Constomer


JMS Message header

1)jms destination 消息目的地 队列或者主题
2)jms deviverymode 持久化方式
3)jms expiration 消息过期时间 
4)jms 优先级 1到4是普通消息  5-9是加急消息
5)消息id  唯一识别每个消息的标识,是有MQ自己生成
The destination of the message header

Insert picture description here
Of course, it can also be set through the message
Insert picture description here

Four overloads: destination, message, priority, time to live, whether to persist or not
. Destination of message: queue and topic

Persistence
Insert picture description here
The expiration time of the message will never expire by default

Informed body

发送的消息类型有哪些:
StringMessage MapMessage ByteMessage StringMessage ObjectMessage 五中类型

要求:发送的消息体和接受的消息体要求类型一致。

Insert picture description here
Requirement: The sent message body and the received message body require the same type.
Insert picture description here

Custom message attributes

自定义的消息属性能有什么还用呢 ?
去重、识别、重点标注等	

TextMessage textMessage = session.createTextMessage("myTopic……"+ i );
messageProducer.send(textMessage);
textMessage.setStringProperty("自定义消息的key", "自定义消息的value");

How to ensure the reliability of the message? ? ?

消息的可靠性可以从以下四个方面来回答:
1)消息的持久性
2)消息的事务特性
3)消息的签收机制
4)消息持久化

(Queue) Persistence of messages

Insert picture description here
Verification 1:
Set the message to be non-persistent, then produce the message, (the server is not shut down), and then consume the message. The
Insert picture description here
message is normally consumed.
Insert picture description here
Verification 2:
Set the message to be non-persistent, then produce the message, (the server is closed), and then consume it Message
Generated message After the
Insert picture description here
server is closed, go to consume the message. The
Insert picture description here
message is lost and cannot be consumed.
The message just generated is lost.
Insert picture description here
Verification 3: Set the message to be persistent, and then generate the message. This is the message just generated.
Insert picture description here
Shut down the message server, restart the message again, the message still exists
Insert picture description here
to consume the message. The message was successfully consumed
Insert picture description here
Insert picture description here

(Topic) the persistence of the message

For topic messages, persistence does not make much sense, because in topic mode, you need to start the consumer first, and then start the producer. If message persistence is set, but the consumer has not been started, these messages will be Lost and cannot be consumed by consumers

Set the persistent topic generator

package com.ttzz.activemq;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQProduceByTopic {
    
    
	public static String url = "tcp://localhost:61616";
	public static String topicName = "myTopic";
	
	public static void main(String[] args) throws JMSException {
    
    
		//1.获取工厂
		ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(url);
		//2. 创建连接
		Connection connection = activeMQConnectionFactory.createConnection();
		
		//3.创建会话
		// 第一个参数 是否开启开启事务
		// 第二个参数 是签收模式
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//4. 创建目的地 Topic 
		Topic topic =session.createTopic(topicName);
		//5. 创建生产者
		MessageProducer messageProducer = session.createProducer(topic);
		
		messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
		connection.start();
		//6. 发送消息
		for (int i = 0; i < 4; i++) {
    
    
			TextMessage textMessage = session.createTextMessage("myTopic……"+ i );
			textMessage.setStringProperty("自定义消息的key", "自定义消息的value");
			messageProducer.send(textMessage);
		}
		//关闭资源
		messageProducer.close();
		session.close();
		connection.close();
		System.out.println("OOKK");
	}
}

topic consumer

package com.ttzz.activemq;

import java.io.IOException;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSubscriber;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQConsumerByTopic {
    
    
	public static String url = "tcp://localhost:61616";
	public static String topicName = "myTopic";

	public static void main(String[] args) throws JMSException, IOException {
    
    
		// 1.获取工厂
		ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(url);
		// 2. 创建连接
		Connection connection = activeMQConnectionFactory.createConnection();
		
		connection.setClientID("消费者1");
		System.out.println("topic消费者1");
		
		// 3.创建会话
		// 第一个参数 是否开启开启事务
		// 第二个参数 是签收模式
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		// 4. 创建目的地Topic
		Topic topic = session.createTopic(topicName);
		// 5. 创建消费者
		TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic, "remark..."); // 创建持久化的订阅

		connection.start();

		Message message = topicSubscriber.receive();
		while (message != null) {
    
    
			TextMessage textMessage = (TextMessage) message;
			System.out.println(textMessage.getText());
			message = topicSubscriber.receive();
		}

		session.close();
		connection.close();
		System.out.println("OOKK2");
	}
}

Verification 1: Start a consumer
Insert picture description here
Insert picture description here
Active Durable Topic Subscribers: persistent topic consumers in active state
Offline Durable Topic Subscribers: persistent topic consumers in offline state
Start persistent topic generators:
topic consumers consume messages
Insert picture description here
Message server
Insert picture description here
Insert picture description here
Verification 2: Turn off consumer 1 and start consumer 2 and
Insert picture description here
consumer 1 is offline and start producer consumption.
Insert picture description here
Consumer 2 can consume messages normally.
Insert picture description here
Start consumer 1 again, and consumer 1 can also consume messages normally.
Insert picture description here
Insert picture description here

Transaction characteristics of the message

The transaction is mainly for the producer, and the signing is mainly for the consumer

	//3.创建会话
	// 第一个参数 是否开启开启事务
	// 第二个参数 是签收模式
	Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Verification 1: The
transaction is set to false, and the message is sent to the server automatically.
Insert picture description here
Insert picture description here
Because the persistence of the message is set, the server is shut down and restarted again, the message still exists

Verification 2: The
transaction is set to true, the execution of the message is sent,
Insert picture description here

See if the server receives the message. The message just sent was not received from the server. Because there is no message commit operation, the
Insert picture description here
transaction is committed, the
Insert picture description here
message is enqueued, and the
Insert picture description here
transaction is sent for multiple messages at the same time, which can guarantee the atomicity of
session.rollback();

In order to verify the need, when you need to restart, delete the persistent message

Operation: Add deleteAllMessagesOnStartup="true" in the broker field of the configuration file activemq.xml
Insert picture description here
Insert picture description here
to see that the persistent messages are deleted.

The producer starts the transaction and sends the message to the message server.
Insert picture description here
Seeing the message, the
Insert picture description here
consumer consumes the message.
Insert picture description here
Starts a consumer again and finds that the message has been consumed, indicating that the message cannot be re-consumed.
Insert picture description here
Verification 2: Set the consumer to start the transaction, but no submission Transaction [message is repeatedly consumed]. For the first time, consumer 1 consumes the message normally,
Insert picture description here
but it sees on the server that the message has not been consumed.
Insert picture description here
Start another consumer again and find that the message can be consumed multiple times
Insert picture description here

An interesting phenomenon

The producer sends the producer to the server in a transactional manner. The
consumer starts the transaction for consumption, but the transaction is not committed. Ensure that the control is indestructible. I started consumer No. 2 again, and found that I can’t repeat consumption? ? ?
Insert picture description here
Insert picture description here
If (remove: System.in.read();) after setting the consumer for 4 seconds, there is no message, it will be closed automatically. Consumer No. 2 is activated and can be used repeatedly.
The reason? ? ? Hahahaha

Guess you like

Origin blog.csdn.net/weixin_39472101/article/details/114993631