Spring JMS 和 Websphere MQ
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14523572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring JMS and Websphere MQ
提问by nepJava
hi I am new to Spring JMS and websphere MQ. Can Any one give me step by step processs or example how to receive message from websphere MQ and be able to print that message in console thanks u very much for your help
嗨,我是 Spring JMS 和 websphere MQ 的新手。任何人都可以给我一步一步的过程或示例如何从 websphere MQ 接收消息并能够在控制台中打印该消息,非常感谢您的帮助
回答by Dwight Shih
I just went through this myself. Start with the Spring Boot JMS Starter
我自己刚刚经历了这个。从Spring Boot JMS Starter 开始
Add a bean providing a MQQueueConnectionFactory
添加一个提供 MQQueueConnectionFactory 的 bean
@Configuration
@EnableJms
public class MQConfiguration {
@Bean
public MQQueueConnectionFactory mqFactory()
{
MQQueueConnectionFactory factory = null;
try {
factory = new MQQueueConnectionFactory();
factory.setHostName("localhost");
factory.setPort(1414);
factory.setQueueManager("QM.LOCAL");
factory.setChannel("SYSTEM.DEF.SVRCONN");
factory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
}
catch (JMSException e) {
System.out.println(e);
}
return factory;
}
}
Remove the dependency on org.apache.activemq / activemq-broker to make sure activemq doesn't sneak its way in.
删除对 org.apache.activemq / activemq-broker 的依赖,以确保 activemq 不会潜入。
Add dependencies on com.ibm.mqjms.jar, com.bim.mq.jmqi.jar, dhbcore.jar
添加对 com.ibm.mqjms.jar、com.bim.mq.jmqi.jar、dhbcore.jar 的依赖
Run
跑
回答by user3344338
Here is a working sample using Spring MDP/Activation Spec for websphere MQ
这是一个使用 Spring MDP/Activation Spec for websphere MQ 的工作示例
mdp-listener.xml
mdp-listener.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<bean id="messageListener" class="com.rohid.samples.SpringMdp" />
<bean class="org.springframework.jms.listener.endpoint.JmsMessageEndpointManager">
<property name="activationSpec">
<bean class="com.ibm.mq.connector.inbound.ActivationSpecImpl">
<property name="destinationType" value="javax.jms.Queue"/>
<property name="destination" value="QUEUE1"/>
<property name="hostName" value="A.B.C"/>
<property name="queueManager" value="QM_"/>
<property name="port" value="1414"/>
<property name="channel" value="SYSTEM.ADMIN.SVNNN"/>
<property name="transportType" value="CLIENT"/>
<property name="userName" value="abc"/>
<property name="password" value="jabc"/>
</bean>
</property>
<property name="messageListener" ref="messageListener"/>
<property name="resourceAdapter" ref="myResourceAdapterBean"/>
</bean>
<bean id="myResourceAdapterBean" class ="org.springframework.jca.support.ResourceAdapterFactoryBean">
<property name="resourceAdapter">
<bean class="com.ibm.mq.connector.ResourceAdapterImpl">
<property name="maxConnections" value="50"/>
</bean>
</property>
<property name="workManager">
<bean class="org.springframework.jca.work.SimpleTaskWorkManager"/>
</property>
</bean>
</beans>
web.xml
网页.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context/mdp-listener.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
SpringMdp
春季MDP
package com.rohid.samples;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class SpringMdp implements MessageListener {
public void onMessage(Message message) {
try {
if(message instanceof TextMessage) {
System.out.println(this + " : " + ((TextMessage) message).getText());
}
} catch (JMSException ex){
throw new RuntimeException(ex);
}
}
}
'
'
回答by T.Rob
These were written for WMQ V5.3 but mostly still apply. The things that have changed are more about the WMQ admin than the connectivity and config.
这些是为 WMQ V5.3 编写的,但大部分仍然适用。发生变化的更多是关于 WMQ 管理而不是连接和配置。
developerWorks: The Spring Series
Please be sure to include the versions of WMQ server and client on future posts because the details of the Java/JMS configuration differ. Also, be sure to use the version of the documentation that matches the version of WMQ client or server that you are working with.
请确保在以后的帖子中包含 WMQ 服务器和客户端的版本,因为 Java/JMS 配置的细节不同。此外,请务必使用与您正在使用的 WMQ 客户端或服务器版本相匹配的文档版本。
回答by Gary Russell
You might also want to consider using Spring Integration on top of JMS; there's a sample here that uses ActiveMQ https://github.com/SpringSource/spring-integration-samples/tree/master/basic/jms- you would just need to change the JMS config to use MQ instead.
您可能还想考虑在 JMS 之上使用 Spring Integration;这里有一个使用 ActiveMQ 的示例https://github.com/SpringSource/spring-integration-samples/tree/master/basic/jms- 您只需要更改 JMS 配置即可使用 MQ。
The sample reads from the console sends the message over JMS, read by a message-driven adapter, and written to the console.
从控制台读取的示例通过 JMS 发送消息,由消息驱动的适配器读取,然后写入控制台。

