|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
End of file exception when using MQ classes for Java |
« View previous topic :: View next topic » |
Author |
Message
|
csongebalazs |
Posted: Thu May 15, 2014 10:58 pm Post subject: End of file exception when using MQ classes for Java |
|
|
Voyager
Joined: 30 Jan 2004 Posts: 78
|
Hi,
I am brand new in java, and maybe I asking noob questions, but need help.
I decided to make a simple java application which can connect to a queue manager, send and receive message from a queue. I started very basic way, and sent a message (from a JTextArea) using writeUTF method of an MQMessage object, and receiving the message with the readUTF method. Everything was fine.
So I decided to try read message which was sent by mqsiput.exe, and which contains MQRFH2 header with usr fields. And found many problems. First I had to realize, readUTF is not a good way to get message text.
Now, if I trying to do the following:
Code: |
MQHeaderList lista = new MQHeaderList(rcvMessage, false);
int index = lista.indexOf ("MQRFH2");
setConsolText(Integer.toString(index));
byte[] b = new byte[rcvMessage.getMessageLength()];
rcvMessage.readFully(b);
String msgText = new String(b);
|
then I got a following log and error:
Code: |
Getting a message...
0
An IOException occured whilst writing to the message buffer: java.io.EOFException: MQJE086: End of file exception ('MQMessage.readFully()'). |
But when I change order of the code, I can read the whole message (included the MQRFH2 header) into a string, but header list is empty.
Code: |
byte[] b = new byte[rcvMessage.getMessageLength()];
rcvMessage.readFully(b);
String msgText = new String(b);
MQHeaderList lista = new MQHeaderList(rcvMessage, false);
int index = lista.indexOf ("MQRFH2");
setConsolText(Integer.toString(index)); |
and
Code: |
Getting a message...
-1
The received message body displayed in message text panel. |
Pls, can someone explain, why is this working this way?
And how can I get header list and get message body text into a string at a same time? |
|
Back to top |
|
 |
csongebalazs |
Posted: Fri May 16, 2014 12:00 am Post subject: |
|
|
Voyager
Joined: 30 Jan 2004 Posts: 78
|
If I doing the following, i can read header and body :
Code: |
MQHeaderIterator hi = new MQHeaderIterator (rcvMessage);
while (hi.hasNext())
{
MQHeader header = hi.nextHeader();
setConsolText("+1");
setConsolText("Header type " + header.type() + ": " + header);
}
String msgText = hi.getBodyAsText();
|
But still can someone explain why my previous solution was wrong?  |
|
Back to top |
|
 |
fjb_saper |
Posted: Fri May 16, 2014 1:24 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
Glad you managed it. However you should also think about not using the RFH header, if all you need out of it is in its user folder.
Use the gmo/pmo option propertiesInHandle and see the information being now a property of the message... The same way do not set the RFH set the properties on the message. For those who need the RFH, the queue setting of msgprop=compat will deliver the msg either with properties or RFH2 depending on their gmo options....
Have fun  _________________ MQ & Broker admin |
|
Back to top |
|
 |
csongebalazs |
Posted: Fri May 16, 2014 1:53 am Post subject: |
|
|
Voyager
Joined: 30 Jan 2004 Posts: 78
|
Not using RFH2 header is not an option for me, I must.
Anyway, still can someone explain, why linked code not worked as way as i expected? |
|
Back to top |
|
 |
fjb_saper |
Posted: Fri May 16, 2014 7:29 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
csongebalazs wrote: |
Not using RFH2 header is not an option for me, I must.
Anyway, still can someone explain, why linked code not worked as way as i expected? |
Not quite... If you use properties in handle and msgprop=Compat the receiver will see an RFH header (if he/she, in turn, does not specify properties in handle)...
As to your question about the message and header, what is wrong with the following picture:
Code: |
byte[] b = new byte[rcvMessage.getMessageLength()];
rcvMessage.readFully(b);
String msgText = new String(b); |
Remember that at this point the payload contains both headers and data.
And I don't remember seeing that it was ok to treat the header as text...
So you could get an exception at new String(b); ...
Have fun  _________________ MQ & Broker admin |
|
Back to top |
|
 |
calanais |
Posted: Fri May 16, 2014 8:06 am Post subject: |
|
|
Apprentice
Joined: 12 Mar 2010 Posts: 32
|
Hi;
Just to pick up on a comment above; could you expand on the reasons for using the com.ibm.mq.* ("Base Java Classes") rather than the WMQ JMS provision?
For handling a simple set of text, JMS would be a simpler model.
It is sometimes required to iterate over the headers, especially when connecting to systems/applications that need to manually create headers. For a new application I would move to the JMS route. If there's a specific barrier to entry for JMS would be interested to know.
Matthew |
|
Back to top |
|
 |
csongebalazs |
Posted: Fri May 23, 2014 12:26 am Post subject: |
|
|
Voyager
Joined: 30 Jan 2004 Posts: 78
|
Thanks for replies and advices what should I do instead of what I done.
As I told I am brand new in Java, and the Using WebSphere MQ classes for Java chapter was before than the Using WebSphere MQ classes for JMS chapter, and I started to read sequentially. JMS can be better and easier way, but that is still a blind area for me. Of course I will read/watch/learn.
Anyway, luckily my excollege told me how can I make it to work with MQHeaderIterator.
After reading headers with this iterator, I should use the following code to read message body:
byte[] b = new byte[rcvMessage.getDataLength()];
rcvMessage.readFully(b);
Tested, and it is working!  |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|