简体   繁体   English

Apache 带有 ActiveMQ 的 Camel JMS:发送具有动态优先级的消息

[英]Apache Camel JMS with ActiveMQ: Sending Messages with Dynamic Priority

I am currently implementing a Java messaging system with Apache Camel and ActiveMQ.我目前正在使用 Apache Camel 和 ActiveMQ 实现 Java 消息传递系统。 My goal is to dynamically set the priority of a message based on a few attributes the message has.我的目标是根据消息具有的一些属性动态设置消息的优先级。

I already configured my ActiveMQ as explained here .我已经按照 此处的说明配置了我的 ActiveMQ。 Then I created the following method that sends a TextMessage :然后我创建了以下发送TextMessage的方法:

public void send(BaseMessage baseMessage, int jmsPriority) throws JsonProcessingException {
    Map<String, Object> messageHeaders = new HashMap<>();
    messageHeaders.put(MESSAGING_HEADER_JMS_PRIORITY, jmsPriority);
    messageHeaders.put(MESSAGING_HEADER_TYPE, baseMessage.getClass().getSimpleName());
    String payload = objectMapper.writeValueAsString(baseMessage);
    producerTemplate.sendBodyAndHeaders(payload, messageHeaders);
}

Sending the message perfectly works, and the dynamic type of BaseMessage is properly set to the header of each message.发送消息完美工作,并且BaseMessage的动态类型正确设置为每条消息的header。 The priority is set as well, but is ignored.优先级也被设置,但被忽略。 The order for the outcoming messages is still FIFO, as queues usually do.与队列通常一样,输出消息的顺序仍然是 FIFO。

Until now I did not achieve to set the priority of the message dynamically.直到现在我还没有实现动态设置消息的优先级。 I do not want to use Apache Camel's Resequencer since I would have to create several new queues only for "sorting".我不想使用 Apache Camel 的 Resequencer,因为我必须为“排序”创建几个新队列。 From my point of view ActiveMQ must be able to prioritize and reorder the messages itself.从我的角度来看,ActiveMQ 必须能够对消息本身进行优先级排序和重新排序。

Any tip is appreciated.任何提示表示赞赏。 Ask me for further details if required.如果需要,请向我询问更多详细信息。

By default, ActiveMQ disables message priority.默认情况下,ActiveMQ 禁用消息优先级。 This is normal.这个是正常的。 When doing distributed messaging-- sending messages across servers, prioritization does not practically work out, since the broker can only scan so many messages in the queue for messages of a higher priority before it stats to slow down all traffic for that queue.在进行分布式消息传递时——跨服务器发送消息,优先级实际上并不能解决,因为代理只能扫描队列中的这么多消息以查找具有更高优先级的消息,然后才能统计减慢该队列的所有流量。

Prioritized messages can work well when embedding a broker and using it for task dispatch-- where queue depth generally doesn't exceed the low-thousands.在嵌入代理并将其用于任务分派时,优先消息可以很好地工作——其中队列深度通常不超过低千。

Updated:更新:

Reminder-- the QOS settings in JMS must be set on the MessageProducer object, and not the message per JMS-spec.提醒 - JMS 中的 QOS 设置必须在 MessageProducer object 上设置,而不是根据 JMS 规范设置消息。

Enable Prioritized Messages 启用优先消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM