How to Handle JMS Queue efficiently avoiding load on server

60 Views Asked by At

My question here is to know how can I handle load on server if jms queue (Wildfly 10) gets filled at high extent. The problem is messages are continuously being added to JMS queue but they are processed one by one.The logic that is written is to process messages one by one only.We can't process multiple messages at a time that is logic cannot be changed. But this leads to increase load on server as queue gets loaded with many messages. How can I manage the queue at this point.

Here is the sample code I am using for producing and consuming:-

The following code runs through schedular every 100 milliseconds and fetches 100 messages from database and send to queue.

Arraylist arrMessages=GetMessagesFromdatabase();

for(MessageObject obj:arrMessages) sendMessagetoQueue(obj);

Producer code:-

void sendMessagetoQueue(MessageObject messageObject){
        ConnectionFactory connectionFactory = JMSConstants.getConnFactory.getJmsConnectionFactory();
            Context initialContext = JMSConstants.getConnFactory.getInitialcontext();
            Connection connection = null;

            String destinationName = "java:/jms/queue/Queue";
            MessageProducer publisher = null;
            Session session = null;

            try {

                connection = connectionFactory.createConnection();

                Queue queue = (Queue) initialContext
                        .lookup(destinationName);

                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

                publisher = session.createProducer(queue);

                connection.start();

                ObjectMessage objMessage = session.createObjectMessage(messageObject);

                publisher.send(objMessage);
                } catch (Exception exc) {
                exc.printStackTrace();
                LOG.error(exc);
            }
}

Then another schedular runs every 100 millisecond to consume message from queue and forward it for processing one by one.

Consumer Code:-

ConnectionFactory connectionFactory = JMSConstants.getConnFactory.getJmsConnectionFactory();
        Context initialContext = JMSConstants.getConnFactory.getInitialcontext();
        Connection connection = null;
        String destinationName = "java:/jms/queue/Queue";
        Session session = null;
        MessageConsumer consumer = null;
        Boolean checkFlag = true;
        QueueBrowser queueBrowserconnect = null;
        try {



                connection = connectionFactory.createConnection();

                Queue queue = (Queue) initialContext.lookup(destinationName);

                session = connection.createSession();
                consumer = session.createConsumer(queue);
                Queue senderqueueconnect = (Queue) initialContext.lookup(senderQueueconnect);
                queueBrowserconnect = session.createBrowser(senderqueueconnect);

                connection.start();


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

                if (objectMessage != null) {

                    objectMessage.acknowledge();

                    MessageObject messageobject= (MessageObject) objectMessage.getObject();

                    //Send object for processing
                    messageService.processInputFromQueue(messageobject);

                }


        } catch (Exception exc) {
            exc.printStackTrace();
            LOG.error(exc.getMessage());
        }
0

There are 0 best solutions below