I'm using Spring @JmsListener to consume message from IBM MQ queue (running in Docker container). I test a scenario as below:
- Send message to MQ via web browser
- My Spring service will consume and send extracted data from message to a stub (wiremock)
- service receives success response and exit normally
In a happy case, message will be committed and removed from the queue, now I add response delay in stub (30s for example), when service waiting for response, I quit Docker to simulate a network issue or MQ down (docker stop cause MQ quiescing which is not my expectation).
So I have 2 questions here?
- How can I catch the commit exception thrown by DefaultMessageListenerContainer?
- I use DefaultJmsListenerContainerFactory::setExceptionListener() method to attach a listener, I can log the exception here but logback MDC is not logged (MDC contains messageId and payload for audit purpose). How can I pass MDC values to this listener?
Source code:
@Bean
public DefaultJmsListenerContainerFactory containerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(createConnectionFactory());
factory.setExceptionListener(exceptionListener());
factory.setSessionAcknowledgeMode(javax.jms.Session.CLIENT_ACKNOWLEDGE);
return factory;
}
@Bean
public ExceptionListener exceptionListener() {
return exception -> {
if (exception.getCause() instanceof InterruptedException) {
// MDC value is not logged in below log.error()
log.error("commit error");
} else {
log.error("jms connection error");
}
}
}
---
@JmsListener(id="0", destination="DEV.QUEUE.1", containerFactory="containerFactory")
public void listener(Message msg) {
try {
// extract data
MDC.put("key", value); // for audit
// call stub
} catch (JMSException e) {
throw new Exception(e); // throw exception, don't commit
}
}
Any help would be most appreciated.
docker stop
on the MQ queue manager docker instance then your queue manager will stop. If you terminate the process then it will terminate. Can you usedocker network disconnect
to simulate a network outage between your various containers? Do you see theJMSXDeliveryCount
incremented for the message you are processing? – richcJMSXDeliveryCount
is 1 when I receive msg and after I disconnect, it's still 1, but there is no exception thrown from my service when I rundocker network disconnect
– dauruydis qs(DEV.QUEUE.1)
it returns UNCOM(1), but after a while, it returns UNCOM(NO) – dauruy