Author |
Message
|
hoangnam214 |
Posted: Sat Jul 02, 2016 4:59 pm Post subject: Can not read MQ headers from file using Java |
|
|
![](images/avatars/1308489581577864de6cb8a.jpg) Newbie
Joined: 02 Jul 2016 Posts: 4 Location: Hanoi, Vietnam
|
Hi everyone,
I'm very new to IBM MQ.
I'm trying to use Java with libraries from com.ibm.mq.headers.* to read headers from a file.
This file, I made by using RFHUtil tool.
My code throws an exception when it reads this line "MQRFH2 rfh = new MQRFH2(in, CMQC.MQENC_NATIVE, 1208);":
Code: |
com.ibm.mq.headers.MQDataException: MQJE001: Completion Code '2', Reason '2195'.
at com.ibm.mq.headers.MQDataException.getMQDataException(MQDataException.java:331)
at com.ibm.mq.headers.MQRFH2.read(MQRFH2.java:240)
at com.ibm.mq.headers.MQRFH2.<init>(MQRFH2.java:151)
at com.ibm.sterling.MQHeadersReader.main(MQHeadersReader.java:32)
Caused by: java.lang.IndexOutOfBoundsException
at java.io.DataInputStream.readFully(Unknown Source)
at com.ibm.mq.headers.internal.DataInputWrapper.readFully(MessageWrapper.java:528)
at com.ibm.mq.headers.internal.store.ByteStore.readFrom(ByteStore.java:499)
at com.ibm.mq.headers.internal.Header.read(Header.java:1053)
at com.ibm.mq.headers.MQRFH2.read(MQRFH2.java:229)
... 2 more
|
In RFHUtil, I set "MQ Message Format" to "MQHRF2", "CCSID" to "1208", add some properties inside "usr" tab like "myfirstheader=abc123".
My Java code is as follows:
Code: |
File file = new File("00000673E7_20160613173555000920_ZRN_SHIPMENTS_3B2__FROM_SAP.xml.zip.mq");
InputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
DataInput in = new DataInputStream(new ByteArrayInputStream(bytes));
MQRFH2 rfh = new MQRFH2(in, CMQC.MQENC_NATIVE, 1208);
} catch (MQDataException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e3) {
e3.printStackTrace();
}
}
|
If I change my code as follows, the program keep iterating forever through "MQHeaderIterator it" and all "it.next()" return null:
Code: |
File file = new File("00000673E7_20160613173555000920_ZRN_SHIPMENTS_3B2__FROM_SAP.xml.zip.mq");
InputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
DataInput in = new DataInputStream(new ByteArrayInputStream(bytes));
MQHeaderIterator it = new MQHeaderIterator(in, "MQHRF2", CMQC.MQENC_NATIVE, 1208);
while (it.hasNext()) {
MQHeader header = (MQHeader) it.next();
if (header != null) {
System.out.println(header);
}
}
} catch (IOException e2) {
e2.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e3) {
e3.printStackTrace();
}
}
|
Thank you for your help.
Nam |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
hughson |
Posted: Sat Jul 02, 2016 6:28 pm Post subject: |
|
|
![](images/avatars/810794940518bb946528b3.jpg) Padawan
Joined: 09 May 2013 Posts: 1916 Location: Bay of Plenty, New Zealand
|
I don't understand, why are you reading a file? These interfaces are for reading messages from a queue. Why would they work for a file? _________________ Morag Hughson @MoragHughson
IBM MQ Technical Education Specialist
Get your IBM MQ training here!
MQGem Software |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
fjb_saper |
Posted: Sat Jul 02, 2016 9:25 pm Post subject: |
|
|
![](images/avatars/gallery/misc/2.gif) Grand High Poobah
Joined: 18 Nov 2003 Posts: 20697 Location: LI,NY
|
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
hoangnam214 |
Posted: Sun Jul 03, 2016 11:12 pm Post subject: |
|
|
![](images/avatars/1308489581577864de6cb8a.jpg) Newbie
Joined: 02 Jul 2016 Posts: 4 Location: Hanoi, Vietnam
|
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
hoangnam214 |
Posted: Sun Jul 03, 2016 11:21 pm Post subject: |
|
|
![](images/avatars/1308489581577864de6cb8a.jpg) Newbie
Joined: 02 Jul 2016 Posts: 4 Location: Hanoi, Vietnam
|
My purpose is to extract the headers and body from the input file.
I've tried HeaderList instead of HeaderIterator:
Code: |
MQHeaderList myHeaderList = new MQHeaderList(in, "MQHRF2", CMQC.MQENC_NATIVE, 1208); |
But when I ran the program, it threw an exception as follows:
Code: |
Exception in thread "main" java.lang.NullPointerException
at com.ibm.mq.headers.MQHeaderList.read(MQHeaderList.java:324)
at com.ibm.mq.headers.MQHeaderList.read(MQHeaderList.java:302)
at com.ibm.mq.headers.MQHeaderList.read(MQHeaderList.java:283)
at com.ibm.mq.headers.MQHeaderList.<init>(MQHeaderList.java:158)
at com.ibm.sterling.MQHeadersReader.main(MQHeadersReader.java:78)
|
![](images/smiles/icon_frown.gif) |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
hughson |
Posted: Mon Jul 04, 2016 3:22 am Post subject: |
|
|
![](images/avatars/810794940518bb946528b3.jpg) Padawan
Joined: 09 May 2013 Posts: 1916 Location: Bay of Plenty, New Zealand
|
These interfaces are not designed for reading from a file. Therefore it is not a surprise that they do not work. You have to write some other code, you cannot use these interfaces to do what you are trying to do. _________________ Morag Hughson @MoragHughson
IBM MQ Technical Education Specialist
Get your IBM MQ training here!
MQGem Software |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
fjb_saper |
Posted: Mon Jul 04, 2016 9:39 am Post subject: |
|
|
![](images/avatars/gallery/misc/2.gif) Grand High Poobah
Joined: 18 Nov 2003 Posts: 20697 Location: LI,NY
|
So what you're after is not the Message, or the header list per se, but the message content. Instead of downloading the message to file using RFHUtil, which is not the same as serializing an MQMessage... you should look at retrieving a message and accessing its payload...
Then you can use that payload to try your google translation API.
And don't forget to use UTF-8.
Have fun ![](images/smiles/innocent.gif) _________________ MQ & Broker admin |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
hoangnam214 |
Posted: Sun Jul 17, 2016 12:59 am Post subject: |
|
|
![](images/avatars/1308489581577864de6cb8a.jpg) Newbie
Joined: 02 Jul 2016 Posts: 4 Location: Hanoi, Vietnam
|
Thank you fjb_saper and hughson,
I have done extracting the RFH2 headers.
Following to your advises, I tried using RFHUtil tool to put the message to a queue, then wrote Java code to get that message from my queue.
I notice an important thing that I must specify MQGMO_PROPERTIES_FORCE_MQRFH2 open option when getting the message. Otherwise, all headers will be loss.
Best regards,
Nam |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
fjb_saper |
Posted: Sun Jul 17, 2016 4:10 am Post subject: |
|
|
![](images/avatars/gallery/misc/2.gif) Grand High Poobah
Joined: 18 Nov 2003 Posts: 20697 Location: LI,NY
|
Try using MQGMO_PROPERTIES_IN_HANDLE and accessing them as message properties... ![](images/smiles/innocent.gif) _________________ MQ & Broker admin |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
hughson |
Posted: Sun Jul 17, 2016 9:43 pm Post subject: |
|
|
![](images/avatars/810794940518bb946528b3.jpg) Padawan
Joined: 09 May 2013 Posts: 1916 Location: Bay of Plenty, New Zealand
|
hoangnam214 wrote: |
I notice an important thing that I must specify MQGMO_PROPERTIES_FORCE_MQRFH2 open option when getting the message. Otherwise, all headers will be loss. |
Alternatively you can use the queue attribute PROPCTL(FORCE), but it is better to do it in the application.
Cheers
Morag _________________ Morag Hughson @MoragHughson
IBM MQ Technical Education Specialist
Get your IBM MQ training here!
MQGem Software |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
|