无法使用JMSListener消耗来自activeMQ的消息

时间:2016-02-10 06:14:41

标签: spring

首先,我向 ActiveMQ 发送消息及其工作,现在我想使用 JMSListener 从队列中使用该消息。但这不起作用。请帮忙!

这是我的config.java

ApplicationConfig.java

            package com.ge.health.gam.poc.config;
            import javax.annotation.Resource;
            import javax.jms.ConnectionFactory;
            import org.apache.activemq.ActiveMQConnectionFactory;
            import org.apache.activemq.RedeliveryPolicy;
            import org.springframework.context.ApplicationContext;
            import org.springframework.context.annotation.Bean;
            import org.springframework.context.annotation.Configuration;
            import org.springframework.jms.annotation.EnableJms;
            import org.springframework.jms.annotation.JmsListener;
            import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
            import org.springframework.jms.core.JmsOperations;
            import org.springframework.jms.core.JmsTemplate;
            import org.springframework.jms.support.destination.BeanFactoryDestinationResolver;
            import org.springframework.jms.support.destination.DestinationResolver;

                @Configuration
                @EnableJms
                public class AppConfig {

                @Resource
                ApplicationContext applicationContext;

                @Bean
                public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
                DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
                factory.setConnectionFactory(connectionFactory());
                factory.setDestinationResolver(destinationResolver());
                factory.setConcurrency("9");
                factory.setTransactionManager(null);
                return factory;
                }

                @Bean
                public ConnectionFactory connectionFactory() {

                    ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

                    RedeliveryPolicy policy = new RedeliveryPolicy();
                    policy.setMaximumRedeliveries(0);
                    activeMQConnectionFactory.setRedeliveryPolicy(policy);

                    return activeMQConnectionFactory;


                //return new ActiveMQConnectionFactory("tcp://localhost:61616");

                }

                @Bean
                JmsTemplate jmsTemplate(ConnectionFactory connectionFactory){
                return new JmsTemplate(connectionFactory);
                }

                @Bean
                public DestinationResolver destinationResolver() {
                return new BeanFactoryDestinationResolver(applicationContext);
                }


            }

    First I am sending message to ActiveMQ and its working and now i want to consume that message from queue using JMSListener. But this is not working. Please help.This is DemoConsumer class

            package com.ge.health.gam.poc.consumer;

            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.beans.factory.annotation.Qualifier;
            import org.springframework.jms.annotation.JmsListener;
            import org.springframework.jms.core.JmsTemplate;
            import org.springframework.messaging.handler.annotation.SendTo;
            import org.springframework.stereotype.Component;

            @Component
            public class DemoConsumer {

                    @JmsListener(destination ="NewQueue")
                    public void demoConsumeMessage(String message){
                    System.out.println("Message Received");
                }
            }             

0 个答案:

没有答案