0

I am learning to implement active mq interface in my project. This is how I am creating producers and consumers.

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()); 
            } 
    } 

So, we are creating producer and consumer and storing them in a map for every PortTO object. Now, producer is sending messages:

TextMessage message = session.createTextMessage(); 
 message.setIntProperty(key, 2); 
  producer.send(message);        

But consumer is not consuming it...

        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) { 

                                    } 
                            } 

                    } 
                    } 

What can be the reason? Is my approach wrong ??

user746458
  • 369
  • 2
  • 13
  • 26

1 Answers1

0

You are setting the messageListener in the onMessage method. This is a catch 22, since the onMessage method gets invoked only if the messageListener is set to that object.

Another thing, I am not sure why you would do a receive in a message listener. The onMessage will be invoked for each message on the queue once it has been set as listener and the logic for each received message should reside in there in an event driven fashion. At least, that is the idea with JMS in the first place

Petter Nordlander
  • 22,053
  • 5
  • 50
  • 84
  • So how would I recieve the message..I mean I do not want to set messageListener manually in xml file. – user746458 Jul 11 '12 at 11:26
  • consumer.setMessageListener(this); Sets the message listener by code. You don't need to edit the xml for this. Don't do this inside the onMessage, that method will be invoked when a message arrives. – Petter Nordlander Jul 11 '12 at 13:15
  • I got the point. Actually, in my project, we are creating message listener in xml files. But the number of message listeners may vary in future, depending upon the requirement. Is it possible to create message listeners dynamically, without coding them in xml? – user746458 Jul 12 '12 at 07:23