第十一章ActiveMQ代理器的新特性【笔记】
来源:互联网 发布:弯矩计算软件 编辑:程序博客网 时间:2024/06/05 06:27
概述:主要介绍代理器比较炫特效,好用的功能。
11.1、介绍
包括通配符、多个目的地,提示信息、虚拟话题(topic), 过期消息队列
11.2、通配符和多个目的地
11.2.1、订阅使用通配符目的地
有三种特殊通配符
. : 点 用于区分目的地名称的元素
* : 星号,匹配一个元素(这个和一般通配符不一样)
> : 右尖括号,配置一个或多个元素
String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);Connection connection = connectionFactory.createConnection();connection.start();Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);Topic allLeeds = session.createTopic("*.*.Leeds");MessageConsumer consumer = session.createConsumer(allLeeds);Message result = consumer.receive();
11.2.2.将消息发送到多个目的地
以逗号隔开 例如:session.createQueue("store.orders, topic://store.orders");
String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);Connection connection = connectionFactory.createConnection();connection.start();Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);Queue ordersDestination = session.createQueue("store.orders, topic://store.orders");MessageProducer producer = session.createProducer(ordersDestination);Message order = session.createObjectMessage();producer.send(order);
11.3、提示信息
系统提示信息,生成提示信息会产生Advisory相关属性
originBrokerId : 生成Id
originBrokerName: 名称
originBrokerURL : 传输url地址
String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);Connection connection = connectionFactory.createConnection();connection.start();Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);Topic connectionAdvisory = org.apache.activemq.advisory.AdvisorySupport.CONNECTION_ADVISORY_MessageConsumer consumer = session.createConsumer(connectionAdvisory);ActiveMQMessage message = (ActiveMQMessage) consumer.receive();DataStructure data = (DataStructure) message.getDataStructure();if (data.getDataStructureType() == ConnectionInfo.DATA_STRUCTURE_TYPE) {ConnectionInfo connectionInfo = (ConnectionInfo) data;System.out.println("Connection started: " + connectionInfo);} else if (data.getDataStructureType() == RemoveInfo.DATA_STRUCTURE_TYPE) {RemoveInfo removeInfo = (RemoveInfo) data;System.out.println("Connection stopped: " + removeInfo.getObjectId());} else {System.err.println("Unknown message " + data);}
<destinationPolicy> <policyMap> <policyEntries> <policyEntry queue=">" advisoryForSlowConsumers="true" /> </policyEntries> </policyMap></destinationPolicy>
11.4、虚拟话题
将多个queue组成一个topic(只是虚拟,实际不存在)
String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);Connection consumerConnection = connectionFactory.createConnection();consumerConnection.start(); Session consumerSessionA = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);Queue consumerAQueue = consumerSessionA.createQueue("Consumer.A.VirtualTopic.orders");MessageConsumer consumerA = consumerSessionA.createConsumer(consumerAQueue);Session consumerSessionB = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);Queue consumerBQueue = consumerSessionB.createQueue("Consumer.B.VirtualTopic.orders");MessageConsumer consumerB = consumerSessionB.createConsumer(consumerAQueue);//setup the senderConnection senderConnection = connectionFactory.createConnection();senderConnection.start();Session senerSession = senderConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);Topic ordersDestination = senerSession.createTopic("VirtualTopic.orders");MessageProducer producer = senerSession.createProducer(ordersDestination);
11.5、重新连接消费者
可以在断开连接之后,保存发送的消息,连接上在发送
设置consumer.retroactive=true
String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);Connection connection = connectionFactory.createConnection();connection.start();Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);Topic topic = session.createTopic("soccer.division1.leeds?consumer.retroactive=true");MessageConsumer consumer = session.createConsumer(topic);Message result = consumer.receive();在服务端需要配置
<destinationPolicy> <policyMap> <policyEntries> <policyEntry topic=">"> <subscriptionRecoveryPolicy> <fixedSizedSubscriptionRecoveryPolicy /> </subscriptionRecoveryPolicy> </policyEntry> </policyEntries> </policyMap></destinationPolicy>
11.6、消息再次传递和失效消息队列
对于那些过期消息会移动到Dead Letter Queue(失效消息队列),随后可以由管理员查看
消息会重新传递的情况:
- 客户端使用事务时调用rollback()方法
- 客户端使用事务,在提交之前关闭。
- 客户端正在使用CLIENT_ACKNOWLEDGE,然后调用recover
配置恢复规则
RedeliveryPolicy policy = connection.getRedeliveryPolicy();policy.setInitialRedeliveryDelay(500);policy.setBackOffMultiplier(2);policy.setUseExponentialBackOff(true);policy.setMaximumRedeliveries(2);
配置失效消息队列
<destinationPolicy> <policyMap> <policyEntries> <policyEntry queue=">"> <deadLetterStrategy> <individualDeadLetterStrategy queuePrefix="DLQ." useQueueForQueueMessages="true" processExpired="false" processNonPersistent="false" /> </deadLetterStrategy> </policyEntry> </policyEntries> </policyMap></destinationPolicy>
注意:这个配置忽略非持久和过期消息
阅读全文
0 0
- 第十一章ActiveMQ代理器的新特性【笔记】
- Objective-C学习笔记第十一章特性
- 第十一章特性
- ActiveMQ学习笔记目的地特性
- ActiveMQ学习笔记-消息特性
- [Java5新特性]动态代理
- mysql的event学习笔记---新特性
- java8的新特性学习笔记一
- 【java学习笔记】java8的新特性
- html5的一些新特性笔记详解
- iOS8的新特性(MOdel笔记)
- 第十一章 Camera笔记
- JAVA新特性笔记
- JDK7新特性笔记
- php新特性笔记
- 笔记_PHP7新特性
- 新特性笔记
- HTML5新特性笔记
- 跨浏览器开发笔记
- 软件工程课程实验报告:实验四
- 端口号区间合并
- NEON 3x3 max pool示例代码
- Google EventBus 使用详解
- 第十一章ActiveMQ代理器的新特性【笔记】
- mysql+mmm+proxy实现mysql读写分离
- netty的tcp粘包与拆包
- linux基础(一)
- eclipse maven error: Archive for required library in project cannot be read or is not a valid ZIP fi
- Ehcache相关
- 【mui】窗口管理及窗口之间的数据传递
- sublime text3 安装codeFormatter
- android图片尺寸获取