jms - 如何在Spring集成中设置每个出站jms消息的优先级?

标签 jms spring-integration ibm-mq

嘿,我正在使用 Spring Integration 的 jms:outbound-channel-adapter,并且需要在将消息推送到消息系统之前设置消息的优先级。

现在,在普通的 JMS 中,我有两种方法可以做到这一点。

MessageProducer 上设置优先级:

this.producer.setPriority(i);

或者在发送方法本身上:

channel.send(message, DeliveryMode.PERSISTENT, 5, 1000);

这些选项对我来说都不再可用,因为 channel 适配器将我从这些细节中抽象出来。

设置消息本身的优先级仅在 Spring 集成的内存 channel 中起作用,并且在我将其放入实际队列中后立即失效。事实证明,设置消息优先级根本不是一个选项:JMS message priority not working on Message

channel 适配器上有一个属性,我可以在其中设置优先级,但这是静态的。

    <jms:outbound-channel-adapter id="101Out" 
                              channel="101MessageChannel"
                              connection-factory="101Factory"
                              destination="QUEUE_NAME"
                              priority="1" />

我能做的最大事情是从属性文件中读取它。 (或者我是这么认为。我不确定)。我可以使用 destination-expression 属性来检查传入消息并将其动态路由到不同的目的地,但没有 priority-express 对应部分可供我执行相同操作优先级。

我有一种解决方法,但这不是一个很好的方法:

    <jms:outbound-channel-adapter id="101HighPriorityOut" 
                              channel="101HighPriorityChannel"
                              connection-factory="101Factory"
                              destination-expression="headers.QUEUE_NAME"
                              priority="1"
                              explicit-qos-enabled="true" />

    <jms:outbound-channel-adapter id="101LowPriorityOut" 
                              channel="101LowPriorityChannel"
                              connection-factory="101Factory"
                              destination-expression="headers.QUEUE_NAME"
                              priority="0"
                              explicit-qos-enabled="true" />

一旦确定了优先级,我就将消息路由到适当的出站适配器。但如果优先级的数量增加,我就会遇到麻烦。即使没有,拥有两个出站适配器而不是一个,因为我无法动态分配优先级,我认为这有点笨拙。

感谢帮助:-)

哦,我正在使用 Websphere MQ 作为我的消息代理。我不知道这是否与消息代理有任何关系。

最佳答案

只需在消息中设置优先级 header ...

<int:header-enricher ...>
    <int:priority value="2" />
</int:heaer-enricher>

适配器配置中的优先级是默认值,当没有优先级 header 时使用该优先级(您可以使用属性占位符从属性文件中设置它)。

或者,使用表达式...

<int:header-enricher ...>
    <int:priority expression="payload.foo == 'bar' ? 1 : 2" />
</int:heaer-enricher>

<int:header-enricher ...>
    <int:priority expression="payload.priority" />
</int:heaer-enricher>

<int:header-enricher ...>
    <int:priority expression="@someBean.calculatePriority(payload)" />
</int:heaer-enricher>

关于jms - 如何在Spring集成中设置每个出站jms消息的优先级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33211606/

相关文章:

java - 是否可以从 Weblogic JMS 文件存储中抢救消息?

spring-integration - lambda 和匿名变压器的不同行为

java - QueueManager 未连接,出现异常 : MQJE001:

activemq - 我们可以纯粹使用非 Websphere MQSeries 软件与远程 Websphere MQSeries 通话吗

java - 将 Zip 内容读入字符串时出现 "ZipException - unexpected end of file when reading short buff"

java - Camel 路由发布者未发布到 JMS 出站队列失败,并出现 java.util.concurrent.RejectedExecutionException

jms - 从 Camel 连接到 WebSphere MQ 服务器时出现 "Channel not defined remotely"问题

java - Glassfish 3.1 中未找到通用资源适配器 genericra.rar

spring-integration - JMS 出站网关请求目的地 - 成功后处理

java - 如何处理 Spring 集成流的事务(Java DSL)