Search code examples
javaibm-mq

MQ Response only writing 1 request JAVA


i have a program to write 4 response to queue at a time but it is only writing the first response, I tried this code. But its only writing one response to the queue. Im using a ibm m queue , For getting all the request its fine . But when it comes to putting request into the queue the first response is only writing and the loop executes only one time . Dont know what is happening

MQQueueManager queueManager = null;
        MQQueue queue = null;
        try {

            String queueManagerName = "TEST";`
            String queueName = "REPONSE_QUEUE";
            String port = "1452";
            String host = "DESKTOP-32F2BVV";
            String chanel = "NEW";
            log.info("Queue Manager Name : " + queueManagerName);
            log.info("Queue Name : " + queueName);
            log.info("Port : " + port);
            log.info("Host : " + host);
            log.info("Chanel : " + chanel);

            if (null != host && host != "" && host.trim().length() > 0) {
                MQEnvironment.hostname = host;
            }
            if (null != port && port != "" && port.trim().length() > 0) {
                MQEnvironment.port = Integer.parseInt(port);
            }
            if (null != chanel && chanel != "" && chanel.trim().length() > 0) {
                MQEnvironment.channel = chanel;
            }
            queueManager = new MQQueueManager(queueManagerName.trim());
            int option = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING;
            log.info("Open Option : " + option);
            queue = queueManager.accessQueue(queueName.trim(), option, null, null, null);
            
            
            for(int i=0;i<paymentResponse.size();i++) {
                System.out.println(paymentResponse.size());
                System.out.println(paymentResponse.get(i));
                MQPutMessageOptions pmo = new MQPutMessageOptions();
                pmo.options = MQConstants.MQPMO_ASYNC_RESPONSE;
                MQMessage mqMessage = new MQMessage();
                // mqMessage.messageId = messageId.getBytes();
                mqMessage.format = MQC.MQFMT_STRING;
                mqMessage.feedback = MQC.MQFB_NONE;
                mqMessage.messageType = MQC.MQMT_DATAGRAM;
                mqMessage.characterSet = 1208;
                mqMessage.encoding = 546;
            // mqMessage.replyToQueueName = replayQueue.trim();
            // mqMessage.replyToQueueManagerName = replayQueueManager.trim();
            // mqMessage.writeUTF(paymentRequest);
            // mqMessage.writeString(paymentRequest);
            byte[] bytes = paymentResponse.get(i).getBytes("UTF-8");
            mqMessage.write(bytes, 0, bytes.length);
            queue.put(mqMessage, pmo);  
            }
        } catch (MQException e) {
            
            System.out.println(e);
        } catch (Exception e) {
            
            System.out.println(e);
        }

        return null;

    }

Solution

  • You cutoff the beginning part of the method, so I'm going to guess that paymentResponse is an ArrayList object.

    But when it comes to putting request into the queue the first response is only writing and the loop executes only one time . Dont know what is happening

    You should use the debugger that is in your IDE because without seeing the output, my first guess is that paymentResponse[1] is NULL. Hence, it is jumping out of the loop and into the catch for Exception. Therefore, only 1 message is put to the queue.

    You have several items that goes against coding best practises.

    1. Your try/catch should never be super wide. i.e encompass many lines of code.
    2. If your code connects to something then your code MUST make sure it properly disconnects from it.
    3. If your code opens something then your code MUST make sure it properly closes it.
    4. Never ever rely on a system to do work for you.
    5. You do not check variables before using them.
    6. Do not use MQEnvironment class as it is not thread safe.
    7. You set CCSID and Encoding rather than using the platform defaults. Is your data in a different format or encoding that is different from you local platform?
    8. If you are sending a response message to an original request then the correct Message Type is MQMT_REPLY.
    9. Why are you using the MQPMO Option of MQPMO_ASYNC_RESPONSE ? What is so important about your reply messages that the code cannot wait for MQ to complete?
    10. You seem to be using an assortment of MQ classes for constants. Why? i.e. MQC and MQConstants Either use CMQC class or MQConstants class. Don't use MQC as it is depreciated.
    11. You did not set a UserId and Password for the connection to the queue manager. You should always implement proper security from the beginning.

    Here's how your code should be:

    String qMgrName = "TEST";
    String queueName = "REPONSE_QUEUE";
    String port = "1452";
    String host = "DESKTOP-32F2BVV";
    String channel = "NEW";
    String userId = "myUser";
    String password = "myPwd";
    
    MQQueueManager qMgr = null;
    MQQueue queue = null;
    Hashtable<String,Object> mqht = new Hashtable<String,Object>();
    int option = CMQC.MQOO_OUTPUT + CMQC.MQOO_FAIL_IF_QUIESCING;
    MQMessage mqMessage;
    byte[] bytes = new byte[0];
    
    if (null != host && host != "" && host.trim().length() > 0)
       mqht.put(CMQC.HOST_NAME_PROPERTY, host.trim());
    
    if (null != port && port != "" && port.trim().length() > 0) {
        try {
           mqht.put(CMQC.PORT_PROPERTY, new Integer(Integer.parseInt(port.trim())));
        } catch (NumberFormatException e) {
           mqht.put(CMQC.PORT_PROPERTY, new Integer(1414));  // default value
        }
    }
    
    if (null != channel && channel != "" && channel.trim().length() > 0)
        mqht.put(CMQC.CHANNEL_PROPERTY, channel.trim());
    
    if (null != userId && userId != "" && userId.trim().length() > 0)
       mqht.put(CMQC.USER_ID_PROPERTY, userId.trim());
    
    if (null != password && password != "" && password.trim().length() > 0)
       mqht.put(CMQC.PASSWORD_PROPERTY, password.trim());
    
    try {
        qMgr = new MQQueueManager(qMgrName.trim(), mqht);
        queue = qMgr.accessQueue(queueName.trim(), option);
    
        System.out.println(paymentResponse.size());
        for(int i=0; i < paymentResponse.size(); i++) {
            System.out.println(paymentResponse.get(i));
            if (null != paymentResponse.get(i)) {
               mqMessage = new MQMessage();
               mqMessage.format = CMQC.MQFMT_STRING;
               mqMessage.messageType = CMQC.MQMT_REPLY;
               try {
                  bytes = paymentResponse.get(i).getBytes("UTF-8");
                  mqMessage.write(bytes, 0, bytes.length);
                  queue.put(mqMessage, new MQPutMessageOptions());
               } catch (MQException e) {
                  System.out.println("CC=" +e.completionCode + " : RC=" + e.reasonCode);
               } catch (IOException e) {
                  System.out.println(e);
               } catch (Exception e) {
                  System.out.println(e);
               }
            }
        }
    } catch (MQException e) {
        System.out.println("CC=" +e.completionCode + " : RC=" + e.reasonCode);
    } catch (Exception e) {
        System.out.println(e);
    }
    finally
    {
       try {
          if (queue != null) {
             queue.close();
             System.out.println("closed: "+ queueName);
          }
       } catch (MQException e) {
          System.out.println("CC=" +e.completionCode + " : RC=" + e.reasonCode);
       }
       try {
          if (qMgr != null) {
             qMgr.disconnect();
             System.out.println("disconnected from "+ qMgrName);
          }
       } catch (MQException e) {
          System.out.println("CC=" +e.completionCode + " : RC=" + e.reasonCode);
       }
    }