我们需要在处理有效负载后使用来自请求队列的相同消息回复响应队列。我在尝试将字符串写入传入消息时遇到问题。处理消息将追加到现有有效负载。这是我正在使用的代码片段
public void readFromQueue(String reqQueue, String queueMgrName) {
int depth = 0;
MQQueueManager queueMgr = null;
MQQueue queue = null;
MQHeaderList headerList = null;
try {
queueMgr = getQueueManager(); //queue manager is initiated
queue = getQueue("get", queueMgr); // queue is intiated
MQGetMessageOptions getOptions = new MQGetMessageOptions(); // Message GET options
getOptions.options = MQConstants.MQGMO_WAIT + MQConstants.MQGMO_FAIL_IF_QUIESCING +
MQConstants.MQGMO_COMPLETE_MSG +
MQConstants.MQGMO_PROPERTIES_FORCE_MQRFH2;
byte[] b = null;
while (true) {
try {
message = new MQMessage();
queue.get(message, getOptions);
headerList = new MQHeaderList(message, true);
processMessage(headerList, message);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public void processMessage(MQHeaderList headerList, MQMessage message) {
String receivedRequest = headerList.getBody().toString();
/* ---- Message processing logic here ---- */
message.writeString(outMessage);
writeToQueue(message);
}
public void writeToQueue (MQMessage putMsg)
throws MQDataException, ConfigurationException {
MQQueueManager queueMgr = null;
MQQueue queue = null;
try {
queueMgr = getQueueManager();
queue = getQueue("put", queueMgr);
MQPutMessageOptions pmo = new MQPutMessageOptions();
queue.put(putMsg, pmo);
queueMgr.commit();
}
catch (Exception mqex) {
System.out.println(mqex);
}
}
有效负载消息示例:
{
Msg:{
MsgHeader: {
headerKey1: headerValue1,
headerKey2: headerValue2
},
MsgBody: {
bodyKey1: bodyValue1,
bodyKey2: bodyValue2
}
}
响应消息中的预期有效负载:
{
Msg:{
MsgHeader: {
headerKey1: headerValue1,
headerKey2: headerValue2
},
MsgBody: {
newBodyKey1: newBodyValue1,
newBodyKey2: newBodyValue2,
}
}
响应消息中的直接有效负载:(附加到实际有效负载而不是替换它(
{
Msg:{
MsgHeader: {
headerKey1: headerValue1,
headerKey2: headerValue2
},
MsgBody: {
bodyKey1: bodyValue1,
bodyKey2: bodyValue2
}
}
{
Msg:{
MsgHeader: {
headerKey1: headerValue1,
headerKey2: headerValue2
},
MsgBody: {
newBodyKey1: newBodyValue1,
newBodyKey2: newBodyValue2,
}
}
请帮助我解决此问题
我不明白你为什么要使用MQHeaderList类。 原始请求消息中有多少个 MQ 内部标头?
另外,您是否尝试将MQMessage对象放在JSON对象中,因为您的描述非常混乱。
听起来您的请求消息布局是:
{RFH2 header}{mcd folder}{jms folder}{usr folder}{message payload}
并且您希望回复消息是:
{RFH2 header}{mcd folder}{jms folder}{usr folder}{updated message payload}
其中 RFH2 标头、mcd 文件夹、JMS 文件夹和 usr 文件夹包含请求消息的原始值。
如果这是您要做的,请查看另一篇博客文章,看看它是否与您尝试做的事情相匹配。
更新时间:2020 年 2 月 3 日。
要从请求消息中获取消息有效负载,只需执行以下操作:
String msgStr = null;
if (CMQC.MQFMT_STRING.equals(rfh2.getFormat()))
{
msgStr = requestMsg.readStringOfByteLength(requestMsg.getDataLength());
}
else
{
byte[] b = new byte[requestMsg.getDataLength()];
requestMsg.readFully(b);
msgStr = new String(b);
}