Spring Boot @JmsListener consuming messages without msg.acknowledge() even though Session is set to CLIENT_ACKNOWLEDGE

88 Views Asked by At

Spring Boot @JmsListener is consuming messages without msg.acknowledge() even though the Session is set to CLIENT_ACKNOWLEDGE. I have tried setting spring.jms.listener.acknowledge-mode=CLIENT in application.properties, but it's no use.

import javax.jms.ConnectionFactory;
import javax.jms.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;

@EnableJms
@Configuration
public class MQConfiguration {
    protected final Log logger = LogFactory.getLog(getClass());

    @Bean(name = "jmsListenerContainerFactory")
    DefaultJmsListenerContainerFactory jmsContainerFactory(ConnectionFactory connectionFactory,
            DefaultJmsListenerContainerFactoryConfigurer configurer) {

        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setErrorHandler(t -> {
            logger.warn("An error has occurred in the transaction");
            logger.error(t.getCause().getMessage());
        });
        factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
        configurer.configure(factory, connectionFactory);
        factory.setConcurrency("1");
        factory.setSessionTransacted(false);
        
        return factory;
    }    
}

@JmsListener(containerFactory = "jmsListenerContainerFactory", destination = "school.XXXX.request.clock-in", concurrency = "1")
public void consumeMessageFromMQ(String message, Message msg) throws Throwable {
    -----------
} 

Spring Version is:

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.7.15</version>
   <relativePath />
</parent>
<properties>
   <java.version>17</java.version>
</properties>
<dependency>
   <groupId>org.apache.activemq</groupId>
   <artifactId>artemis-jms-client</artifactId>
</dependency>    
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
0

There are 0 best solutions below