readStringOfCharLength(int length) throws EOF exception

huangapple go评论83阅读模式
英文:

readStringOfCharLength(int length) throws EOF exception

问题

我已经编写了以下代码以在IBM MQ中读取多条消息:

com.ibm.mq.MQQueue defaultLocalQueue;
MQQueueManager qManager=null;

MQEnvironment.hostname = "reese.int.westgroup.com";
MQEnvironment.channel = "CLIENTCONNECTION";
MQEnvironment.port = 1414;
String qMngrStr = "";
qManager = new MQQueueManager(qMngrStr); 
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE ; 
String queueName="COMSERV.SRCHEXT.EVENTS.PUBLISH.QA.Q01";
System.out.println("accessing::"+queueName);
defaultLocalQueue = qManager.accessQueue(queueName, openOptions);
//set transport properties.
System.out.println("set MQ props");
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
System.out.println("new Queuemanager");
ArrayList<MQMessage> msg = new ArrayList<MQMessage>();
//MQMessage putMessage = new MQMessage();
//String msg = "hello";
// putMessage.writeUTF(msg);

//specify the message options...
// MQPutMessageOptions pmo = new MQPutMessageOptions(); 
// accept 
// put the message on the queue
//defaultLocalQueue.put(putMessage, pmo);
boolean getMore = true;
while(getMore)
{  
    MQMessage getMessages= new MQMessage();
    MQGetMessageOptions gmo = new MQGetMessageOptions();
    gmo.options=MQC.MQGMO_WAIT;
    gmo.waitInterval=1000000;
    //gmo.wait(100000);

    System.out.println("get messages::"+gmo.toString());
    defaultLocalQueue.get(getMessages, gmo);

    msg.add(getMessages);
    int length=getMessages.getMessageLength();
    System.out.println("length::"+length);
    String retrievedMsg = getMessages.readStringOfCharLength(length);
    System.out.println("Message got from MQ: "+retrievedMsg);

}
if(defaultLocalQueue.getCurrentDepth()==0)
{
    getMore=false;
}
for (MQMessage message : msg)
{
    System.out.println("message::"+message);

}
}

以下行会引发EOF异常,即使消息在MQ中存在且消息的长度也打印为3760。

String retrievedMsg = getMessages.readStringOfCharLength(length);

以下是异常的完整堆栈跟踪:

length::3760

java.io.EOFException: MQJE086: End of file exception ('MQMessage.readChar()').
	at com.ibm.mq.MQMessage.readChar(MQMessage.java:908)
	at com.ibm.mq.MQMessage.readStringOfCharLength(MQMessage.java:1232)
	at com.tlr.searchextract.messages.MessageTrigger_ABean.init(MessageTrigger_ABean.java:109)
	at com.tlr.searchextract.servlet.SEControllerServlet.insertRequestTemplate(SEControllerServlet.java:1385)
	at com.tlr.searchextract.servlet.SEControllerServlet.performTask(SEControllerServlet.java:2055)
	at com.tlr.searchextract.servlet.SEControllerServlet.doPost(SEControllerServlet.java:117)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:203)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

请帮我解决这个问题。

英文:

I have written below code to read more than one messages in IBM MQ

com.ibm.mq.MQQueue defaultLocalQueue;
MQQueueManager qManager=null;
MQEnvironment.hostname = &quot;reese.int.westgroup.com&quot;;
MQEnvironment.channel = &quot;CLIENTCONNECTION&quot;;
MQEnvironment.port = 1414;
String qMngrStr = &quot;&quot;;
qManager = new MQQueueManager(qMngrStr); 
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE ; 
String queueName=&quot;COMSERV.SRCHEXT.EVENTS.PUBLISH.QA.Q01&quot;;
System.out.println(&quot;accessing::&quot;+queueName);
defaultLocalQueue = qManager.accessQueue(queueName, openOptions);
//set transport properties.
System.out.println(&quot;set MQ props&quot;);
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
System.out.println(&quot;new Queuemanager&quot;);
ArrayList&lt;MQMessage&gt; msg = new ArrayList&lt;MQMessage&gt;();
//MQMessage putMessage = new MQMessage();
//String msg = &quot;hello&quot;;
// putMessage.writeUTF(msg);
//specify the message options...
// MQPutMessageOptions pmo = new MQPutMessageOptions(); 
// accept 
// put the message on the queue
//defaultLocalQueue.put(putMessage, pmo);
boolean getMore = true;
while(getMore)
{  
MQMessage getMessages= new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options=MQC.MQGMO_WAIT;
gmo.waitInterval=1000000;
//gmo.wait(100000);
System.out.println(&quot;get messages::&quot;+gmo.toString());
defaultLocalQueue.get(getMessages, gmo);
msg.add(getMessages);
int length=getMessages.getMessageLength();
System.out.println(&quot;length::&quot;+length);
String retrievedMsg = getMessages.readStringOfCharLength(length);
System.out.println(&quot;Message got from MQ: &quot;+retrievedMsg);
}
if(defaultLocalQueue.getCurrentDepth()==0)
{
getMore=false;
}
for (MQMessage message : msg)
{
System.out.println(&quot;message::&quot;+message);
}
}

The line

String retrievedMsg = getMessages.readStringOfCharLength(length); 

is causing an EOF exception even though the messages are there in MQ and length of the message is also printing 3760.

Below is the complete stack trace of the exception.

length::3760
java.io.EOFException: MQJE086: End of file exception (&#39;MQMessage.readChar()&#39;).
at com.ibm.mq.MQMessage.readChar(MQMessage.java:908)
at com.ibm.mq.MQMessage.readStringOfCharLength(MQMessage.java:1232)
at com.tlr.searchextract.messages.MessageTrigger_ABean.init(MessageTrigger_ABean.java:109)
at com.tlr.searchextract.servlet.SEControllerServlet.insertRequestTemplate(SEControllerServlet.java:1385)
at com.tlr.searchextract.servlet.SEControllerServlet.performTask(SEControllerServlet.java:2055)
at com.tlr.searchextract.servlet.SEControllerServlet.doPost(SEControllerServlet.java:117)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:203)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

Please help me with this.

答案1

得分: 2

来自MQ知识中心中的MQMessage:

> getMessageLength() 获取此消息中消息数据的字节数。

因此,一个方法涉及字节,然后您使用readStringOfCharLength方法涉及字符。

注意:字节长度 != 字符长度

您应该使用readStringOfByteLength方法。

即,

int length = getMessages.getMessageLength();
String retrievedMsg = getMessages.readStringOfByteLength(length);

更新:2020年8月19日。

如果MQ无法将消息转换为字符串,则可以将其作为字节数组获取,并让JVM尝试将其转换为字符串。

int length = getMessages.getMessageLength();
byte[] bMsg = new byte[length];
getMessages.readFully(bMsg);
String retrievedMsg = new String(bMsg);
英文:

From the MQMessage in the MQ KnowLedge Center:

> getMessageLength() Gets the number of bytes of message data in this
> message.

So, one method deals in bytes then you use readStringOfCharLength method which deals in characters.

FYI: byte length != character length

You should be using readStringOfByteLength method.

i.e.

int length=getMessages.getMessageLength();
String retrievedMsg = getMessages.readStringOfByteLength(length);

Updated: August 19, 2020.

If MQ cannot convert the message to a string then you can get it as a byte array and have the JVM attempt to convert it to a string.

int length=getMessages.getMessageLength();
byte[] bMsg = new byte[length];
getMessages.readFully(bMsg);
String retrievedMsg = new String(bMsg);

答案2

得分: 0

这在使用IBM® MQ Java类打印消息内容中有详细记录:

import com.ibm.mq.MQMessage;
import com.ibm.mq.headers.MQHeaderList;
...
MQMessage message = ... // 从队列接收到的消息。

System.out.println(new MQHeaderList(message, true));
英文:

This is documented in Printing the content of a message using IBM® MQ classes for Java:

import com.ibm.mq.MQMessage;
import com.ibm.mq.headers.MQHeaderList;
...
MQMessage message = ... // Message received from a queue.
System.out.println (new MQHeaderList (message, true));

huangapple
  • 本文由 发表于 2020年8月18日 21:55:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63470191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定