JMS message selector on Object message

3.1k Views Asked by At

I working on liberty 18.0.0.2 with JavaEE 8 .
I created Custom jms object message like this :

public class MyTextMessage implements Serializable {
    private String text;
    private String destination;
    private LocalDateTime dateTime;

    public MyTextMessage(String text, String destination, LocalDateTime dateTime) {
        this.text = text;
        this.destination = destination;
        this.dateTime = dateTime;
    }

    public MyTextMessage() {
    }

    // Getter and Setter 

    @Override
    public String toString() {
        return "MyTextMessage{" +
                "text='" + text + '\'' +
                ", destination='" + destination + '\'' +
                ", dateTime=" + dateTime +
                '}';
    }
}

How can select on queue by object property ?
this is my code but not work :

JMSConsumer consumer = context.createConsumer(destination, "destination='abcdefg'");
 Message message = consumer.receiveNoWait();
 if (message != null) {
      MyTextMessage myTextMessage = message.getBody(MyTextMessage.class);
      System.out.println(myTextMessage);
 }    
1

There are 1 best solutions below

2
Justin Bertram On BEST ANSWER

You're trying to select on the property of an ObjectMessage implementation which is technically part of the body of the message. However, section 3.8.1 of the JMS 2 specification states:

Message selectors cannot reference message body values.

A message selector matches a message when the selector evaluates to true when the message’s header field and property values are substituted for their corresponding identifiers in the selector.

Therefore, you need to set a property on the message with a value you can select on (e.g. using javax.jms.Message.setStringProperty("destination", "abcdefg")).