无法从 Spring 引导应用程序连接到 IBM MQ

Unable to connect to IBM MQ from a Spring Boot application

我正在尝试从我的 Spring 引导应用程序连接到集中托管的 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",但是没有成功。

我是不是漏掉了什么?

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

A connection to a queue manager was requested, specifying TLS encryption. However, the connection mode requested is one that does not support TLS (for example, bindings connect).

查看您的代码,虽然您已经设置了客户端(又名网络)连接所需的一些属性,但您以以下行结束:-

mqQueueConnectionFactory.setTransportType(CommonConstants.WMQ_CM_BINDINGS);

这意味着您使用的是本地绑定(又名内存)类型的连接,而不是客户端连接。这意味着您的许多其他设置都将被忽略。删除该行以使之前的下一行生效,并查看您的进展情况。

mqQueueConnectionFactory.setTransportType(CommonConstants.WMQ_CM_CLIENT);

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

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

关于遗漏的mqjbnd64,不再重复信息,请参阅