http://activemq.apache.org/message-groups.html

与Exclusive Consumer相比,Message Groups的对消息分组的粒度更细。具有相同groupId的消息会被投送到同一个消费者,除非这个消费者挂了。

代码示例:

Mesasge message = session.createTextMessage("<foo>hey</foo>");
// 设置groupId
message.setStringProperty("JMSXGroupID", "IBM_NASDAQ_20/4/05");
// 设置sequence
message.setIntProperty("JMSXGroupSeq", -1); producer.send(message);

对应的代码在 org.apache.activemq.broker.region.Queue 中:

// 判断消息能否分发给消费者,返回true表示可以
// Subscription 表示消费者,QueueMessageReference 表示消息
protected boolean assignMessageGroup(Subscription subscription, QueueMessageReference node)
throws Exception {
// 默认为true
boolean result = true;
// Keep message groups together.
// 获取消息的"JMSXGroupID"属性
String groupId = node.getGroupID();
// 获取消息的"JMSXGroupSeq"属性
int sequence = node.getGroupSequence();
if (groupId != null) {
// MessageGroupMap是一个Map,键是groupId,值是消费者
MessageGroupMap messageGroupOwners = getMessageGroupOwners();
// If we can own the first, then no-one else should own the
// rest.
if (sequence == 1) {
assignGroup(subscription, messageGroupOwners, node, groupId);
} else { // Make sure that the previous owner is still valid, we may
// need to become the new owner.
ConsumerId groupOwner;
// 根据groupId取出消费者
groupOwner = messageGroupOwners.get(groupId);
if (groupOwner == null) {
assignGroup(subscription, messageGroupOwners, node, groupId);
} else {
if (groupOwner.equals(subscription.getConsumerInfo().getConsumerId())) {
// A group sequence < 1 is an end of group signal.
if (sequence < 0) {
messageGroupOwners.removeGroup(groupId);
subscription.getConsumerInfo().
setLastDeliveredSequenceId(subscription.getConsumerInfo().getLastDeliveredSequenceId() - 1);
}
} else {
result = false;
}
}
}
} return result;
} // 往MessageGroupMap中插入键值对
protected void assignGroup(Subscription subs, MessageGroupMap messageGroupOwners,
MessageReference n, String groupId) throws IOException {
messageGroupOwners.put(groupId, subs.getConsumerInfo().getConsumerId());
Message message = n.getMessage();
message.setJMSXGroupFirstForConsumer(true);
subs.getConsumerInfo().
setLastDeliveredSequenceId(subs.getConsumerInfo().getLastDeliveredSequenceId() + 1);
}

ActiveMQ Message Groups的更多相关文章

  1. JMS学习(三)ActiveMQ Message Persistence(转)

    1,JMS规范支持两种类型的消息传递:persistent and non-persistent.ActiveMQ在支持这两种类型的传递方式时,还支持消息的恢复.中间状态的消息(message are ...

  2. JMS学习(三)ActiveMQ Message Persistence

    1,JMS规范支持两种类型的消息传递:persistent and non-persistent.ActiveMQ在支持这两种类型的传递方式时,还支持消息的恢复.中间状态的消息(message are ...

  3. ActiveMQ 中 consumer 的优先级,message 的优先级

    http://activemq.apache.org/consumer-priority.htmlconsumer 优先级 http://activemq.apache.org/activemq-me ...

  4. ActiveMQ消息队列的使用及应用

    这里就不说怎么安装了,直接解压出来就行了. 谢绝转载,作者保留所有权力 目录:  一:JMQ的两种消息模式 1.1:点对点的消息模式 1.2:订阅模式 二:点对点的实现代码 2.1:点对点的发送端 2 ...

  5. ActiveMQ笔记(1):编译、安装、示例代码

    一.编译 虽然ActiveMQ提供了发布版本,但是建议同学们自己下载源代码编译,以后万一有坑,还可以尝试自己改改源码. 1.1 https://github.com/apache/activemq/r ...

  6. ActiveMQ 简介与安装

    一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provide ...

  7. JMS消息中间件系列[ActiveMQ](一)

    版本5.13.3的特性: 1.Supports a variety of Cross Language Clients and Protocols from Java, C, C++, C#, Rub ...

  8. 关于ActiveMQ的问题分析

    目前常用的消息队列组建无非就是MSMQ和ActiveMQ,至于他们的异同,这里不想做过多的比较.简单来说,MSMQ内置于微软操作系统之中,在部署上包含一个隐性条件:Server需要是微软操作系统.(对 ...

  9. MQ学习(二)----ActiveMQ简介(转)

    1.  什么是ActiveMQ ActiveMQ是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信.ActiveMQ使用A ...

随机推荐

  1. linq to sql and linq to object 总结

    Enumable类型是linq to object   是一个很特殊的类型   这个类型的数据源都是在程序的内存中 Queryable类型是 Linq to sql   对数据库进行操作都是这个类型  ...

  2. SqlServer中exists和in的区别

    1.in 2.exists

  3. python 文件保存 出错

    如果你出现如下错误: file save failed encoding failed: 'latin-1' codec can't encode characters in position 57- ...

  4. linux c/c++ 文件是否存在

    linux c/c++ 文件是否存在 #include <unistd.h> int FileExist(const char* fname) { return access(fname, ...

  5. ubuntu 安装cuda 9.1 pytorch 0.3.0

    毕业再没用配过机器学习的环境了,既亲切又陌生,久违了. 系统 mint18  x64 1安装cuda 按官网提示 选的9.1版  https://developer.nvidia.com/cuda-t ...

  6. Codeforces 377A - Maze

    A. Maze 题目链接:http://codeforces.com/contest/377/problem/A time limit per test 2 seconds memory limit ...

  7. 关于Python中的ifelse、while和for循环的简单小结

    1.ifelse 1.1首先简单编辑一个关于ifelse的程序: _username = 'yanfeixu' _password = 'wuyifan' username = input(" ...

  8. ubuntu16.04安装LCM

    1.sudo apt-get install build-essential autoconf automake autopoint libglib2.0-dev libtool openjdk-8- ...

  9. Python 编程快速上手 第五章总结

    第五章 字典和结构化数据 创建数组 格式:myCat = {'size':'fat','color':'gray',disposition':'loud'} 对字典的操作 通过[ ] 访问字典的值 [ ...

  10. 雷林鹏分享:XML 实例

    XML 实例 这些例子演示了 XML 文件.XML 格式化和 XML 转换(XSLT). 还演示了与 XML 一起使用的 JavaScript(AJAX). 查看 XML 文件 查看一个简单的 XML ...