0

need your help with updating the following code,currently this code fetches the first msg from IBM MQ ,but my requirement is to fetch until MQ is empty

    private JmsTemplate jmsTemplate;

public JmsTemplate getJmsTemplate() {
    return jmsTemplate;
}

@Autowired
public void setJmsTemplate(JmsTemplate jmsTemplate) {
    this.jmsTemplate = jmsTemplate;
}

public String onMessage() throws JMSException, IOException {        

    Message source = jmsTemplate.receive("LQ.SCCO4313");

    BytesMessage bm = (BytesMessage) source;

    byte[] byteArr = new byte[(int) bm.getBodyLength()];
    bm.readBytes(byteArr); 

    String msg = new String(byteArr, "UTF-8");  

    logger.info("Message " + msg);
    logger.info("MESSAGE received from myMessageQueue ");
    return msg;

}
JoshMc
  • 10,239
  • 2
  • 19
  • 38
Tanuja Gupta
  • 89
  • 1
  • 3
  • 12

1 Answers1

0

A JMS MessageConsumer's receive() or receiveNoWait() would return with null where there is no message in a queue. There will be an exception if there is any internal error.

So you can check for source == null in your code.

Shashi
  • 14,980
  • 2
  • 33
  • 52