一、是什么
二、MQ中间件的其他落地产品
消息队列的详细对比
三、JMS的组成和特点
1、JMS privider
实现JMS接口与规范的消息中间件,也就是我们的MQ服务器
2、JMS producer
消息生产者,创建与发送JMS消息的客户端应用
3、JMS consumer
消息的消费者,接受与处理JMS消息的客户端应用
4、JMS message
①、消息头
JMSdesination:消息发送的目的地,主要是指Queue与Topic
JMSDeliveryMode
JMSExpiration
JMSPriority
JMSMessageID:唯一识别每个消息的表示,由MQ产生
②、消息体
1、封装具体消息的数据
2、五种消息体的格式
3、发送和接受的消息体类型必须是一致对应的
③、消息属性
如果需要使用消息头以外的值,那么可以使用消息属性///一种加强型的api
识别/去重/重点标注等操作非常有用的方法
四、JMS的可靠性
1、persistent:持久性
①、参数设置说明
非持久
持久
默认是持久化的
②、持久的queue
③、持久的topic
注意:先启动订阅再启动生产
持久的发布主题生产者
package com.atguigu.activemq.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* @ClassName JmsProduce
* @Description TODO
* @Author DuanYueFeng
* @Version 1.0
**/
public class JmsProduce_Topic_p {
public static final String URL = "tcp://10.112.70.211:61616";
public static final String topic_name = "topic_p";
public static void main(String[] args) throws JMSException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(URL);
Connection connection = factory.createConnection();
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(topic_name);
MessageProducer producer = session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
connection.start();
for (int i = 1; i <=3; i++) {
TextMessage textMessage = session.createTextMessage("Topic mag..." + i);
producer.send(textMessage);
}
producer.close();
session.close();
connection.close();
System.out.println("完成....");
}
}
持久的订阅主题消费者
package com.atguigu.activemq.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.component.test.TestComponent;
import javax.jms.*;
import java.io.IOException;
/**
* @ClassName JmsConsumer
* @Description TODO
* @Author DuanYueFeng
* @Version 1.0
**/
public class JmsConsumer_Topic_p {
public static final String URL = "tcp://10.112.70.211:61616";
public static final String topic_name = "topic_p";
public static void main(String[] args) throws JMSException, IOException, InterruptedException {
System.out.println("我是3号消费者。。。");
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(URL);
Connection connection = factory.createConnection();
connection.setClientID("z3");
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(topic_name);
TopicSubscriber durableSubscriber = session.createDurableSubscriber(topic, "remark..");
Message message = durableSubscriber.receive();
while (null!=message){
TextMessage textMessage = (TextMessage)message;
System.out.println("收到的持久化:"+textMessage.getText());
message = durableSubscriber.receive(1000L);
}
System.in.read();
// Thread.sleep(1000);
session.close();
connection.close();
System.out.println("完成....");
}
}
类似微信公众号的订阅发布
测试先发送消息
消费者关机,消费者启动之后可以收到之前发的消息
测试先发送消息
消费者关机,服务器宕机,消费者启动之后可以收到之前发的消息
④、非持久的topic
没有意义,因为发布订阅模式是先启动订阅在启动生成,消息已经被消费了。如果先启动生产者后启动订阅者,消息会被当作废消息
2、transaction:事务
①、producer提交时的事务
②、事务偏生产者/签收偏消费者
3、Acknowledge:签收
五、JMS的点对点模式总结
六、JMS的发布订阅模式总结
1、非持久订阅
2、持久订阅
在发布端的持久化投递好像没有意义,注释之后,订阅者离线重上依然可以收到没有签收的消息。
3、用哪个
当所有消息必须被接受,则用持久订阅。当丢失消息能够被容忍,则用非持久订阅