jmsibm-mqmq

How to set Content-Type header for JMS message


We have a Java application that sends JMS message via IBM WebSphere MQ. The consumer application requires the message content type to be set to "application/json". How can I go about doing it?

I've checked through a few references and it seems I can set extra header via method "setStringProperty(headerKey, headerName)", E.g.

Message jmsMsg = session.createTextMessage(msgStr);
jmsMsg.setStringProperty("Content-Type", "application/json");

The problem then is "Content-Type" is not a valid property key since it contains the "-" character.

Is this something that can be done in code? Or is it actually configured in the queue settings?


Solution

  • The property name "Content-Type" has a '-' character. According to JMS specifications a property name can contain any character for which the Java Character.isJavaIdentifierPart method returns a true. For '-' character isJavaIdentifierPart method returns false. Hence the setStringProperty("Content-Type", "application/json") method call fails with the following exception.

    com.ibm.msg.client.jms.DetailedMessageFormatException: JMSCC0049: The property name 'Content-Type' is not a valid Java(tm) identifier.
    The supplied property name does not conform to the allowed format described in the JMS specification.
    Check the characters used in the property name and modify as necessary.
    

    If it's possible to change the receiving application, you could opt for "Content_Type" (Use an underscore) as property name instead of "Content-Type".