Exception: com.ibm.mq.MQException: MQJE001: Completion Code 2, Reason 2009

一世执手 提交于 2020-01-25 10:16:22

问题


I am trying to write my test message to MQ, but failed to do so.

Sharing the source-code and error:

source-code

import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQC;

public class MQResponseWriter {

    public static void main(String[] args) {

        try {
            int openOptions = MQC.MQOO_INQUIRE | MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
            MQEnvironment.hostname = "myserver_name";
            MQEnvironment.port = 1417;
            MQEnvironment.channel = "my_channel_name";
            //MQEnvironment.properties.put(CMQC.USER_ID_PROPERTY, "admin");
            //MQEnvironment.properties.put(CMQC.PASSWORD_PROPERTY, "passw0rd");          
            //MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
            MQQueueManager queueManager;
            queueManager = new MQQueueManager("queue_manger_name");
            MQQueue destQueue = queueManager.accessQueue("queue_name", openOptions);
            MQMessage hello_world = new MQMessage();
            hello_world.writeUTF("Blah...blah...bleah...test message no.1...!");
            MQPutMessageOptions pmo = new MQPutMessageOptions();
            destQueue.put(hello_world, pmo);
            destQueue.close();
            queueManager.disconnect();
            System.out.println("------------------------success...");            
        } catch (Exception e) {
            System.out.println("Exception: " + e);
            e.printStackTrace();
        }

    }

}

error message

MQJE001: An MQException occurred: Completion Code 2, Reason 2195
MQJE007: IO error reading message data
Error occured during API call - reason code0
MQJE001: Completion Code 2, Reason 2009
MQJE001: Completion Code 2, Reason 2018
Exception: com.ibm.mq.MQException: MQJE001: Completion Code 2, Reason 2009
com.ibm.mq.MQException: MQJE001: Completion Code 2, Reason 2009
    at com.ibm.mq.MQQueueManager.<init>(MQQueueManager.java:922)
    at com.ibm.mq.MQManagedConnectionJ11.getConnection(MQManagedConnectionJ11.java:426)
    at com.ibm.mq.MQSimpleConnectionManager.allocateConnection(MQSimpleConnectionManager.java:180)
    at com.ibm.mq.MQQueueManager.obtainBaseMQQueueManager(MQQueueManager.java:771)
    at com.ibm.mq.MQQueueManager.construct(MQQueueManager.java:705)
    at com.ibm.mq.MQQueueManager.<init>(MQQueueManager.java:434)
    at com.module.main.MQResponseWriter.main(MQResponseWriter.java:24)

What might be main cause of above error and solution if any??


回答1:


I don't know how many times I need to post it @ StackOverflow but do NOT use the MQEnvironment class, as it is NOT thread safe. Use a Hashtable instead. Here is a working example:

import java.io.IOException;
import java.util.Hashtable;

import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;

/**
 * Program Name
 *  MQTest11
 *
 * Description
 *  This java class will connect to a remote queue manager with the
 *  MQ setting stored in a HashTable and put a message to a queue.
 *
 * Sample Command Line Parameters
 *  -m MQA1 -h 127.0.0.1 -p 1414 -c TEST.CHL -q TEST.Q1 -u UserID -x Password
 *
 * @author Roger Lacroix
 */
public class MQTest11
{
   private Hashtable<String,String> params;
   private Hashtable<String,Object> mqht;
   private String qManager;
   private String outputQName;

   /**
    * The constructor
    */
   public MQTest11()
   {
      super();
      params = new Hashtable<String,String>();
      mqht = new Hashtable<String,Object>();
   }

   /**
    * Make sure the required parameters are present.
    * @return true/false
    */
   private boolean allParamsPresent()
   {
      boolean b = params.containsKey("-h") && params.containsKey("-p") &&
                  params.containsKey("-c") && params.containsKey("-m") &&
                  params.containsKey("-q") &&
                  params.containsKey("-u") && params.containsKey("-x");
      if (b)
      {
         try
         {
            Integer.parseInt((String) params.get("-p"));
         }
         catch (NumberFormatException e)
         {
            b = false;
         }
      }

      return b;
   }

   /**
    * Extract the command-line parameters and initialize the MQ HashTable.
    * @param args
    * @throws IllegalArgumentException
    */
   private void init(String[] args) throws IllegalArgumentException
   {
      int port = 1414;
      if (args.length > 0 && (args.length % 2) == 0)
      {
         for (int i = 0; i < args.length; i += 2)
         {
            params.put(args[i], args[i + 1]);
         }
      }
      else
      {
         throw new IllegalArgumentException();
      }

      if (allParamsPresent())
      {
         qManager = (String) params.get("-m");
         outputQName = (String) params.get("-q");

         try
         {
            port = Integer.parseInt((String) params.get("-p"));
         }
         catch (NumberFormatException e)
         {
            port = 1414;
         }

         mqht.put(CMQC.CHANNEL_PROPERTY, params.get("-c"));
         mqht.put(CMQC.HOST_NAME_PROPERTY, params.get("-h"));
         mqht.put(CMQC.PORT_PROPERTY, new Integer(port));
         mqht.put(CMQC.USER_ID_PROPERTY, params.get("-u"));
         mqht.put(CMQC.PASSWORD_PROPERTY, params.get("-x"));

         // I don't want to see MQ exceptions at the console.
         MQException.log = null;
      }
      else
      {
         throw new IllegalArgumentException();
      }
   }

   /**
    * Connect, open queue, write a message, close queue and disconnect.
    *
    * @throws MQException
    */
   private void testSend()
   {
      MQQueueManager qMgr = null;
      MQQueue queue = null;
      String line;
      int openOptions = CMQC.MQOO_OUTPUT + CMQC.MQOO_FAIL_IF_QUIESCING;
      MQPutMessageOptions pmo = new MQPutMessageOptions();

      try
      {
         qMgr = new MQQueueManager(qManager, mqht);
         System.out.println("MQTest11 successfully connected to "+ qManager);

         queue = qMgr.accessQueue(outputQName, openOptions);
         System.out.println("MQTest11 successfully opened "+ outputQName);

         // Define a simple MQ message, and write some text in UTF format..
         MQMessage sendmsg = new MQMessage();
         sendmsg.format = CMQC.MQFMT_STRING;
         sendmsg.feedback = CMQC.MQFB_NONE;
         sendmsg.messageType = CMQC.MQMT_DATAGRAM;

         line = "This is a test message embedded in the MQTest11 program.";

         sendmsg.messageId = CMQC.MQMI_NONE;
         sendmsg.correlationId = CMQC.MQCI_NONE;
         sendmsg.writeString(line);

         // put the message on the queue

         queue.put(sendmsg, pmo);
         System.out.println("Message Data>>>" + line);
      }
      catch (MQException e)
      {
         System.out.println("MQTest11 cc=" +e.completionCode + " : rc=" + e.reasonCode);
      }
      catch (IOException e)
      {
         System.out.println("MQTest11 IOException:" +e.getLocalizedMessage());
      }
      finally
      {
         try
         {
            queue.close();
            System.out.println("MQTest11 closed: "+ outputQName);
         }
         catch (MQException e)
         {
            System.out.println("MQTest11 cc=" +e.completionCode + " : rc=" + e.reasonCode);
         }
         try
         {
            qMgr.disconnect();
            System.out.println("MQTest11 disconnected from "+ qManager);
         }
         catch (MQException e)
         {
            System.out.println("MQTest11 cc=" +e.completionCode + " : rc=" + e.reasonCode);
         }
      }
   }

   /**
    * main line
    * @param args
    */
   public static void main(String[] args)
   {
      MQTest11 write = new MQTest11();

      try
      {
         write.init(args);
         write.testSend();
      }
      catch (IllegalArgumentException e)
      {
         System.err.println("Usage: java MQTest11 -m QueueManagerName -h host -p port -c channel -q QueueName -u UserID -x Password");
         System.exit(1);
      }

      System.exit(0);
   }
}


来源:https://stackoverflow.com/questions/49536184/exception-com-ibm-mq-mqexception-mqje001-completion-code-2-reason-2009

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!