例外:com.ibm.mq.MQException:MQJE001:完成代码2,原因2009

时间:2018-03-28 13:42:58

标签: ibm-mq

我正在尝试将测试消息写入MQ,但未能这样做。

分享源代码和错误:

源代码

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)

错误消息

string1 = aabcdef
string2 = abcghi
the result should be final = adefghi

可能是上述错误的主要原因和解决方案(如果有的话)

1 个答案:

答案 0 :(得分:1)

我不知道我需要多少次发布它@ StackOverflow但不使用MQEnvironment类,因为它不是线程安全的。改为使用Hashtable。这是一个有效的例子:

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);
   }
}