I have code that looks at a message and tells me its type (text or byte). I don't understand why my message type outputs null
and not text
or byte
.
Enumeration<Message> messageEnumeration = queueBrowser.getEnumeration();
ArrayList<Message> messages = new ArrayList<>();
while (messageEnumeration.hasMoreElements()){
messages.add(messageEnumeration.nextElement());
}
System.out.println(messages.get(0).getJMSMessageID());
System.out.println(messages.get(0).getJMSType());
Console output:
ID:55350491-5216-11ee-8196-f832e49e9cfd
null
It appears that you expect the value returned from getJMSType
to represent the underlying data type of the message. However, this is not what getJMSType
returns. The "JMSType" is an arbitrary value set by the client when sending the message.
Notice that the documentation for getJMSType()
states:
Gets the message type identifier supplied by the client when the message was sent. (emphasis mine)
Furthermore, the documentation for setJMSType(String)
states:
Some JMS providers use a message repository that contains the definitions of messages sent by applications. The JMSType header field may reference a message's definition in the provider's repository.
The JMS API does not define a standard message definition repository, nor does it define a naming policy for the definitions it contains.
If you want to detect the underlying data type of the message you can use instanceof
along with the various Message
implementations, e.g.:
if (messages.get(0) instanceof TextMessage) {
System.out.println("This is a text message");
} else if (messages.get(0) instanceof BytesMessage) {
System.out.println("This is a bytes message");
} ...