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:單條消息確認spring

若是想設置ack消息確認機制爲客戶端手動確認,在spring總配置消費者監聽器的時候,設置sessionAcknowledgeMode的值爲2,代碼以下:session

<!-- 消息監聽器 -->  
<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的一段源碼:this

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便可。code

相關文章
相關標籤/搜索