2

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());
        }
Divya
  • 87
  • 1
  • 10
  • Don't use the built-in JMS server, but a standalone installation. Configure it to overflow to disk and make sure that it can handle a backlog. Then the wildfly performance will not be affected when the queues get full. This will also allow you to scale out, it is hard to do that if the queue is in the same application server as your application. – ewramner Mar 26 '19 at 11:57
  • But how to achieve it because our queue is added to wildfly server that is our application server – Divya Mar 26 '19 at 11:59
  • Yes, so if you don't want the broker to run inside Wildfly you need to use another queue. One that lives in a standalone broker. Need to point both the client and the server to that broker. Alternatively there are probably options for configuring the embedded broker to handle memory pressure better, but I haven't used them. – ewramner Mar 26 '19 at 15:19

0 Answers0