7

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

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
nepJava
  • 81
  • 1
  • 1
  • 4
  • I posted a Spring mdp/activation spec/webpshere mq example at http://stackoverflow.com/questions/7390286/whats-the-difference-between-activationspec-and-connectionfactory – user3344338 Feb 24 '14 at 23:30

4 Answers4

6

Here is a working sample using Spring MDP/Activation Spec for 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);
        }
     }
  }

'

user3344338
  • 76
  • 1
  • 3
  • 1
    Can you post the pom for your code? I am trying to get this working using Spring STS and having resource exceptions. Posted question here; http://stackoverflow.com/questions/32257686/spring-jms-failing-to-connect-to-websphere-mq-resource-exception – haju Aug 28 '15 at 12:59
  • 1
    Where to configure the Queue and QueueManager details?Here only ActiveSpec is configured.How AS will know which queue to connect – Chinmoy Jan 02 '18 at 06:42
5

I just went through this myself. Start with the Spring Boot JMS Starter

Add a bean providing a 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;
    }
}

Remove the dependency on org.apache.activemq / activemq-broker to make sure activemq doesn't sneak its way in.

Add dependencies on com.ibm.mqjms.jar, com.ibm.mq.jmqi.jar, dhbcore.jar

Run

Dwight Shih
  • 91
  • 1
  • 4
2

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.

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.

T.Rob
  • 31,522
  • 9
  • 59
  • 103
2

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.

The sample reads from the console sends the message over JMS, read by a message-driven adapter, and written to the console.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • 1
    thanks for the reply and the link . When i downlad it gave me like 20 different project and i cdnt find example needed or neither run it . I just want simple example to be able to run – nepJava Jan 25 '13 at 15:24
  • ? confused ? The link points right to the sample on GitHub with a ReadMe about how to run it. Simply clone the Github sample repo and follow the instructions. – Gary Russell Jan 25 '13 at 16:22