当前位置: 首页 > 知识库问答 >
问题:

JMS无法连接到websphere mq

侯英达
2023-03-14
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')。

共有1个答案

羊越
2023-03-14

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

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

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

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

 类似资料:
  • 在网上进行了几个小时的反复试验和研究后,问题似乎是由于授权错误而无法连接,但我可以使用Java代码(使用相同的库MQQueueConnectionFactory)连接,也可以使用QueueZee与完全相同的库连接,获得所有队列的列表并浏览它们,这样我就知道用户授权问题不应该是问题所在。 我运行的是Hermes JMS1.14,并且尝试使用Java1.6.0_33和1.7.0_5。Websphere

  • 我正在尝试使用IntelliJ在WildFly 19中创建一个简单的JMS ActiveMQ连接。我遵循了安装指南,但遇到了连接错误。 我将Wildfly作为本地服务器运行,处于独立模式。我已经更新了IntelliJ中的启动脚本环境变量,以指向standalone-full.xml(显然我需要使用,以便使用JMS?) 我已经更新了XML文件,以便在代码中添加要连接的JMS队列: EAR正在部署,J

  • 我有Spring启动应用程序,它通过注释从组件类中侦听 IBM MQ 队列。MQ 属性(主机名、通道、端口等)是从 yaml 文件设置的。 MQ 依赖项在分级构建中添加,如下所示: 这可以正常工作,只要我使用Tomcat容器在本地运行应用程序即可侦听消息。但是,如果我将其打包为 EAR 并部署到 Wesbphere8.5 服务器,它将引发以下异常,并且侦听器未从队列中读取消息。我确认所有运行时依赖

  • 我正在尝试连接到EAP 7.1上的ActiveMq Artemis,它具有传统配置(远程:4447)。我可以使用JMSToolBox通过端口5445连接,但是当我想从我的Spring Boot应用程序使用remote://xxx:4447访问服务器时,我得到了这个警告 对于目标“java:/队列/参与方”,JMS 消息侦听器调用程序的安装失败 - 尝试恢复。原因:无法将org.apache.act

  • 问题内容: 我正在尝试使用Ruby on Rails运行Selenium的示例脚本。我必须使用代理运行它。这是我的代码: 我收到以下错误: 有人能帮我吗…?我已经尝试了好几个小时,却找不到问题…真的不知道该怎么办。 环境: Ubuntu 16.04 LTS,Firefox 45.0,rbenv 2.3.1 另一个问题:有人知道Selenium + Ruby on Rails的示例吗?我找不到真正好

  • 我正在尝试连接到MySQL服务器,但出现无法处理的错误。 java.sql.SQLNonTransientConnectionException:无法创建到数据库服务器的连接。尝试重新连接3次。放弃。com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)com.mysql.cj.jdbc.excepti