Activemq 消费者在动态创建时不使用消息

Activemq 消费者在动态创建时不使用消息,activemq,producer-consumer,Activemq,Producer Consumer,我正在学习在我的项目中实现活动mq接口。这就是我创造生产者和消费者的方式 public void connectionSetup(String portName) { // portname is object of PortTO class. We are creating producer and consumer pair for every existing PortTO object. Connection connection = null;

我正在学习在我的项目中实现活动mq接口。这就是我创造生产者和消费者的方式

public void connectionSetup(String portName) { // portname is object of PortTO class.   We are creating producer and consumer pair for every existing PortTO object.
            Connection connection = null; 
            try { 

                    if (timeToLive != 0) { 

                    } 

                    // Create the connection. 
                    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); 
                    connection = connectionFactory.createConnection(); 

                    connection.start(); 
                    connection.setExceptionListener(this); 

                    // Create the session 
                    Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE); 
                    if (topic) { 
                            destination = session.createTopic(subject); 
                    } else { 
                            destination = session.createQueue(portName); 
                    } 

                    // Create the producer. 
                    MessageProducer producer = session.createProducer(destination);                        if (persistent) { 
                            producer.setDeliveryMode(DeliveryMode.PERSISTENT); 
                    } else { 
                            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 
                    } 


                    MessageConsumer consumer = session.createConsumer(destination);                        if (timeToLive != 0) 
                            producer.setTimeToLive(timeToLive); 

                    mapOfSession.put(portName, session); 
                    mapOfMessageProducer.put(portName, producer); 
                    mapOfMessageConsumer.put(portName, consumer);                        log.info("Producer is " + producer); 
                    log.info("Consumer is " + consumer); 

            } catch (Exception e) { 

                    log.error(e.getMessage()); 
            } 
    } 
因此,我们创建生产者和消费者,并将它们存储在每个PortTO对象的映射中。现在,制作人正在发送消息:

TextMessage message = session.createTextMessage(); 
 message.setIntProperty(key, 2); 
  producer.send(message);        
但消费者并没有消费它

        public void onMessage(Message message) { 
            PortService portService = new PortService();      
           List<PortTO> portTOList = portService.getMoxaPorts(); 
                    for(PortTO portTO : portTOList) {  // catching messages from producers of every PortTO object                                
              MessageConsumer consumer = DataCollectionMessageProducer.getMapOfMessageConsumer().get(portTO.getPort());  // getting consumer from map of PortTO
                            consumer.setMessageListener(this); 
                            message = consumer.receive(1000);                                if (message instanceof TextMessage) { 
            / / some processing 
                         } 
                            } else { 
                                    if (verbose) { 

                                    } 
                            } 

                    } 
                    } 

原因可能是什么?我的方法错了吗

您正在onMessage方法中设置messageListener。这是一个catch 22,因为只有当messageListener设置为该对象时,才会调用onMessage方法


另一件事,我不知道为什么要在消息侦听器中进行接收。一旦队列上的每条消息被设置为侦听器,onMessage将被调用,并且每条接收到的消息的逻辑应该以事件驱动的方式驻留在那里。至少,这是JMS最初的想法

那么我如何接收消息..我的意思是我不想在xml文件中手动设置messageListener.consumer.setMessageListenerthis;按代码设置消息侦听器。您不需要为此编辑xml。不要在onMessage中执行此操作,当消息到达时将调用该方法。我明白了。实际上,在我的项目中,我们正在xml文件中创建消息侦听器。但是消息侦听器的数量将来可能会有所不同,这取决于需求。是否可以动态创建消息侦听器,而不使用xml编码?