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

http://activemq.apache.org/activemq-message-properties.html 消息优先级

1、设置 consumer 的优先级:

queue = new ActiveMQQueue("TEST.QUEUE?consumer.priority=10");
consumer = session.createConsumer(queue);

priority 的取值从0到127。broker 按照 consumer 的优先级给 queue 的 consumers 排序,首先把消息分发给优先级最高的 consumer。一旦该 consumer 的 prefetch buffer 满了,broker 就把消息分发给优先级次高的,prefetch buffer 不满的 consumer。

// org.apache.activemq.broker.region.Queue
// consumer priority 的比较器
private final Comparator<Subscription> orderedCompare = new Comparator<Subscription>() { @Override
public int compare(Subscription s1, Subscription s2) {
// We want the list sorted in descending order
// 倒序,即数值大的优先级高
int val = s2.getConsumerInfo().getPriority() - s1.getConsumerInfo().getPriority();
if (val == 0 && messageGroupOwners != null) {
// then ascending order of assigned message groups to favour less loaded consumers
// Long.compare in jdk7
long x = s1.getConsumerInfo().getLastDeliveredSequenceId();
long y = s2.getConsumerInfo().getLastDeliveredSequenceId();
val = (x < y) ? -1 : ((x == y) ? 0 : 1);
}
return val;
}
}; // 添加 consumer 的时候,会触发排序
// 在 consumers 列表中,靠前的 consumer,先分发消息
private void addToConsumerList(Subscription sub) {
if (useConsumerPriority) {
consumers.add(sub);
Collections.sort(consumers, orderedCompare);
} else {
consumers.add(sub);
}
}

2、设置 message 的优先级需要在 broker 端和 producer 端配置:

2.1 在 broker 端设置 TEST.BAT 队列为 prioritizedMessages = "true"

<policyEntry queue="TEST.BAT" prioritizedMessages="true" producerFlowControl="true" memoryLimit="1mb">
<deadLetterStrategy>
<individualDeadLetterStrategy queuePrefix="TEST"/>
</deadLetterStrategy>
<pendingQueuePolicy>
<storeCursor/>
</pendingQueuePolicy>
</policyEntry>

2.2 producer 发送消息时,设置 message 的优先级

TextMessage message = session.createTextMessage(text);
producer.send(destination, message, DeliveryMode.NON_PERSISTENT, 1, 0);

设置 message 的优先级,需要调用:

void javax.jms.MessageProducer.send(Destination destination, Message message, int deliveryMode, int priority, long timeToLive)
throws JMSException

而不能这么写:

TextMessage message = session.createTextMessage(text);
message.setJMSPriority(0);

初步看是 ActiveMQ 的 bug。消息的 priority 值,从0到9。消息配置了优先级之后,消息存放在 PrioritizedPendingList 中。

// 省略部分代码
private class PrioritizedPendingListIterator implements Iterator<MessageReference> {
private int index = 0;
private int currentIndex = 0;
List<PendingNode> list = new ArrayList<PendingNode>(size()); PrioritizedPendingListIterator() {
for (int i = MAX_PRIORITY - 1; i >= 0; i--) {
// priority 值大的优先级高
OrderedPendingList orderedPendingList = lists[i];
if (!orderedPendingList.isEmpty()) {
list.addAll(orderedPendingList.getAsList());
}
}
}
}

ActiveMQ 中 consumer 的优先级,message 的优先级的更多相关文章

  1. spring+activemq中多个consumer同时处理消息时遇到的性能问题

    最近在做数据对接的工作,用到了activemq,我需要从activemq中接收消息并处理,但是我处理数据的步骤稍微复杂,渐渐的消息队列中堆的数据越来越多,就想到了我这边多开几个线程来处理消息. 可是会 ...

  2. 【转】STM32中的抢占优先级、响应优先级概念

    STM32(Cortex-M3)中有两个优先级的概念--抢占式优先级和响应优先级,有人把响应优先级称作'亚优先级'或'副优先级',每个中断源都需要被指定这两种优先级. 具有高抢占式优先级的中断可以在具 ...

  3. ActiveMQ 中的链表

    ActiveMQ 中的消息在内存中时,以链表形式保存,以 PendingList 表示,每一个消息是 PendingNode. PendingList 主要有2种实现:OrderedPendingLi ...

  4. ActiveMQ中Broker的应用与启动方式

    Broker:英语有代理的意思,在activemq中,Broker就相当于一个Activemq实例. 1. 命令行启动实例: 1.activemq start使用默认的activemq.xml启动 E ...

  5. 802.1p 优先级与内部优先级的映射关系

    缺省情况下,所有华为 S 系列交换机的 802.1P 优先级 与内部优先级的映射关系是 一 样的,如表 10-3 所示.从中可以看出,这些交换机中 802.1p 优先级与内部优先级的缺省映射关系是按等 ...

  6. stm32中断 抢占优先级 和 响应优先级 有什么区别

    与51不同,stm32的中断分类更灵活.51只是按先后顺序大小排列互相打断. stm32中多了响应优先级这一概念. stm32的中断分为 1.抢占(占先)优先级. 2.响应优先级. 1.抢占优先级.抢 ...

  7. STM32 抢占优先级和响应优先级

    一.抢占优先级和响应优先级 STM32 的中断向量具有两个属性,一个为抢占属性,另一个为响应属性,其属性编号 越小,表明它的优先级别越高. 抢占,是指打断其他中断的属性,即因为具有这个属性会出现嵌套中 ...

  8. Spring Boot 监听 Activemq 中的特定 topic ,并将数据通过 RabbitMq 发布出去

    1.Spring Boot 和 ActiveMQ .RabbitMQ 简介 最近因为公司的项目需要用到 Spring Boot , 所以自学了一下, 发现它与 Spring 相比,最大的优点就是减少了 ...

  9. Object-C中ARC forbids explicit message send of ' ' 错误

    OC中ARC forbids explicit message send of '...'错误 转自CSDN hahahacff 有所整理 ARC forbids explicit message s ...

随机推荐

  1. activity 运行流程

    图1 图2 图3 图四

  2. Vs自定nuget push菜单

    1.需要准备 nuget.exe 和 nuget-push.cmd 命名行 nuget.ext 下载地址:https://files.cnblogs.com/files/liuxiaoji/nuget ...

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

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

  4. (转)stm32硬件IIC

    cube与I2C:https://www.cnblogs.com/121792730applllo/p/5044920.html I2C官网:https://www.i2c-bus.org/stand ...

  5. 7.2内存管理-ARC

    @0-简介 1编译器会自动在适当的地方插入适当的retain.release.autorelease语句 @1-ARC的判断原则 1只要还有一个强指针变量指向对象,对象就会保持在内存中 2强指针   ...

  6. VS2008版本引入第三方dll无强签名

    sn.exe 和ilasm.exe 是系统自带程序.如果显示无此命令,可以从“我的电脑”直接搜索. 将dll文件放入目录下,用VS开发人员命令执行以下命令即可.(以Interop.Scripting. ...

  7. @Builder与@NoArgsConstructor | Lombok

    一.@Builder与@NoArgsConstructor一起使用存在的问题   @Data @Builder @NoArgsConstructor public class User { priva ...

  8. gdb 不同位置,函数调用参数显示差异

    gdb 不同位置,函数调用参数显示差异,如: copy_strings (argc=1, argv=0xffcf08, page=0xffce6c, p=131068, from_kmem=2) at ...

  9. 网络基础之 tcp/ip五层协议 socket

    1 网络通信协议(互联网协议) 1.1 互联网的本质就是一系列的网络协议 1.2 osi七层协议 1.3 tcp/ip五层模型讲解 1.3.1 物理层 1.3.2 数据链路层 1.3.3 网络层 1. ...

  10. 77. Combinations (java 求C(n,k)的组合,排除重复元素)

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 解析:同求全 ...