Skip to content
Advertisement

Connect to IBM MQ using JmsListener

I have a Spring application and I want to connect to IBM MQ using JmsListener. This is my configuration:

package com.in28minutes.example.layering.model;

import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter;

import javax.jms.ConnectionFactory;

@Configuration
@EnableJms
public class Qm1Config {

    @Bean
    public MQQueueConnectionFactory qm1ConnectionFactory() {
        MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
        try {
            cf.setHostName("localhost");
            cf.setPort(1414);
            cf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
            cf.setQueueManager("QM1");
            cf.setChannel("DEV.ADMIN.SVRCONN");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cf;
    }

    @Bean
    UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(@Qualifier("qm1ConnectionFactory") ConnectionFactory connectionFactory) {
        UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
        userCredentialsConnectionFactoryAdapter.setUsername("admin");
        userCredentialsConnectionFactoryAdapter.setPassword("passw0rd");
        userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(connectionFactory);
        return userCredentialsConnectionFactoryAdapter;
    }

    @Bean
    public JmsListenerContainerFactory<?> qm1JmsListenerContainerFactory(@Qualifier("userCredentialsConnectionFactoryAdapter") UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(userCredentialsConnectionFactoryAdapter);
        return factory;
    }
}

This is my consumer:

package com.in28minutes.example.layering.model;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class QueueConsumer {

    @JmsListener(destination = "DEV.QUEUE.1", containerFactory = "qm1JmsListenerContainerFactory")
    public void receive1(String text) {
        System.out.println("Received from qm1: " + text);
    }

}

I have deployed the application into TomEE plus container, and I have sent few messages to the queue:

enter image description here

I have also added the below entries in the servlet-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:beans=""
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.in28minutes.example.layering.model" />
</beans>

However, application did not consume these messages. How do I fix this?

Update#1 I have annotated the consumer class with @EnableJMS

@Component
@EnableJms
public class QueueConsumer {

    @JmsListener(destination = "DEV.QUEUE.1", containerFactory = "qm1JmsListenerContainerFactory")
    public void receive1(String text) {
        System.out.println("Received from qm1: " + text);
    }

}

still I am not getting the messages

Advertisement

Answer

can you try adding @EnableJms on QueueConsumer class?

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement