2013-01-25 401 views
3

嗨我是Spring JMS和websphere MQ的新手。可以任何人给我一步一步的过程或例如如何接收来自websphere MQ的消息,并能够在控制台中打印该消息 非常感谢您的帮助Spring JMS和Websphere MQ

+0

我发布了春季MDP /激活规范/网络在 http://stackoverflow.com/questions/7390286/whats-the-difference-between-activationspec-and-connectionfactory – user3344338

回答

4

下面是使用Spring的MDP /激活规范为WebSphere MQ

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

<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

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); 
     } 
    } 
    } 
工作示例

'

+0

@Jan:评论这个答案。不要编辑代码。 – Brian

+0

你可以发布你的代码吗?我试图用Spring STS来解决这个问题,并且有资源异常。在这里发布问题; http://stackoverflow.com/questions/32257686/spring-jms-failing-to-connect-to-websphere-mq-resource-exception – haju

+0

在哪里配置队列和细节的QueueManager?这里只配置ActiveSpec。AS将如何知道连接哪个队列 – Chinmoy

2

这些是为WMQ V5.3编写的,但大多仍然应用。 WMQ管理员所做的更改与连接和配置有关。

developerWorks: The Spring Series

请务必包括WMQ服务器和客户端对未来岗位的版本,因为在Java/JMS配置的细节上有所不同。另外,请务必使用与您正在使用的WMQ客户端或服务器版本相匹配的文档版本。

2

您可能还想考虑在JMS之上使用Spring集成;这里有一个示例使用ActiveMQ https://github.com/SpringSource/spring-integration-samples/tree/master/basic/jms - 您只需要将JMS配置更改为使用MQ。

样品读取来自控制台发送通过JMS消息,由消息驱动适配器读取,并且写入到控制台。

+1

感谢您的答复和链接pshere MQ例子。当我downlad它给了我像20个不同的项目,我的cdnt找到需要的例子或既不运行它。我只是想简单的例子能够运行 – nepJava

+0

?困惑?链接指向GitHub上关于如何运行它的自述文件的示例。简单地克隆Github示例回购并按照说明操作。 –

2

我刚刚经历过这个自己。先从Spring Boot JMS Starter

添加一个bean提供MQQueueConnectionFactory

@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; 
    } 
} 

卸下org.apache.activemq/ActiveMQ的经纪人的依赖,以确保ActiveMQ的不潜行它到底。

添加依赖于com.ibm.mqjms.jar,com.bim.mq.jmqi.jar,dhbcore.jar

运行