消费者在动态创建时不消费消息

时间:2012-06-12 09:10:51

标签: 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; 
            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) { 

                                    } 
                            } 

                    } 
                    } 

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

1 个答案:

答案 0 :(得分:0)

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

另一件事,我不确定你为什么要在消息监听器中进行接收。一旦将其设置为侦听器,就会对队列中的每个消息调用onMessage,并且每个接收到的消息的逻辑应以事件驱动的方式驻留在那里。至少,这是JMS首先的想法