Spring的jms分隔符由messageId

时间:2018-01-22 08:16:17

标签: java spring jms ibm-mq spring-jms

我正在使用@JmsListener注释来接收来自队列的所有消息

@JmsListener(destination = MyQueue, selector = "MAINST = '" + instanceName + "'")
            public void receivMsg(Message message) {

但是另一个系统不能处理消息属性。它以相关id响应。当LinkedList<String>

中存在时,我如何通过correlationID过滤消息

2 个答案:

答案 0 :(得分:1)

使用instanceName的属性占位符。这是一个在同一队列中有两个监听器的例子......

@SpringBootApplication
public class So48377178Application {

    public static void main(String[] args) {
        SpringApplication.run(So48377178Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(JmsTemplate template) {
        return args -> {
            template.convertAndSend("foo", "This one's for A", m -> {
                m.setJMSCorrelationID("A");
                return m;
            });
            template.convertAndSend("foo", "This one's for B", m -> {
                m.setJMSCorrelationID("B");
                return m;
            });
        };
    }

    @JmsListener(destination = "foo", selector = "JMSCorrelationID='${a.instance}'")
    public void listenA(Message message) {
        System.out.println("A: " + message);
    }

    @JmsListener(destination = "foo", selector = "JMSCorrelationID='${b.instance}'")
    public void listenB(Message message) {
        System.out.println("B: " + message);
    }

}

a.instance=A
b.instance=B

B: ActiveMQTextMessage {commandId = 5, responseRequired = true, messageId = ID:gollum.local-56240-1516631774140-4:4:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:gollum.local-56240-1516631774140-4:4:1:1, destination = queue://foo, transactionId = null, expiration = 0, timestamp = 1516631774348, arrival = 0, brokerInTime = 1516631774348, brokerOutTime = 1516631774349, correlationId = B, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 1056, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = This one's for B}
A: ActiveMQTextMessage {commandId = 5, responseRequired = true, messageId = ID:gollum.local-56240-1516631774140-4:3:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:gollum.local-56240-1516631774140-4:3:1:1, destination = queue://foo, transactionId = null, expiration = 0, timestamp = 1516631774341, arrival = 0, brokerInTime = 1516631774341, brokerOutTime = 1516631774345, correlationId = A, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 1056, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = This one's for A}

答案 1 :(得分:0)

您可以使用该标题属性的名称来选择JMS关联ID:

selector = "JMSCorrelationID = '" + something + "'"

同时查看Message API的消息选择器部分:https://docs.oracle.com/javaee/6/api/javax/jms/Message.html