最近在研究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