【问题标题】:Unable to connect to IBM MQ from a Spring Boot application无法从 Spring Boot 应用程序连接到 IBM MQ
【发布时间】:2020-06-16 13:04:14
【问题描述】:

我正在尝试从我的 Spring Boot 应用程序连接到集中托管的 IBM MQ。这是我正在尝试的配置:

application.yml

spring:
  application:
    name: test-app

server:
  port: 8088

ibm:
  mq:
    channel: xxx.SVRCONN
    queue-manager: QM.xxx
    conn-name: xxx.xx.xxx(1414)
    user: user
    password: password

test:
  mq:
    queue-name: XXX.QUEUE

Bean 配置

@Configuration
@EnableJms
public class JMSConfig {

    @Value("${ibm.mq.conn-name}")
    private String host;

    @Bean
    public MQQueueConnectionFactory mqQueueConnectionFactory() {
        MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
        mqQueueConnectionFactory.setHostName(host);
        try {
            String keystoreKey = "xxx";
            KeyStore keyStore = KeyStore.getInstance("JKS");
            keyStore.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("mq-client.jks"),
                    keystoreKey.toCharArray());

            // Create key manager
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
            keyManagerFactory.init(keyStore, keystoreKey.toCharArray());
            KeyManager[] km = keyManagerFactory.getKeyManagers();

            // Create trust manager
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
            trustManagerFactory.init(keyStore);
            TrustManager[] tm = trustManagerFactory.getTrustManagers();

            // Initialize SSLContext
            SSLContext sslContext = SSLContext.getInstance("TLSv1");
            sslContext.init(km, tm, null);
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            mqQueueConnectionFactory.setPort(1414);
            mqQueueConnectionFactory.setChannel(channel);
            mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
            mqQueueConnectionFactory.setObjectProperty(WMQConstants.WMQ_SSL_SOCKET_FACTORY, sslSocketFactory);
            // mqQueueConnectionFactory.setSSLCipherSuite("TLS_RSA_WITH_AES_256_CBC_SHA256");
            mqQueueConnectionFactory.setIntProperty(CommonConstants.WMQ_CONNECTION_MODE, CommonConstants.WMQ_CM_CLIENT);
            mqQueueConnectionFactory.setTransportType(CommonConstants.WMQ_CM_BINDINGS);


            MQEnvironment.sslSocketFactory = sslSocketFactory;
            MQEnvironment.sslCipherSuite = "TLS_RSA_WITH_AES_256_CBC_SHA256";

        } catch (Exception e) {
            e.printStackTrace();
        }
        return mqQueueConnectionFactory;
}

问题: 在此配置中,如果添加此行:

mqQueueConnectionFactory.setSSLCipherSuite("TLS_RSA_WITH_AES_256_CBC_SHA256");

然后我得到这个异常:

Could not refresh JMS Connection for destination 'RCAEC.RCA.CPO.1.QUEUE' - retrying using FixedBackOff{interval=5000, currentAttempts=1, maxAttempts=unlimited}. Cause: JMSFMQ6312: An exception occurred in the Java(tm) MQI.; nested exception is com.ibm.mq.MQException: MQJE001: Completion Code '2', Reason '2396'.

如果添加了这一行(删除前面提到的那一行):

MQEnvironment.sslCipherSuite = "TLS_RSA_WITH_AES_256_CBC_SHA256";

然后我总是得到这个异常:

Could not refresh JMS Connection for destination 'xxxx.QUEUE' - retrying using FixedBackOff{interval=5000, currentAttempts=2, maxAttempts=unlimited}. Cause: JMSFMQ6312: An exception occurred in the Java(tm) MQI.; nested exception is com.ibm.mq.jmqi.JmqiException: CC=2;RC=2495;AMQ8568: The native JNI library 'mqjbnd64' was not found. For a client installation this is expected. [3=mqjbnd64]

我也尝试设置-Djava.library.path="C:/Program Files/IBM/WebSphere MQ/java/lib64",但没有成功。

我在这里遗漏了什么吗?

【问题讨论】:

    标签: java spring-boot ibm-mq


    【解决方案1】:

    您的第一个错误显示 MQ 原因代码 2396 (MQRC_SSL_NOT_ALLOWED)。 IBM Knowledge Center 中对此的解释是:

    请求了与队列管理器的连接,并指定了 TLS 加密。但是,请求的连接模式是不支持 TLS 的(例如,绑定连接)。

    查看您的代码,虽然您已经设置了客户端(也称为网络)连接所需的许多属性,但您以以下行结束:-

    mqQueueConnectionFactory.setTransportType(CommonConstants.WMQ_CM_BINDINGS);
    

    这意味着您使用的是本地绑定(又名内存)类型的连接,而不是客户端连接。这意味着您的许多其他设置都将被忽略。删除那一行,让后面的那一行生效,看看你的进展如何。

    mqQueueConnectionFactory.setTransportType(CommonConstants.WMQ_CM_CLIENT);
    

    有关此特定 JMS 对象属性的更多详细信息,请参阅 IBM Knowledge Center: TRANSPORT

    请注意,一旦您切换到使用客户端连接而不是本地绑定连接,您将遇到各种其他障碍,例如客户端安全设置无疑会拒绝您最初的连接权限!这里有很多关于如何解决这些问题的问题。当您遇到 MQ 2035 原因代码时,请记得检查您的队列管理器错误日志。

    关于遗漏的mqjbnd64,不再重复信息,请看this answer (including comments)

    【讨论】:

      猜你喜欢
      • 2021-09-25
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多