Reputation: 31
I am new to MQ programmimg. As per my requirement I am trying to put a sample XML message in a queue and expecting a response back from the response queue. I can see that the associated channel is opening for a short duration, for a few seconds and then getting closed. Please find below the code I am using to put the message in queue. Request your valuable inputs in getting this issue resolved.
Error:
Process(12908.13579) User(abc) Program(amqrmppa)
Host(hostname)
AMQ9208: Error on receive from host 10 (10.0.0.1).
EXPLANATION:
An error occurred receiving data from 10 (10.0.0.1) over TCP/IP. This may
be due to a communications failure.
Code Used:
package com.company.mq;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
public class MQConnection {
private static final String CORR_ID = "CORRELID";
String qMgrStr = "";
String hostName = "hostname";
String password ="xxxx";
String userName ="username";
String putqueueName = "putqueuename";
String getqueuename = "getqueuename ";
String channel = "channel";
String replyToQueue = "replyToQueue";
String replyToQueueManager = "";
static String content = "";
int port =10000;
MQQueue readQueue = null;
MQQueue writeQueue = null;
MQQueueManager qManager;
@SuppressWarnings("unchecked")
public void init(){
MQEnvironment.hostname =hostName;
MQEnvironment.channel = channel;
MQEnvironment.port = port;
MQEnvironment.userID = userName;
MQEnvironment.password = password;
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
try {
qManager = new MQQueueManager("");
System.out.println("qManager====>"+qManager);
}catch(Exception e){
e.printStackTrace();
}
try {
System.out.println("qManager==> hhh"+qManager);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String putAndGetMessage() throws InterruptedException, IOException{
int openOptions = MQC.MQOO_OUTPUT | MQC.MQPMO_SET_ALL_CONTEXT | MQC.MQOO_FAIL_IF_QUIESCING;
String msgString = content.toString();
System.out.println("msgString=="+msgString);
int expiryTime =60000;
MQMessage getmessage = null;
int waitInterval =4000;
try {
System.out.println("qManager Desc==>"+qManager.getDescription());
writeQueue =openWriteQueue(qManager,putqueueName);
MQMessage message = myPut(writeQueue,msgString,expiryTime,getqueuename);
// qManager.accessQueue(putqueueName, openOptions,null,null,null);
readQueue =openReadQueue(qManager,getqueuename);
getmessage =mqGet(readQueue,waitInterval,message.messageId);
/*MQMessage msg = new MQMessage();
msg.messageType = MQC.MQMT_REQUEST;
msg.format = "MQSTR";
// msg.characterSet = 500;
msg.persistence = MQC.MQPER_NOT_PERSISTENT;
msg.correlationId = CORR_ID.getBytes();
// msg.messageId = CORR_ID.getBytes();
msg.expiry= 10000;*/
/*System.out.println("before");
Thread.sleep(10000);
System.out.println("after");*/
/*MQGetMessageOptions gmo = new MQGetMessageOptions();
int openOptions1 = MQC.MQGMO_WAIT| MQC.MQGMO_CONVERT| MQC.MQGMO_FAIL_IF_QUIESCING;
System.out.println("in getqManager==>"+qManager);
readQueue = qManager.accessQueue(getqueuename, openOptions1);
System.out.println("deafaultQueue======>"+readQueue);
readQueue.get(getmessage,gmo);
System.out.println(getmessage.readInt());
String retriveMsg = getmessage.readUTF();
System.out.println("read===>"+retriveMsg);*/
} catch(MQException e){
e.printStackTrace();
}
return getmessage.readString(getmessage.getMessageLength());
}
private MQMessage mqGet(MQQueue readQueue2, int waitInterval,
byte[] corrID) throws MQException {
MQMessage responseMessage = new MQMessage();
responseMessage.correlationId =corrID;
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options =MQC.MQGMO_WAIT| MQC.MQGMO_CONVERT| MQC.MQGMO_FAIL_IF_QUIESCING;
gmo.matchOptions = MQC.MQMO_MATCH_CORREL_ID;
gmo.waitInterval = waitInterval;
// TODO Auto-generated method stub
readQueue2.get(responseMessage,gmo);
return responseMessage;
}
private MQQueue openReadQueue(MQQueueManager manager, String getqueuename2) throws MQException {
// TODO Auto-generated method stub
return openQueue(manager,getqueuename2,MQC.MQOO_INPUT_SHARED | MQC.MQOO_INQUIRE |MQC.MQOO_FAIL_IF_QUIESCING);
}
private MQMessage myPut(MQQueue writeQueue2, String msgString,
int expiryTime, String getqueuename2) {
// TODO Auto-generated method stub
MQPutMessageOptions mpo =new MQPutMessageOptions();
mpo.options = MQC.MQPMO_NEW_MSG_ID | MQC.MQMO_MATCH_CORREL_ID;
MQMessage putmessage = new MQMessage();
putmessage.format = MQC.MQFMT_STRING;
putmessage.messageFlags = MQC.MQMT_REQUEST;
putmessage.replyToQueueName =replyToQueue;
putmessage.replyToQueueManagerName = qMgrStr;
putmessage.userId="userId";
putmessage.expiry =expiryTime;
try {
putmessage.write(msgString.getBytes());
try {
writeQueue2.put(putmessage,mpo);
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return putmessage;
}
private MQQueue openWriteQueue(MQQueueManager manager, String queueName) throws MQException{
// TODO Auto-generated method stub
return openQueue(manager,queueName,MQC.MQOO_OUTPUT | MQC.MQPMO_SET_ALL_CONTEXT | MQC.MQOO_FAIL_IF_QUIESCING);
}
private MQQueue openQueue(MQQueueManager manager, String queueName, int options) throws MQException{
// TODO Auto-generated method stub
return manager.accessQueue(queueName, options,null,null,null);
}
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
MQConnection conn = new MQConnection();
DataInputStream dis = new DataInputStream ( new FileInputStream ("c://request//Request.xml"));
byte[] datainBytes = new byte[dis.available()];
dis.readFully(datainBytes);
dis.close();
content = new String(datainBytes, 0, datainBytes.length);
//System.out.println("content===>"+content);
conn.init();
System.out.println("connected");
conn.putAndGetMessage();
}
}
Upvotes: 2
Views: 988
Reputation: 31832
The application appears to be a class which encapsulates the MQ I/O. On instantiation, the init
routine constructs the class and attempts to connect to the queue manager. There are a few problems with the code that I can see. Also, and I suspect I know why the code fails but there is no diagnostic information provided in the question so I can only guess. I'll describe the problems first, then which diagnostic info I'd normally expect.
Code issues
The code provides a user ID and password. Unless this is a v8.0 client talking to a v8.0 queue manager, or a client that uses an exit to talk to MQ over a TLS channel, the password is not being checked and not needed. Even worse, if the channel is not TLS and encrypted, the ID and password are being transmitted in the clear over the network. Take the password out. Use the ID only if the ID presented by the app needs to be overridden in the connection request.
The code is not printing the linked exception. A JMS exception is a multi-level data structure in which the top level is the exception as JMS understands it and the lower level exception is the one the transport provider understands. Many transport providers, especially those written in pure Java, use only the top level of the data structure and do not supply a linked exception. However, as a developer you must not assume that this will never be the case.
There is no valid reason for a JMS developer to ever fail to print the linked exception.
When I was managing the MQ Admin team at a large bank, my policy was that nothing went to Production if it did not follow these rules:
The code opens the queue with the MQC.MQPMO_SET_ALL_CONTEXT
option. This level of privilege is not normally granted to non-admin users. The code explicitly specifies an ID which suggests it is not running with admin privileges. For these reasons, I suspect that you are getting a 2035 error, which of course you do not see because the code fails to print the linked exception.
Missing diagnostics
MQ behaves differently from version to version. Over the years the Java/JMS classes have been repackaged, refactored, and upgraded to JMS 1.1, then to JMS 2.0. Diagnostic questions, whether posted here or sent to your MQ Admin tean for a Production outage, should indicate the versions of MQ client and MQ server in use. The output of dspmqver -a
on the client node and the server node are always helpful.
As mentioned previously, the linked exception is not being printed. Without it the stack trace is probably worthless, so for once I'm not that disappointed no stack trace was provided. However, update the code to print the linked exception and include a stack trace in the future.
Both the client and the server produce error logs. In addition to the queue manager's error log, there are global error logs on the server for things that happen before the queue manager has been identified or for commands which operate on the global MQ configuration. No error log information from any of these sources was provided in this case. Why not?
The server also produces event messages. These are normally consumed by a monitoring agent but may be enabled by administrators and reviewed to determine the problem. In the absence of error log messages, event messages can be very helpful. I would not expect them in this case but mention it here for completeness.
FDC files are produced by the queue manager and stored in the same directory as the global error logs. These provide much more detailed information than the error log entries but are not always produced. Usually a problem diagnosis request would mention either that there were no FDC files produced, or else that they were and provide the header block from them.
Resources
Please see:
Stick with it and good luck!
Full disclosure: I am one of the founders, and my MQ consulting firm is a sponsor, of the MQ Tech conference. However, it had almost as many MQ sessions on it's first day last year as Interconnect had all week so I do not see a conflict of interest in recommending it.
Upvotes: 2