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

后端 未结 1 1271
梦毁少年i
梦毁少年i 2021-01-28 16:37

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.m         


        
相关标签:
1条回答
  • 2021-01-28 17:35

    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);
       }
    }
    
    0 讨论(0)
提交回复
热议问题