+

Search Tips | Advanced Search

Create classes for new header types using IBM MQ classes for Java

We can create Java classes for header types not supplied with IBM MQ classes for Java.

To add a Java class representing a new header type that we can use in the same way as any header class supplied with IBM MQ classes for Java, you create a class that implements the MQHeader interface. The simplest approach is to extend the com.ibm.mq.headers.impl.Header class. This example produces a fully-functional class representing the MQTM header structure. You do not have to add individual getter and setter methods for each field, but it is a useful convenience for users of the header class. The generic getValue and setValue methods that take a string for the field name will work for all fields defined in the header type. The inherited read, write and size methods will enable instances of the new header type to be read and written and will calculate the header size correctly based upon its field definition. The type definition is created just once, however many instances of this header class are created. To make the new header definition available for decoding using the MQHeaderIterator or MQHeaderList classes, you would register it using the MQHeaderRegistry. Note however that the MQTM header class is in fact already provided in this package and registered in the default registry.
import com.ibm.mq.headers.impl.Header;
import com.ibm.mq.headers.impl.HeaderField;
import com.ibm.mq.headers.CMQC;

public class MQTM extends Header {
	final static HeaderType TYPE = new HeaderType ("MQTM");
	final static HeaderField StrucId = TYPE.addMQChar ("StrucId", CMQC.MQTM_STRUC_ID);
	final static HeaderField Version = TYPE.addMQLong ("Version", CMQC.MQTM_VERSION_1);
	final static HeaderField QName = TYPE.addMQChar ("QName", CMQC.MQ_Q_NAME_LENGTH);
	final static HeaderField ProcessName = TYPE.addMQChar ("ProcessName", 
      CMQC.MQ_PROCESS_NAME_LENGTH);
	final static HeaderField TriggerData = TYPE.addMQChar ("TriggerData", 
      CMQC.MQ_TRIGGER_DATA_LENGTH);
	final static HeaderField ApplType = TYPE.addMQLong ("ApplType");
	final static HeaderField ApplId = TYPE.addMQChar ("ApplId", 256);
	final static HeaderField EnvData = TYPE.addMQChar ("EnvData", 128);
	final static HeaderField UserData = TYPE.addMQChar ("UserData", 128);

	protected MQTM (HeaderType type){
		super (type);
	}
	public String getStrucId () {
		return getStringValue (StrucId);
	}
	public int getVersion () {
		return getIntValue (Version);
	}
	public String getQName () {
		return getStringValue (QName);
	}
	public void setQName (String value) {
		setStringValue (QName, value);
	}
	// ...Add convenience getters and setters for remaining fields in the same way.
}
Parent topic: Handling IBM MQ message headers with IBM MQ classes for Java

Last updated: 2020-10-04