-1

I have to implement a method in which there will be a listener which will read a messages from queue and then it will send those messages into table. Now my concern is that there can be 1 message or 10 messages. I have to read them one by one and dump those messages into table; I have implemented it but I doubt that the conditions I have put are not in correct order. Can you guys please advise is it the correct condition below.

@Transactional(rollbackFor = { Throwable.class })
public String dumpMesagesFromabcQueue() {
    String tibcoQueueName = configuration.getSpecificConfiguration(Constants.DFR_QUEUE);
    jmsTemplate.setDefaultDestinationName(tibcoQueueName);
    jmsTemplate.setPubSubDomain(false);

    try {
        while (tibcoUtility.getQueueMessagePendingCount(tibcoQueueName) != 0) {
            Message message = jmsTemplate.receive();
            String messageType = null;
            String cashFlowMesg = null;
            if (message instanceof ObjectMessage) {
                try {
                    ObjectMessage objMessage = (ObjectMessage) message;
                    String[] messageArray = (String[]) objMessage.getObject();
                    cashFlowMesg = messageArray[0];
                    messageType = messageArray[1];
                    abcHelper.ttt(rrr, null, ddd, eee, rrrrrr, trw, tyi, new Throwable(ero));
                } catch (JMSException e) {
                    logger.error("   Error retriving messages from error queue to ttt queu ", e);
                    throw new RuntimeException(e);
                }
            }
        }
    } catch (TibjmsAdminException exp) {
        String err = "<font color=red><b>Error encountered while processing  queue ";
        err += exp.toString();
        err += "</b></font>";
        return err;
    }
    return "<font color=blue><b>Messages consumed successfully </b></font>";
}
Leos Literak
  • 8,805
  • 19
  • 81
  • 156
  • Sorry I do not know tibco messaging to answear you. I would recommend you to limit loop with some counter that breaks it when some limit is reached. I do not like HTML formatting inside business logic methods. I do not know your code but it would be cleaner to return true/false here and create formatted user message elsewhere. This will make your code more flexible and easier to test. – Leos Literak Mar 08 '14 at 17:27
  • Btw is it neccessary to use tibcoUtility.getQueueMessagePendingCount()? Why do you not rely on jmsTemplate inside loop and break when it returns null or limit is reached. – Leos Literak Mar 08 '14 at 17:32
  • @LeosLiterak Thanks can you please write pseudocode so that I can grasp more Thanks – user3270422 Mar 09 '14 at 03:48

1 Answers1

0

I thought something as simple. Disclaimer: I have developed many JMS programs for JEE/Oracle AQ, but I have no experience with Tibco/Spring's JMSTemplate.

This method will be responsible for processing the queue with some limit. If there is an error it returns false. There shall be some presentation method that utilizes this method and will format error message if neccessary.

@Transactional(rollbackFor = {Throwable.class})
public boolean dumpMesagesFromabcQueue() {
    String tibcoQueueName = configuration.getSpecificConfiguration(Constants.DFR_QUEUE);
    jmsTemplate.setDefaultDestinationName(tibcoQueueName);
    jmsTemplate.setPubSubDomain(false);

    int i = 0, limit = configuration.getSpecificConfiguration(Constants.FETCH_QUEUE_SIZE);
    while (i < limit) {
        Message message = jmsTemplate.receive();
        if (message == null) {
            return true;
        }
        i++;
        if (message instanceof ObjectMessage) {
            try {
                ObjectMessage objMessage = (ObjectMessage) message;
                String[] messageArray = (String[]) objMessage.getObject();
// unused                   String cashFlowMesg = messageArray[0];
// unused                   String messageType = messageArray[1];
                abcHelper.ttt(rrr, null, ddd, eee, rrrrrr, trw, tyi, new Throwable(ero));
            } catch (JMSException e) {
                logger.error("   Error retriving messages from error queue to ttt queu ", e);
                return false;
            }
        }
    }
    return true;
}
Leos Literak
  • 8,805
  • 19
  • 81
  • 156