嘿,我正在使用 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/