3
\$\begingroup\$

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
                    .lookup(senderQueue);
            QueueBrowser queueBrowser = session.createBrowser(senderqueue); //this is second queue named senderQueue

            connection.start();
//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()) {
                numMsgs++;
                if(numMsgs>10){
                    checkFlag=false;
                    break;
                }else{
                    checkFlag=true;
                }
            }

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

            if (checkFlag) {

                ObjectMessage objectMessage = (ObjectMessage) consumer
                        .receive(1);

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

                    SendService messageService = new SendService();

                    messageService.processInputFromQueue(inputJson);


                }



            }

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..

\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.