【问题标题】:JMS can not connect to websphere mqJMS 无法连接到 websphere mq
【发布时间】:2015-07-08 15:03:53
【问题描述】:

这是我的java类的代码


package com.ibm.point_A_point;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;

/**
 * A minimal and simple application for Point-to-point messaging.
 * 
 * Application makes use of fixed literals, any customisations will require re-compilation of this
 * source file. Application assumes that the named queue is empty prior to a run.
 * 
 * Notes:
 * 
 * API type: JMS API (v1.1, unified domain)
 * 
 * Messaging domain: Point-to-point
 * 
 * Provider type: WebSphere MQ
 * 
 * Connection mode: Client connection
 * 
 * JNDI in use: No
 * 
 */
public class SimplePTP {

  // System exit status value (assume unset value to be 1)
  private static int status = 1;

  /**
   * Main method
   * 
   * @param args
   */
  public static void main(String[] args) {

    // Variables
    Connection connection = null;
    Session session = null;
    Destination destination = null;
    MessageProducer producer = null;
    MessageConsumer consumer = null;

    try {
      // Create a connection factory
      JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
      JmsConnectionFactory cf = ff.createConnectionFactory();

      // Set the properties
      cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, "localhost");
      cf.setIntProperty(WMQConstants.WMQ_PORT, 1414);
      cf.setStringProperty(WMQConstants.WMQ_CHANNEL, "SYSTEM.DEF.SVRCONN");
      cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
      cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, "QM1");
      cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "SimplePTP (JMS)");

      // Create JMS objects
      connection = cf.createConnection();
      session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      destination = session.createQueue("queue:///Q1");
      producer = session.createProducer(destination);
      consumer = session.createConsumer(destination);

      long uniqueNumber = System.currentTimeMillis() % 1000;
      TextMessage message = session.createTextMessage("SimplePTP: Your lucky number today is "
                                                      + uniqueNumber);

      // Start the connection
      connection.start();

      // And, send the message
      producer.send(message);
      System.out.println("Sent message:\n" + message);

      Message receivedMessage = consumer.receive(15000); // in ms or 15 seconds
      System.out.println("\nReceived message:\n" + receivedMessage);

      recordSuccess();
    }
    catch (JMSException jmsex) {
      recordFailure(jmsex);
    }
    finally {
      if (producer != null) {
        try {
          producer.close();
        }
        catch (JMSException jmsex) {
          System.out.println("Producer could not be closed.");
          recordFailure(jmsex);
        }
      }
      if (consumer != null) {
        try {
          consumer.close();
        }
        catch (JMSException jmsex) {
          System.out.println("Consumer could not be closed.");
          recordFailure(jmsex);
        }
      }

      if (session != null) {
        try {
          session.close();
        }
        catch (JMSException jmsex) {
          System.out.println("Session could not be closed.");
          recordFailure(jmsex);
        }
      }

      if (connection != null) {
        try {
          connection.close();
        }
        catch (JMSException jmsex) {
          System.out.println("Connection could not be closed.");
          recordFailure(jmsex);
        }
      }
    }
    System.exit(status);
    return;
  } // end main()

  /**
   * Process a JMSException and any associated inner exceptions.
   * 
   * @param jmsex
   */
  private static void processJMSException(JMSException jmsex) {
    System.out.println(jmsex);
    Throwable innerException = jmsex.getLinkedException();
    if (innerException != null) {
      System.out.println("Inner exception(s):");
    }
    while (innerException != null) {
      System.out.println(innerException);
      innerException = innerException.getCause();
    }
    return;
  }

  /**
   * Record this run as successful.
   */
  private static void recordSuccess() {
    System.out.println("SUCCESS");
    status = 0;
    return;
  }

  /**
   * Record this run as failure.
   * 
   * @param ex
   */
  private static void recordFailure(Exception ex) {
    if (ex != null) {
      if (ex instanceof JMSException) {
        processJMSException((JMSException) ex);
      }
      else {
        System.out.println(ex);
      }
    }
    System.out.println("FAILURE");
    status = -1;
    return;
  }

}

我一直有这个错误(例外) com.ibm.msg.client.jms.DetailedJMSSecurityException:JMSWMQ2013:使用连接模式“Client”和主机名“localhost (1414)”提供给队列管理器“QM1”的身份验证安全性无效。 检查在您连接到的等待队列管理器中提供的用户名和密码是否正确。 WebSphere MQ 调用失败,完成代码为“2”(“MQCC_FAILED”);模式“2035”(“MQRC_NOT_AUTHORIZED”)。

【问题讨论】:

  • 您可能应该提供用户名和密码以连接到队列。
  • 但是如何获取密码和用户名?

标签: java jms ibm-mq


【解决方案1】:
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, "SYSTEM.DEF.SVRCONN");

您不应该使用 SYSTEM 频道。请让您的 MQAdmin 为您的应用程序创建一个。

connection = cf.createConnection();

改成:

connection = cf.createConnection("myUser","myPswd");

询问您的 MQAdmin 身份验证是针对本地操作系统还是 LDAP,然后使用适当的用户 ID 和密码。

【讨论】:

    【解决方案2】:

    您需要用户 ID 和密码(取决于 MQ 版本)才能连接到队列管理器。此用户标识通常存在于运行队列管理器的机器上。

    您需要在代码中传递用户 ID 和密码,如下所示。替换为您的用户名和密码。

    cf.setStringProperty(WMQConstants.USERID,"userid"); 
    cf.setStringProperty(WMQConstants.PASSWORD, "password"); 
    

    除此之外,用户必须被授予连接到队列管理器、放入/获取应用程序正在使用的队列的权限。

    我建议您与您的 MQ 管理员联系,以提供用户 ID/密码信息以及所需的权限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      • 2015-06-19
      • 2012-03-10
      • 1970-01-01
      相关资源
      最近更新 更多