我想我终于想出了如何解决这个问题……这里是对步骤的简要说明。如果您需要更多详细信息,请告诉我。
前置要求:
已安装 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”——全部小写,没有空格,(不带引号),密钥标签必须是像 'ibmwebspheremquserid' - 全部小写,没有空格,(不带引号)其中 userid 是运行 tomcat 的用户 ID。如果您需要详细了解我是如何使用自签名证书做到这一点的,请告诉我。
2c。现在您必须获取 tomcat 运行的 JVM,才能读取您的密钥库。有很多方法,但我是这样做的:
在tomcat bin文件夹下创建一个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" 的行。如果您需要设置其他此类参数,请参阅此链接中的“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 步:编写一个 Message Listener 类,如下所示:
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>