【问题标题】:JMSWMQ0018: Failed to connect to queue manager 'SM2T' with connection mode 'Client' and host name 'null'JMSWMQ0018:无法使用连接模式“客户端”和主机名“空”连接到队列管理器“SM2T”
【发布时间】:2021-12-06 01:45:16
【问题描述】:

您好,我正在尝试使用 JMS 连接 IBM MQ。我遇到了错误

JMSWMQ0018:无法使用连接模式“客户端”和主机名“空”连接到队列管理器“SM2T”

这是我的代码:

public void putMessageToqueue() throws JMSException {
        JMSContext context = null;
        Destination destination = null;
        JMSProducer producer = null;
        JMSConsumer consumer = null;
        
        JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
        JmsConnectionFactory cf = ff.createConnectionFactory();

        // Set the properties
        cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, queueHost);
        cf.setIntProperty(WMQConstants.WMQ_PORT, port);
        cf.setStringProperty(WMQConstants.WMQ_CHANNEL, queueChannel);
        cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
        cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, queueManager);
        cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "JmsPutGet (JMS)");
        cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
        cf.setStringProperty(WMQConstants.USERID, queueUser);
        cf.setStringProperty(WMQConstants.PASSWORD, queueUser);
        cf.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, "TLS_RSA_WITH_AES_128_CBC_SHA256");
        cf.setStringProperty("javax.net.ssl.trustStore", trustStore);
        cf.setStringProperty("javax.net.ssl.keyStore", trustStore);
        cf.setStringProperty("javax.net.ssl.keyStorePassword", "");
        cf.setStringProperty("com.ibm.mq.cfg.useIBMCipherMappings", "false");
        
        
        context = cf.createContext();
        destination = context.createQueue("queue:///" + queueName);

        long uniqueNumber = System.currentTimeMillis() % 1000;
        TextMessage message = context.createTextMessage("Your lucky number today is " + uniqueNumber);
        producer = context.createProducer();
        producer.send(destination, message);
        System.out.println("Sent message:\n" + message);

                    context.close();

        //recordSuccess();
        
    }

【问题讨论】:

  • 这里的主要问题是主机名是空的
  • 你在初始化变量queueHost吗?
  • 是的,我像所有其他变量一样初始化
  • 如果您仍然无法确定问题,请更新您的问题以显示初始化变量的代码。
  • 私有静态字符串 trustStore = "C:\\ssl\\MQDEV.jks";私有字符串 cipherSuite="TSL_RSA_WITH_AES_128_CBC_SHA256";私有静态字符串 queueHost="xxx";私有静态字符串 queueManager="SM2T";私有静态整数端口=1404;私有静态字符串 queueChannel="xxx";私有静态字符串 queueName="xxx";私有静态字符串 queueUser="TMQMTS50";私有静态字符串 queuePassword="TMQMTS50";

标签: websphere ibm-mq


【解决方案1】:

您的代码存在很多问题。看起来您从几个不同的来源复制并粘贴了代码,但您遗漏了一些重要的代码。

问题:

  1. 我看不到您在哪里创建了连接
  2. 我看不到你从哪里开始连接
  3. 我看不到您在哪里创建了会话
  4. TLS/SSL 可信存储和密钥存储值不属于连接工厂,而应该是 JVM 系统环境变量。

System.setProperty("javax.net.ssl.trustStore", trustedStore);
System.setProperty("javax.net.ssl.trustStorePassword", trustedStorePasswd);

System.setProperty("javax.net.ssl.keyStore", keyStore);
System.setProperty("javax.net.ssl.keyStorePassword", keyStorePasswd);

此外,如果您使用的是受信任的存储或密钥存储,那么它必须具有有效密码。如果您使用相互身份验证,您只需要一个密钥库。

  1. IBM Cipher Mappings 值不属于连接工厂,而应该是 JVM 系统环境变量。

System.setProperty("com.ibm.mq.cfg.useIBMCipherMappings", "false");

注意:JVM 系统环境变量可以外部化到 Java 应用程序并在命令行中传递。

java -Djavax.net.ssl.trustStore=mytrustedstore -Djavax.net.ssl.trustStorePassword=mytrustedstorepwd -Dcom.ibm.mq.cfg.useIBMCipherMappings=false blah blah blah

这是一个功能齐全的 MQ/JMS 应用程序,它从头开始构建连接工厂。

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import javax.jms.*;

import com.ibm.msg.client.jms.*;
import com.ibm.msg.client.wmq.*;

/**
 * Program Name
 *  MQTestJMS51
 *
 * Description
 *  This java JMS class will connect to a remote queue manager and put a message to a queue.
 *  This code will create the Connection Factory from scratch.
 *
 * 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 MQTestJMS51
{
   private static final SimpleDateFormat  LOGGER_TIMESTAMP = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
   private Hashtable<String,String> params;

   public MQTestJMS51()
   {
      super();
      params = new Hashtable<String,String>();
   }

   /**
    * 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");
      
      return b;
   }

   /**
    * Extract the command-line parameters and initialize the MQ variables.
    * @param args
    * @throws IllegalArgumentException
    */
   private void init(String[] args) throws IllegalArgumentException
   {
      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())
      {
         throw new IllegalArgumentException();
      }
   }

   /**
    * Create a connection to the queue manager then publish a message to a queue.
    */
   private void handleIt() 
   {
      Connection conn = null;
      Session session = null;
      Destination destination = null;
      MessageProducer producer = null;
      
      try
      {
         /*
          * Set JVM system environment variables
          */
         System.setProperty("com.ibm.mq.cfg.useIBMCipherMappings", "false");
         
//         System.setProperty("javax.net.ssl.trustStore", trustedStore);
//         System.setProperty("javax.net.ssl.trustStorePassword", trustedStorePasswd);
         
         JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
         JmsConnectionFactory cf = ff.createConnectionFactory();

         // Set the properties
         cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, (String) params.get("-h"));
         try
         {
            cf.setIntProperty(WMQConstants.WMQ_PORT, Integer.parseInt((String) params.get("-p")));
         }
         catch (NumberFormatException e)
         {
            cf.setIntProperty(WMQConstants.WMQ_PORT, 1414);
         }
         
         cf.setStringProperty(WMQConstants.WMQ_CHANNEL, (String) params.get("-c"));
         cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
         cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, (String) params.get("-m"));
         cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "MQTestJMS51");
         cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
         
         cf.setStringProperty(WMQConstants.USERID, (String) params.get("-u"));
         cf.setStringProperty(WMQConstants.PASSWORD, (String) params.get("-x"));
         
//         cf.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, "TLS_RSA_WITH_AES_128_CBC_SHA256");
         
         conn = cf.createConnection((String) params.get("-u"), (String) params.get("-x"));
         logger("created connection.");
         
         // Start the connection
         conn.start();
         logger("started connection.");

         session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
         logger("created session.");

         /*
          * targetClient=0 means it is sending a JMS message
          * targetClient=1 means it is sending an MQ message (non JMS)
          */
         destination = session.createQueue("queue://" + (String) params.get("-m") + "/" + (String) params.get("-q") + "?targetClient=0");
         logger("created destination.");

         producer = session.createProducer(destination);
         logger("created producer.");
         
         sendMsg(session, producer);
      }
      catch (JMSException e)
      {
         if (e != null)
         {
            logger(e.getLocalizedMessage());
            e.printStackTrace();
            
            Exception gle = e.getLinkedException();
            if (gle != null)
               logger(gle.getLocalizedMessage());
         }
      }
      finally
      {
         try
         {
            if (producer != null)
            {
               producer.close();
               logger("closed producer.");
            }
         }
         catch (Exception e)
         {
            logger("producer.close() : " + e.getLocalizedMessage());
         }

         try
         {
            if (session != null)
            {
               session.close();
               logger("closed session.");
            }
         }
         catch (Exception e)
         {
            logger("session.close() : " + e.getLocalizedMessage());
         }

         try
         {
            if (conn != null)
            {
               conn.stop();
               logger("stopped connection.");
            }
         }
         catch (Exception e)
         {
            logger("conn.stop() : " + e.getLocalizedMessage());
         }

         try
         {
            if (conn != null)
            {
               conn.close();
               logger("closed connection.");
            }
         }
         catch (Exception e)
         {
            logger("connection.close() : " + e.getLocalizedMessage());
         }
      }
   }

   /**
    * Send a message to a queue.
    */
   private void sendMsg(Session session, MessageProducer producer)
   {
      try
      {
         long uniqueNumber = System.currentTimeMillis() % 1000;
         TextMessage msg = session.createTextMessage("Your lucky number today is " + uniqueNumber);

         producer.send(msg);
         logger("Sent message: " + msg);
      }
      catch (JMSException e)
      {
         if (e != null)
         {
            logger(e.getLocalizedMessage());
            e.printStackTrace();
            
            Exception gle = e.getLinkedException();
            if (gle != null)
               logger(gle.getLocalizedMessage());
         }
      }
   }

   /**
    * A simple logger method
    * @param data
    */
   public static void logger(String data)
   {
      String className = Thread.currentThread().getStackTrace()[2].getClassName();

      // Remove the package info.
      if ( (className != null) && (className.lastIndexOf('.') != -1) )
         className = className.substring(className.lastIndexOf('.')+1);

      System.out.println(LOGGER_TIMESTAMP.format(new Date())+" "+className+": "+Thread.currentThread().getStackTrace()[2].getMethodName()+": "+data);
   }

   /**
    * main line
    * @param args
    */
   public static void main(String[] args)
   {
      logger("starting...");
      try
      {
         MQTestJMS51 tj = new MQTestJMS51();
         tj.init(args);
         tj.handleIt();
      }
      catch (IllegalArgumentException e)
      {
         logger("Usage: java MQTestJMS51 -m QueueManagerName -h host -p port -c channel -q Queue_Name -u UserID -x Password");
      }
      catch (Exception e)
      {
         logger(e.getLocalizedMessage());
         e.printStackTrace();
      }
      logger("ending...");
   }
}

【讨论】:

    猜你喜欢
    • 2020-04-06
    • 1970-01-01
    • 2015-06-19
    • 2019-10-19
    • 1970-01-01
    • 2021-01-15
    • 2017-08-21
    • 1970-01-01
    • 2018-06-28
    相关资源
    最近更新 更多