Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selector in @JmsListener does not work with JmsMessagingTemplate [SPR-13770] #18344

Closed
spring-issuemaster opened this issue Dec 7, 2015 · 1 comment

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Dec 7, 2015

Tomoyuki Ikeya opened SPR-13770 and commented

selector attribute in @JmsListener is expected to filter messages with some conditions.
But it looks not to work when messages are sent with Spring Message via JmsMessagingTemplate.
For example, for filtering received messages with priority, I wrote following code.
It is expected to receive the message because the priority is larger than that set in selector, but I found it did not receive the message.

        // producer
        MessageHeaderAccessor accessor = new MessageHeaderAccessor();
        accessor.setHeader("jms_priority", 5);
        Message<Todo> todoMessage = MessageBuilder
                .withPayload(todo)
                .setHeaders(accessor).build();
        
        jmsMessagingTemplate.convertAndSend(destinationName, todoMessage);

        // consumer
        @JmsListener(destination = "queueForJmtRequest", selector = "jms_priority > 4")
        @SendTo("queueForJmtResponse")
        public Message<Todo> receive(Message<Todo> message) throws InterruptedException {
            System.out.println("Received Message : " + message);
            return message;
         }

If using JmsTemplate instead of JmsMessagingTemplate, it works correctly.
Is this bug of JmsMessagingTemplate or @JmsListener?

I pushed repro project to spring-framework-issues repos.


Reference URL: https://github.com/spring-projects/spring-framework-issues/tree/master/SPR-13770

Referenced from: commits spring-projects/spring-framework-issues@2269dae, spring-projects/spring-framework-issues@b21f741

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Dec 8, 2015

Stéphane Nicoll commented

You cannot set priority by passing a header like that, check also the javadoc for JmsHeaders.

You can enable a default priority and enable QoS to override the priority to use. That's exactly what you should do with JmsTemplate anyway:

jmsMessagingTemplate.getJmsTemplate().setPriority(5);
jmsMessagingTemplate.getJmsTemplate().setExplicitQosEnabled(true);
jmsMessagingTemplate.send(destinationName, MessageBuilder
		.withPayload(todo)
		.setHeader("randomId", randomId).build());

There were a couple of additional mistakes that made this issue harder to track:

  • You were calling convertAndSend on your Message<?> object which was wrong since you're passing a Message<?> already. You should have called send
  • The example with JmsTemplate only worked because you set a custom header called jms_priority. If you had tried to call setJMSPriority you'd have realized it wasn't working there either. The proper selector definition for the standard header is selector = "JMSPriority > 4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.