activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dharrigan <dharri...@gmail.com>
Subject Re: Can't get messages off the queue! - Solved!
Date Fri, 25 Aug 2006 09:10:14 GMT

Hi,

I figured it out:

The magic is this:

messageConsumer = queueSession.createReciever(queue);
messageConsumer.setMessageListener(this);
queueConnection.start();

It appears that we have to return "whom" we are sending the messages to from
calling
queueSession.createReceiver(queue), then on the "whom" we tell who is the
message
listener (in this case, ourselves).

All works now! For completeness, here is the code:

    private void bootstrap() {
        try {
            final Context context = new InitialContext(getContext());
            final QueueConnectionFactory queueConnectionFactory =
(QueueConnectionFactory) context.lookup("QueueConnectionFactory");
            final Queue queue = (Queue) context.lookup(getQueueName());
            final QueueConnection queueConnection =
queueConnectionFactory.createQueueConnection();
            final QueueSession queueSession =
queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);            
            MessageConsumer messageConsumer =
queueSession.createReceiver(queue);
            messageConsumer.setMessageListener(this);
            queueConnection.start();       
        } catch(Exception e) {
            logger.error(e);
        }
    }

    public String getName() {
        return name;
    }

    public String getQueueName() {
        return queueName;
    }
    
    public void onMessage(final Message message) {
        try {
            if(message instanceof ObjectMessage) {
                final LoggingEvent loggingEvent = (LoggingEvent)
((ObjectMessage) message).getObject();
               
Logger.getLogger(loggingEvent.getLoggerName()).info(loggingEvent.getRenderedMessage());
            } else if(message instanceof TextMessage) {
                final String payload = ((TextMessage) message).getText();
                System.out.println(payload);
                logger.info(payload);
            }
        } catch(JMSException e) {
            logger.error(e);
        }
    


Thanks for your suggestions James.


-=david=-




James.Strachan wrote:
> 
> Rather than doing a receiveNoWait() then sleeping for 50, you should
> use receive(50) which is a better alternative :)
> 
> Am not sure why onMessage() is not working for you -
> setMessageListener does work fine
> 
> On 8/25/06, dharrigan <dharrigan@gmail.com> wrote:
>>
>> Hi,
>>
>> My JNDI setup was fine, both using dynamicQueues/QueueName and both using
>> ActiveMQInitalcontextFactory (see src, I'm shoving them into a properties
>> object
>> then passing that to InitialContext()).
>>
>> It started to work when I abandoned using a MessageListener and instead
>> had
>> each consumer create a thread with a busy loop polling
>> MessageConsumer.recieveNoWait()
>> (hmm, just thinking, it would be best just to block on recieve() until a
>> message comes
>> thru...) anyway, it all worked when I did this.
>>
>> Is the onMessage broken? Or am I doing something bizarre in not getting
>> it
>> to
>> work correct? Here's my updated code:
>>
>>     private void bootstrap() {
>>         try {
>>             final Context context = new InitialContext(getContext());
>>             final QueueConnectionFactory queueConnectionFactory =
>> (QueueConnectionFactory) context.lookup("QueueConnectionFactory");
>>             final Queue queue = (Queue) context.lookup(getQueueName());
>>             final QueueConnection queueConnection =
>> queueConnectionFactory.createQueueConnection();
>>             final QueueSession queueSession =
>> queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
>>             queueConnection.start();
>>             messageConsumer =
>> queueSession.createConsumer(queueSession.createQueue("queryQueue"));
>>             queueSession.createReceiver(queue);
>>             new Thread(this).start();
>>             System.out.println("Started " + getName() + " on queue " +
>> queue.getQueueName());
>>         } catch(Exception e) {
>>             logger.error(e);
>>         }
>>     }
>>
>>     public String getName() {
>>         return name;
>>     }
>>
>>     public String getQueueName() {
>>         return queueName;
>>     }
>>
>>     public void run() {
>>         Message message = null;
>>         while(true) {
>>             try {
>>                 message = messageConsumer.receiveNoWait();
>>             } catch(JMSException e) {
>>                 logger.error(e);
>>             }
>>             if(message != null) {
>>                 onMessage(message);
>>             }
>>             try {
>>                 Thread.sleep(50);
>>             } catch(InterruptedException e) {
>>
>>             }
>>         }
>>     }
>>
>>     public void onMessage(final Message message) {
>>         try {
>>             if(message instanceof ObjectMessage) {
>>                 final LoggingEvent loggingEvent = (LoggingEvent)
>> ((ObjectMessage) message).getObject();
>>
>> Logger.getLogger(loggingEvent.getLoggerName()).info(loggingEvent.getRenderedMessage());
>>             } else if(message instanceof TextMessage) {
>>                 final String payload = ((TextMessage) message).getText();
>>                 System.out.println(payload);
>>                 logger.info(payload);
>>             }
>>         } catch(JMSException e) {
>>             logger.error(e);
>>         }
>>     }
>>
>>     private Properties getContext() {
>>         Properties properties = new Properties();
>>         properties.put(Context.INITIAL_CONTEXT_FACTORY,
>> "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
>>         properties.put(Context.PROVIDER_URL, "tcp://jmsserver:61616");
>>         return properties;
>>     }
>>
>>
>>
>>
>>
>>
>> James.Strachan wrote:
>> >
>> > Sounds like you may have a mix up in your JNDI configuration. Try
>> > looking in JMX at the consumers to see what they are subscribing to
>> > and check that they are using the correct queue names etc.
>> >
>> > e.g. you might want to use exactly the same code to get the Queue on
>> > both producer and consumer to save you making a mistake on one side.
>> >
>> >
>> > On 8/24/06, dharrigan <dharrigan@gmail.com> wrote:
>> >>
>> >> Hi,
>> >>
>> >> Using 4.0.1 on Linux.
>> >>
>> >> I'm trying to consume some messages from a set of queues that I've
>> >> defined.
>> >>
>> >> Queue 1, 2, 3 and 4
>> >>
>> >> These are created by a factory that instantiates 4 instances of a
>> class,
>> >> passing in the name of the queue that the instantiated class should
>> >> monitor.
>> >> However, nothing is coming off the queue and I can see in the JMX
>> console
>> >> that I have about 1,900 messages waiting for me to pickup...
>> >>
>> >> Here's my code:
>> >>
>> >> First the producer:
>> >>
>> >>     private void doIt() throws Exception {
>> >>         final Context context = new
>> >> InitialContext(getContextProperties());
>> >>         final QueueConnectionFactory queueConnectionFactory =
>> >> (QueueConnectionFactory) context.lookup("QueueConnectionFactory");
>> >>         final Queue queue =
>> >> (Queue)context.lookup("dynamicQueues/queryQueue");
>> >>         final QueueConnection queueConnection =
>> >> queueConnectionFactory.createQueueConnection();
>> >>         final QueueSession queueSession =
>> >> queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
>> >>         final QueueSender queueSender =
>> queueSession.createSender(queue);
>> >>         final TextMessage textMessage =
>> queueSession.createTextMessage();
>> >>         queueConnection.start();
>> >>         for(int i = 0 ; i < 10 ; i++) {
>> >>             textMessage.setText("Hello World! " + i);
>> >>             System.out.println("Sending message with text : " +
>> >> textMessage.getText());
>> >>             queueSender.send(textMessage);
>> >>         }
>> >>         queueSender.send(queueSession.createMessage());
>> >>         queueConnection.close();
>> >>     }
>> >>
>> >>     private Properties getContextProperties() {
>> >>         Properties properties = new Properties();
>> >>         properties.put(Context.INITIAL_CONTEXT_FACTORY,
>> >> "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
>> >>         properties.put(Context.PROVIDER_URL,
>> >> "tcp://jmsserver:61616?jms.useAsyncSend=true");
>> >>         return properties;
>> >>     }
>> >> }
>> >>
>> >>
>> >>
>> >> Now the consumer:
>> >>
>> >>
>> >>  private void bootstrap() {
>> >>         try {
>> >>             final Context context = new InitialContext(getContext());
>> >>             final QueueConnectionFactory queueConnectionFactory =
>> >> (QueueConnectionFactory) context.lookup("QueueConnectionFactory");
>> >>             final Queue queue = (Queue)
>> context.lookup(getQueueName());
>> >>             final QueueConnection queueConnection =
>> >> queueConnectionFactory.createQueueConnection();
>> >>             final QueueSession queueSession =
>> >> queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
>> >>             queueSession.createReceiver(queue);
>> >>             queueSession.setMessageListener(this);
>> >>             queueConnection.start();
>> >>             System.out.println("Started " + getName() + " on queue " +
>> >> queue.getQueueName());
>> >>         } catch(Exception e) {
>> >>             logger.error(e);
>> >>         }
>> >>     }
>> >>
>> >>     public String getName() {
>> >>         return name;
>> >>     }
>> >>
>> >>     public String getQueueName() {
>> >>         return queueName;
>> >>     }
>> >>
>> >>     public void onMessage(final Message message) {
>> >>         try {
>> >>             if(message instanceof ObjectMessage) {
>> >>                 final LoggingEvent loggingEvent = (LoggingEvent)
>> >> ((ObjectMessage) message).getObject();
>> >>
>> >>
>> Logger.getLogger(loggingEvent.getLoggerName()).info(loggingEvent.getRenderedMessage());
>> >>             } else if(message instanceof TextMessage) {
>> >>                 final String payload = ((TextMessage)
>> message).getText();
>> >>                 System.out.println(payload);
>> >>                 logger.info(payload);
>> >>             }
>> >>         } catch(JMSException e) {
>> >>             logger.error(e);
>> >>         }
>> >>     }
>> >>
>> >>     private Properties getContext() {
>> >>         Properties properties = new Properties();
>> >>         properties.put(Context.INITIAL_CONTEXT_FACTORY,
>> >> "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
>> >>         properties.put(Context.PROVIDER_URL, "tcp://jmsserver:61616");
>> >>         return properties;
>> >>     }
>> >>
>> >> Any help would be very very much appreciated!
>> >>
>> >> -=david=-
>> >> --
>> >> View this message in context:
>> >>
>> http://www.nabble.com/Can%27t-get-messages-off-the-queue%21-tf2158965.html#a5965055
>> >> Sent from the ActiveMQ - User forum at Nabble.com.
>> >>
>> >>
>> >
>> >
>> > --
>> >
>> > James
>> > -------
>> > http://radio.weblogs.com/0112098/
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Can%27t-get-messages-off-the-queue%21-tf2158965.html#a5979542
>> Sent from the ActiveMQ - User forum at Nabble.com.
>>
>>
> 
> 
> -- 
> 
> James
> -------
> http://radio.weblogs.com/0112098/
> 
> 

-- 
View this message in context: http://www.nabble.com/Can%27t-get-messages-off-the-queue%21-tf2158965.html#a5980003
Sent from the ActiveMQ - User forum at Nabble.com.


Mime
View raw message