title: Setting WebSphere MQ RFH2 header using Java
body: <p>I would like to put a message to mq with rfh2 header along with jms and usr folder. As a starting point checked this <a href="http://www.mqseries.net/phpBB2/viewtopic.php?t=21151" rel="nofollow">link</a> and came up with the code below. However after putting the message using the below code, I got a MQ error code'2142'(stands for incorrect header values). Could some one help on this. Also how do i modify the code to include jms and usr contents? </p>
  
  <pre><code>pubCommand = "&lt;psc&gt;&lt;Command&gt;Publish&lt;/Command&gt;&lt;Topic&gt;Stock&lt;/Topic&gt;" + 
  
  "&lt;QMgrName&gt;QFLEXT1&lt;/QMgrName&gt;&lt;QName&gt;QFLEXT1.A&lt;/QName&gt;&lt;/psc&gt;"; 
  
  int folderLength = pubCommand.length(); 
  MQMessage sendmsg = new MQMessage();
  //sendmsg.format = MQC.MQFMT_STRING;
  sendmsg.format = MQC.MQFMT_RF_HEADER_2; // Msg Format
  sendmsg.feedback = MQC.MQFB_NONE;
  sendmsg.messageType = MQC.MQMT_DATAGRAM;
  sendmsg.replyToQueueName = "MY.TEST.Q";
  sendmsg.replyToQueueManagerName = qManager;
  sendmsg.writeString(MQC.MQRFH_STRUC_ID); // StrucId 
  sendmsg.writeInt4(MQC.MQRFH_VERSION_2); // Version 
  sendmsg.writeInt4(MQC.MQRFH_STRUC_LENGTH_FIXED_2 + folderLength + 4); 
  sendmsg.writeInt4(MQC.MQENC_NATIVE); // Encoding 
  sendmsg.writeInt4(MQC.MQCCSI_DEFAULT); // CodedCharacterSetId 
  sendmsg.writeString(MQC.MQFMT_NONE); // Format (content) 
  sendmsg.writeInt4(MQC.MQRFH_NO_FLAGS); // Flags 
  sendmsg.writeInt4(1208); // NameValueCCSID = UTF-8 
  sendmsg.writeInt4(folderLength); 
  sendmsg.writeString(pubCommand);
  MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults, same
  // as MQPMO_DEFAULT constant
  
  String line = "test message";
  sendmsg.clearMessage();
  sendmsg.messageId = MQC.MQMI_NONE;
  sendmsg.correlationId = MQC.MQCI_NONE;
  sendmsg.writeString(line);
  
  // put the message on the queue
  queue.put(sendmsg, pmo);
  </code></pre>