I need to check number of messages in a jms queue for that i am using queuebrowser. Actually i have 2 queues. I check if there are more then 10 messages in second queue then the first queue should not send messages to the second queue.

This is my code snippet

            connection = connectionFactory.createConnection();

            Queue queue = (Queue) initialContext.lookup(inputQueue); // this is the first queue (named iniputQueue)

            session = connection.createSession();

                consumer = session.createConsumer(queue);

            // change code for browsing queue
            Queue senderqueue = (Queue) initialContext
            QueueBrowser queueBrowser = session.createBrowser(senderqueue); //this is second queue named senderQueue

//by queuebrowser i check if messages in second queue is more then 10 then do  //not send the message
            Enumeration e = queueBrowser.getEnumeration();
            int numMsgs=0;
            // count number of messages
            while (e.hasMoreElements()) {

            LOG.debug("checkflag is" +checkFlag);

            if (checkFlag) {

                ObjectMessage objectMessage = (ObjectMessage) consumer

                if (objectMessage != null) {
                    LOG.debug("sending one batch out");
                    InputJSON inputJson = (InputJSON) objectMessage

                    SendService messageService = new SendService();




the above code is called by a schedular every 2 secs which keeps checking the sender queue and based on that sends message out from input queue.

Is there any better way to do this..


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.