This tip was submitted to the VS.NET Info Center by member Rick Spiewak. Please let other users know how useful it is by rating it below. Do you have a tip or code of your own you'd like to share? Submit it here.
A couple of months ago, I needed to send notification messages to a World Class system from my .NET-based application through a MQSeries queue. I used the IBM MQAX200 API that comes with the MQClient installation. Then I realized that I had to send the messages in JMS (RFH2) format, despite the fact that the message itself was just ASCII based. I developed a component to add the headers required and to format the message accordingly. Surely you will change certain values that I hard coded. However this code as it is works, I can assure you that. These are the most important parts of my code:
//Constants definitions
//The spaces are important due to the length in bytes required
const string RFH2_STRUCID_DEFAULT = "RFH ";
const int RFH2_VERSION_DEFAULT = 2;
const int RFH2_STRUCLENGTH_DEFAULT = 36;
const int RFH2_ENCODING_DEFAULT = 0x00000222;
const int RFH2_CODEDCHARSETID_DEFAULT = 1208;
const string RFH2_FORMAT_DEFAULT = "MQSTR ";
const int RFH2_FLAGS_DEFAULT = 0;
const int RFH2_NAMEVALUECCSID_DEFAULT = 1208;
const string MCD_DATA_DEFAULT
= "<mcd><Msd>jms_text</Msd></mcd>";
const string FORMAT_MESSAGE_HRF2 = "MQHRF2 ";
//Structure definition
public struct MQRFH2
{
public String StrucId;
public int Version;
public int StrucLength;
public int Encoding;
public int CodedCharSetId;
public String Format;
public int Flags;
public int NameValueCCSID;
public MQRFH2(string sStrucId, int iVersion, int iStrucLength, int
iEncoding, int iCodedCharSetId, string sFormat, int iFlags, int iNameValueCCSID)
{
StrucId = sStrucId;
Version = iVersion;
StrucLength = iStrucLength;
Encoding = iEncoding;
CodedCharSetId = iCodedCharSetId;
Format = sFormat;
Flags = iFlags;
NameValueCCSID = NameValueCCSID;
}
}
//Message generation
MQRFH2 rfh2 = new MQRFH2(RFH2_STRUCID_DEFAULT, RFH2_VERSION_DEFAULT,
RFH2_STRUCLENGTH_DEFAULT, RFH2_ENCODING_DEFAULT, RFH2_CODEDCHARSETID_DEFAULT,
RFH2_FORMAT_DEFAULT, RFH2_FLAGS_DEFAULT, RFH2_NAMEVALUECCSID_DEFAULT);
string mcd_data = MCD_DATA_DEFAULT;
string jms_data = "<jms><Dst>queue:///" + queue
+ "</Dst><Tms>" + (((long)System.DateTime.Now.Ticks) -
621355968000000000)/10000 + "</Tms><Dlv>2</Dlv></jms>";
string Data = "Message to Send. In my case I send XML messages here";
//'getMyParameters()' get the Parameters that you wish to add to the 'usr'
section of the JMS message.
string usr_data = "<usr><version>VERSION_1&tl;/version>" +
getMyParameters() + "<ReplyTo>FALSE</ReplyTo></usr>";
int mcd_len = ((mcd_data.Length - 1) / 4) * 4 + 4;
int jms_len = ((jms_data.Length - 1) / 4) * 4 + 4;
int usr_len = ((usr_data.Length - 1) / 4) * 4 + 4;
int msg_len = Data.Length;
//Padding of sections (again attention to the spaces)
mcd_data += " ";
mcd_data = mcd_data.Substring(0, mcd_len);
jms_data += " ";
jms_data = jms_data.Substring(0, jms_len);
usr_data += " ";
usr_data = usr_data.Substring(0, usr_len);
objMsg = new MQMessageClass();
objMsg.Format = FORMAT_MESSAGE_HRF2;
objMsg.Persistence = (int)MQ.MQPER_PERSISTENT;
objMsg.WriteString (rfh2.StrucId);
objMsg.WriteInt4 (rfh2.Version);
objMsg.WriteInt4 (rfh2.StrucLength + mcd_len + jms_len + usr_len + 12);
objMsg.WriteInt4 (rfh2.Encoding);
objMsg.WriteInt4 (rfh2.CodedCharSetId);
objMsg.WriteString (rfh2.Format);
objMsg.WriteInt4 (rfh2.Flags);
objMsg.WriteInt4 (rfh2.NameValueCCSID);
objMsg.WriteInt4 (mcd_len);
objMsg.WriteString (mcd_data);
objMsg.WriteInt4 (jms_len);
objMsg.WriteString (jms_data);
objMsg.WriteInt4 (usr_len);
objMsg.WriteString (usr_data);
objMsg.WriteString (this.Data);
//Connecting to the QManager and Queue
MQSessionClass objSession = new MQSessionClass();
MQQueueManager objQM = new MQQueueManager();
MQQueue objQueue = new MQQueueClass();
objQM = (MQQueueManager)objSession.AccessQueueManager("My QManager");
objQueue = (MQQueue)objQM.AccessQueue("My Queue", (int)MQ.MQOO_OUTPUT, "My
QManager", "", "");
objQueue.Open();
MQPutMessageOptionsClass objPMO = new MQPutMessageOptionsClass();
//Send the message
objQueue.Put(objMsg, objPMO);