Spring JMS 配置(Websphere MQ - SSL、Tomcat、JNDI、非 IBM JRE)

Spring Configuration for JMS (Websphere MQ - SSL, Tomcat, JNDI, Non IBM JRE)

背景: 我有一个相对较旧的应用程序,它使用 Websphere MQ 进行消息传递。它 运行 在 WAS(Websphere 应用程序服务器)上运行并使用 MDB(消息驱动的 Bean)。我成功地能够使用 Spring Integration - JMS 替换所有 MDB。我的下一步是尝试查看是否可以将它从 WAS 移植出去,以便它可以 运行 在具有非 IBM JRE 的任何其他 servlet 容器上(我正在尝试:apache tomcat)。请注意,使用 SSL 保护通道是一项要求。我更喜欢使用 JNDI。

最终目标: 将我的应用程序与应用程序服务器 (WAS) 和消息传递 (MQ) 等其他基础设施分离。但是将它从 WAS 中取出到 tomcat 是第一步。接下来是用更具可扩展性的东西更新我的消息传递基础设施的任务。这使我可以更新我的应用程序所依赖的基础架构的各个组件,一次更新一件事(应用程序服务器、消息传递层、数据存储),而不会过多地中断我的应用程序。

问题: 现在,我的挑战是在 tomcat 上定义可以访问 Websphere MQ 的 JNDI 资源。我使用我在 context.xml 文件中定义的非 SSL 通道在这方面取得了一些进展,如下所示:

<Resource
   name="jms/qcf_sandbox"
   auth="Container"
   type="com.ibm.mq.jms.MQQueueConnectionFactory"
   factory="com.ibm.mq.jms.MQQueueConnectionFactoryFactory"
   description="JMS Queue Connection Factory for sending messages"
   HOST="localhost"
   PORT="1414"
   CHAN="CHANNEL_SANDBOX"
   TRAN="1"
   QMGR="QM_SANDBOX"/>
<Resource
   name="jms/SandboxQ"
   auth="Container"
   type="com.ibm.mq.jms.MQQueue"
   factory="com.ibm.mq.jms.MQQueueFactory"
   description="JMS Queue"
   QU="SANDBOX_Q"/>

我的下一步是让它与 SSL 通道一起使用。我了解涉及设置密钥库(kdb 文件和证书生成和交换)、在 QM 上配置 SSL 通道等的部分。我已经完成了所有这些工作。如何让 tomcat 使用我的密钥库、密码套件等?指针或工作示例会很棒!

注意:我目前正在使用 Spring Integration 4.2、Websphere MQ v8、Tomcat v9。

我必须补充一点,我确实先尝试了没有 JNDI 的所有方法。所以这是我的 spring 没有 JNDI 的 jms 非 ssl 配置,它有效:

<bean id="mq-jms-cf-sandbox"
   class="org.springframework.jms.connection.SingleConnectionFactory">
    <property name="targetConnectionFactory">
    <ref bean="mqQueueConnectionFactory" />
    </property>
</bean>
<bean id="mqQueueConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
   <property name="hostName" value="localhost" />
   <property name="port" value="1414" />
   <property name="queueManager" value="QM_SANDBOX" />
   <property name="transportType" value="1" />
   <property name="channel" value="CHANNEL_SANDBOX" />
 </bean>
 <bean id="jms-destination-sandbox" class="com.ibm.mq.jms.MQQueue">
   <constructor-arg value="SANDBOX_Q" />
    <property name="baseQueueManagerName">
    <value>QM_SANDBOX</value>
    </property>
    <property name="baseQueueName">
    <value>SANDBOX_Q</value>
    </property>
</bean> 

我想我终于想出了如何实现这一目标...这里是对步骤的简要说明。如果您需要更多详细信息,请告诉我。

先决条件: 已安装 Websphere MQ 服务器(至少 v 8.0.0.2) 配置 QM、SSL 和非 SSL 通道,创建 Q 以及您需要的所有好东西。 不用说,您需要 Websphere MQ jar。请注意任何许可限制。

第 1 步:获得不使用 SSL 和 JNDI 的直接连接。您将需要使用这些 bean 来配置基于 spring 的 JMS 侦听器和 JMS 模板等

<bean id="mq-jms-cf-sandbox"
   class="org.springframework.jms.connection.SingleConnectionFactory">
    <property name="targetConnectionFactory">
    <ref bean="mqQueueConnectionFactory" />
    </property>
</bean>
<bean id="mqQueueConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
   <property name="hostName" value="localhost" />
   <property name="port" value="1414" />
   <property name="queueManager" value="QM_SANDBOX" />
   <property name="transportType" value="1" />
   <property name="channel" value="NON_SSL_CHANNEL" />
 </bean>
 <bean id="jms-destination-sandbox" class="com.ibm.mq.jms.MQQueue">
   <constructor-arg value="SANDBOX_Q" />
    <property name="baseQueueManagerName">
    <value>QM_SANDBOX</value>
    </property>
    <property name="baseQueueName">
    <value>SANDBOX_Q</value>
    </property>
</bean>

第 2 步:获得使用 SSL 的直接连接,没有 JNDI。我发现设置这个有点棘手。

2a。由于我使用的是非 IBM JRE,因此我必须确保需要根据此处指定的映射配置密码规范和密码套件: http://www-01.ibm.com/support/docview.wss?uid=swg1IV66840

这显然意味着我们至少必须将 Websphere MQ 升级到 8.0.0.2。在我的例子中,我在 SSL 通道上使用了 ECDHE_RSA_AES_256_GCM_SHA384,并在应用程序中配置了 jms bean 以使用 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,就像这样:

<bean id="mq-jms-cf-sandbox"
    class="org.springframework.jms.connection.SingleConnectionFactory">
    <property name="targetConnectionFactory">
        <ref bean="mqQueueConnectionFactory" />
    </property>
</bean>
<bean id="mqQueueConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
  <property name="hostName" value="localhost" />
  <property name="port" value="1414" />
  <property name="queueManager" value="QM_SANDBOX" />
  <property name="transportType" value="1" />
  <property name="channel" value="SSL_CHANNEL" />
  <property name="SSLCipherSuite" value="TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"/>
</bean>
<bean id="jms-destination-sandbox" class="com.ibm.mq.jms.MQQueue">
<constructor-arg value="SANDBOX_Q" />
    <property name="baseQueueManagerName">
       <value>QM_SANDBOX</value>
    </property>
    <property name="baseQueueName">
       <value>SANDBOX_Q</value>
    </property>
</bean>

2b。创建证书、密钥库 (kdbs)、交换证书等。有很多方法可以做到这一点。但请注意,您需要存储密码,队列管理器的密钥标签必须是 'ibmwebspheremqqmgr' – 全部小写,没有空格,(不带引号),密钥label 必须类似于 'ibmwebspheremquserid' – 全部小写,无空格(不带引号),其中 userid 是运行 tomcat 的用户标识。如果您需要更多关于我如何使用自签名证书的详细信息,请告诉我。

2c。现在您必须获得 tomcat 运行的 JVM,才能读取您的密钥库。有很多方法,但我是这样做的: 在tomcatbin文件夹下新建一个setenv.bat文件,内容如下(调试SSL可选)

set JAVA_OPTS="-Djavax.net.ssl.trustStore=C:\path-to-keystore\key.jks" "-Djavax.net.ssl.trustStorePassword=topsecret" "-Djavax.net.ssl.keyStore=C:\path-to-keystore\key.jks" "-Djavax.net.ssl.keyStorePassword=topsecret" "-Djavax.net.debug=ssl" "-Dcom.ibm.mq.cfg.useIBMCipherMappings=false"

2d。使用以下命令启动 tomcat:

catalina.bat run > ..\logs\tomcat.log 2>&1 

要停止,只需按 ctrl+c(在 windows 上)。无论采用哪种方式,请确保在启动期间使用 setenv.bat。或者使用 JAVA_OPTS 设置密钥库属性。

2e。验证使用 SSL 通道是否有效。

步骤 3:获取使用非 SSL、JNDI 的 JNDI 连接 有很多是在 tomcat 上设置 JNDI。以下是我的操作方法:在 Web 应用程序中创建一个包含以下内容的文件 META-INF/Context.xml:

<Resource
   name="jms/qcf_sandbox"
   auth="Container"
   type="com.ibm.mq.jms.MQQueueConnectionFactory"
   factory="com.ibm.mq.jms.MQQueueConnectionFactoryFactory"
   description="JMS Queue Connection Factory for sending messages"
   HOST="localhost"
   PORT="1414"
   CHAN="NON_SSL_CHANNEL"
   TRAN="1"
   QMGR="QM_SANDBOX"/>
<Resource
   name="jms/SandboxQ"
   auth="Container"
   type="com.ibm.mq.jms.MQQueue"
   factory="com.ibm.mq.jms.MQQueueFactory"
   description="JMS Queue"
   QU="SANDBOX_Q"/>

现在在您的 spring 配置中,您需要做的不是直接配置,而是:

<jee:jndi-lookup id="mq-jms-cf-sandbox" jndi-name="java:/comp/env/jms/qcf_sandbox" resource-ref="false" />
<jee:jndi-lookup id="jms-destination-sandbox" jndi-name="java:/comp/env/jms/SandboxQ" resource-ref="false" />

请注意,为了简洁起见,我只是没有使用资源引用。如果您这样做,还有一些简单的额外步骤。

第 4 步:现在最后一步是使用 SSL 通道和 JNDI。假设您已完成第 2 步,这很容易。修改META-INF/Context.xml内容如下:

<Resource
   name="jms/qcf_sandbox"
   auth="Container"
   type="com.ibm.mq.jms.MQQueueConnectionFactory"
   factory="com.ibm.mq.jms.MQQueueConnectionFactoryFactory"
   description="JMS Queue Connection Factory for sending messages"
   HOST="localhost"
   PORT="1414"
   CHAN="SSL_CHANNEL"
   TRAN="1"
   QMGR="QM_SANDBOX"
   SCPHS="TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"/>
<Resource
   name="jms/SandboxQ"
   auth="Container"
   type="com.ibm.mq.jms.MQQueue"
   factory="com.ibm.mq.jms.MQQueueFactory"
   description="JMS Queue"
   QU="SANDBOX_Q"/>

注意带有 SCPHS="TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" 的行。如果您需要设置其他此类参数,请参阅本link中的"Short Form"专栏: https://www.ibm.com/support/knowledgecenter/SSFKSJ_8.0.0/com.ibm.mq.ref.dev.doc/q111800_.htm%23jm10910_?lang=en

希望这一切对您有用。祝你好运!

一旦此配置生效,发送消息就非常简单了。但这是您可以使用 Spring JMS 在队列中侦听消息的方式 参考:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/jms.html

第 1 步:使用 Spring 的 DefaultMessageListenerContainer 并在 xml 文件中配置您的 bean,如下所示(spring-beans.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"      
    "http://www.springframework.org/dtd/spring-beans.dtd">

   <!-- this is the Message Driven POJO (MDP) -->
   <bean id="messageListener" class="jmsexample.ExampleListener" />

   <!-- and this is the message listener container -->
   <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
       <property name="connectionFactory" ref="mq-jms-cf-sandbox"/>
       <property name="destination" ref="jms-destination-sandbox"/>
       <property name="messageListener" ref="messageListener" />
   </bean>

</beans>

第 2 步:将此添加到您的 web.xml

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/context/spring-beans.xml</param-value>
 </context-param>

<listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

第 3 步:像这样编写消息侦听器class:

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class ExampleListener implements MessageListener {

    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                System.out.println(((TextMessage) message).getText());
            }
            catch (JMSException ex) {
                throw new RuntimeException(ex);
            }
        }
        else {
            throw new IllegalArgumentException("Message must be of type TextMessage");
        }
    }
}

或者,如果您使用 spring 集成,而不是第 3 步,您可以这样做:

<int:channel id="jms-inbound"/>
    <int-jms:message-driven-channel-adapter
        id="jms-inbound-adapter" container="jmsContainer" channel="jms-inbound" 
        extract-payload="true" acknowledge="transacted" 
        message-converter="messagingMessageConverter" />

<beans:bean id="messagingMessageConverter" class="org.springframework.jms.support.converter.MessagingMessageConverter">
    </beans:bean>