0

I am using JMS listener with spring boot and want to control message dequeing from queue only when the message been processed successfully. I have set session acknowledgement mode to client acknowledgement and i see in case of exception , message.acknowledge method is not getting executed, still the message is being removed from queue. Any idea where i am going wrong. thanks in advance -

jms configuration -

@Bean
  public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(
      ConnectionFactory connectionFactory,
      DefaultJmsListenerContainerFactoryConfigurer configurer) {

    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setSessionTransacted(true);
    factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    factory.setConcurrency(jmsConcurrentConnection);
    factory.setRecoveryInterval(Long.parseLong(jmsConnectionRecoveryInterval));
    factory.setErrorHandler(jmsErrorHandler);
    configurer.configure(factory, connectionFactory);
    return factory;
  }

Message processing code-

@JmsListener(destination = "${queue_name}")
  public void listenMessage(Message message) throws JMSException {

    if (message instanceof TextMessage) {
      TextMessage messageFromQueue = (TextMessage) message;
      try {
        
        processService.processData(
            messageFromQueue.getText(), processHeaders(messageFromQueue));
        log.debug("Received message processed successfully!");
        message.acknowledge();

      } catch (JMSException e) {
        log.error("Exception occurred :" + e.getMessage());
      } catch (ResourceAccessException e) {
        log.error("IOException occurred : [{}]", e.getMessage());
      } catch (CustomRuntimeException e) {
        log.error(
            "CustomRuntimeException occurred. message  couldn't be processed successfully");
      }

When i get any unforseen runtime error, i see the log in the CustomRuntimeException method being executed and that time log.debug("Received message processed successfully!") is not getting printed. That means my message.acknowledge method is not getting processed but i see the message being removed from queue, which i do not want. I want to hold the message in the queue in case it has not been processed successfully.

Reese
  • 389
  • 2
  • 10
  • 26
  • 1
    Does this answer your question? [Spring JMS listener acknowledging even when exception](https://stackoverflow.com/questions/43817402/spring-jms-listener-acknowledging-even-when-exception) – Tim Bish Jul 15 '21 at 13:57
  • Hi Justin.. this answer i had seen already..seems like that is the only way out..but i was looking for a way to control this from the code itself..seems like that is not possible.. but the work around worked.. thank you! – Reese Jul 23 '21 at 17:24

0 Answers0