Reading and writing the message descriptor from an IBM MQ classes for JMS application

You control the ability to access the message descriptor (MQMD) by setting properties on a Destination and a Message.

Some IBM MQ applications require specific values to be set in the MQMD of messages sent to them. IBM MQ classes for JMS provides message attributes that allow JMS applications to set MQMD fields and so enable JMS applications to "drive" IBM MQ applications.

We must set the Destination object property WMQ_MQMD_WRITE_ENABLED to true for the setting of MQMD properties to have any effect. We can then use the property setting methods of the message (for example setStringProperty) to assign values to the MQMD fields. All MQMD fields are exposed except StrucId and Version; BackoutCount can be read but not written to.

This example results in a message being put to a queue or topic with MQMD.UserIdentifier set to "JoeBloggs".
  // Create a ConnectionFactory, connection, session, producer, message
  // ...

  // Create a destination
  // ...

  // Enable MQMD write
  dest.setBooleanProperty(WMQConstants.WMQ_MQMD_WRITE_ENABLED, true);
  
  // Optionally, set a message context if applicable for this MD field
  dest.setIntProperty(WMQConstants.WMQ_MQMD_MESSAGE_CONTEXT, 
    WMQConstants.WMQ_MDCTX_SET_IDENTITY_CONTEXT);

  // On the message, set property to provide custom UserId
  msg.setStringProperty("JMS_IBM_MQMD_UserIdentifier", "JoeBloggs");

  // Send the message
  // ...
It is necessary to set WMQ_MQMD_MESSAGE_CONTEXT before setting JMS_IBM_MQMD_UserIdentifier. For more information about the use of WMQ_MQMD_MESSAGE_CONTEXT, see JMS message object properties.

Similarly, we can extract the contents of the MQMD fields by setting WMQ_MQMD_READ_ENABLED to true before receiving a message and then using the get methods of the message, such as getStringProperty. Any properties received are read-only.

This example results in the value field holding the value of the MQMD.ApplIdentityData field of a message got from a queue or a topic.
  // Create a ConnectionFactory, connection, session, consumer
  // ...

  // Create a destination
  // ...

  // Enable MQMD read
  dest.setBooleanProperty(WMQConstants.WMQ_MQMD_READ_ENABLED, true);

  // Receive a message
  // ...

  // Get MQMD field value using a property
  String value = rcvMsg.getStringProperty("JMS_IBM_MQMD_ApplIdentityData");

Parent topic: Accessing IBM MQ features from an IBM MQ classes for JMS application