activemq与spring整合,配置消费者监听器设置sessionAcknowledgeMode问题

最近在研究activemq的ack消息确认机制,在activemq与spring整合时遇到的了一个问题。
JMS规范的ack消息确认机制有一下四种,定于在session对象中:
AUTO_ACKNOWLEDGE = 1 :自动确认
CLIENT_ACKNOWLEDGE = 2:客户端手动确认
DUPS_OK_ACKNOWLEDGE = 3: 自动批量确认
SESSION_TRANSACTED = 0:事务提交并确认
但是在activemq补充了一个自定义的ACK模式:
INDIVIDUAL_ACKNOWLEDGE = 4:单条消息确认

如果想设置ack消息确认机制为客户端手动确认,在spring总配置消费者监听器的时候,设置sessionAcknowledgeMode的值为2,代码如下:

<!-- 消息监听器 -->  
<bean id="consumerMessageListener" class="com.dazong.service.mq.ConsumerMessageListener"/>  
<!-- 消息监听容器 -->  
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">  
        <property name="connectionFactory" ref="connectionFactory" />  
        <property name="destinationName" value="${default.queue}" />  
        <property name="messageListener" ref="consumerMessageListener" />
        <property name="sessionAcknowledgeMode" value="2"/>
</bean>

但这样设置是无效的,请看spring类org.springframework.jms.listener.AbstractMessageListenerContainer的一段源码:

protected void commitIfNecessary(Session session, Message message) throws JMSException {
        // Commit session or acknowledge message.
        if (session.getTransacted()) {
            // Commit necessary - but avoid commit call within a JTA transaction.
            if (isSessionLocallyTransacted(session)) {
                // Transacted session created by this container -> commit.
                JmsUtils.commitIfNecessary(session);
            }
        }
        else if (message != null && isClientAcknowledge(session)) {
            message.acknowledge();
        }
    }
protected boolean isClientAcknowledge(Session session) throws JMSException {
        return (session.getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE);
}

就是当设置sessionAcknowledgeMode为2时,虽然是客户端手动确认,但是却被spring自动确认了,造成设置无效。这时只需要把sessionAcknowledgeMode的值设置成activemq自定义的类型INDIVIDUAL_ACKNOWLEDGE = 4即可。

相关推荐