剛開始讓我的頭在JMS ActiveMQ Acknowledgements
在春季工作。到目前爲止,我有一位消費者完美地工作,除了當我不承認消息時,它仍然從隊列中取出(我期望它留在那裏或以死信隊列結束)。獲取一個簡單的Spring JMS客戶端確認工作
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">
<!-- A JMS connection factory for ActiveMQ -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
p:brokerURL="failover://(tcp://jms1:61616,tcp://jms2:61616)?randomize=false&jms.redeliveryPolicy.maximumRedeliveries=5" />
<!-- A POJO that implements the JMS message listener -->
<bean id="simpleMessageListener" class="com.company.ConsumerClass" />
<!-- A JMS namespace aware Spring configuration for the message listener container -->
<jms:listener-container
container-type="default"
connection-factory="connectionFactory"
acknowledge="client"
concurrency="10-50"
cache="consumer">
<jms:listener destination="someQueue" ref="simpleMessageListener" method="onMessage" />
</jms:listener-container>
</beans>
在ConsumerClass,我簡單的消費看起來是這樣的:
@Override public final void onMessage(Message message) {
Object postedMessage = null;
try {
postedMessage = ((ObjectMessage) message).getObject();
if (postedMessage.getClass() == SomeMessageType.class) {
try {
//Some logic here
message.acknowledge();
return; //Success Here
} catch (MyException e) {
logger.error("Could not process message, but as I didn't call acknowledge I expect it to end up in the dead message queue");
}
}
} catch (JMSException e) {
logger.error("Error occurred pulling Message from Queue", e);
}
//Also worth noting, if I throw new RuntimeException("Aww Noos"); here then it won't take it from the queue, but it won't get consumed (or end up as dead letter)...
}
對不起,在這種情況下它是成功的,如果我的message.acknowledge後返回()(我加入這個代碼示例) – 2012-02-01 21:39:11
另外,如果我去掉了拋RuntimeException的(它必須運行,以滿足MessageListener接口)我收到一個錯誤: 08:39:59,066 WARN org.springframework.jms.listener.DefaultMessageListenerContainer#0-2 listener.DefaultMessageListenerContainer:694 - JMS消息偵聽器的執行失敗,並且沒有設置ErrorHandler。 java.lang.RuntimeException:Aww Noos 我可以看到,在這種情況下,消息持有消費者,直到我終止它,在這一點上,他們會試圖消費另一位消費者,讓我認爲承認=「客戶端「不執行任何操作 – 2012-02-01 21:44:17
實際上,如果我讀取了javadoc權限,」auto「將在調用偵聽器之前確認: 」sessionAcknowledgeMode「設置爲」AUTO_ACKNOWLEDGE「(默認值):偵聽器執行前的自動消息確認;在拋出異常情況下不會重新傳遞。「 – dstibbe 2013-12-20 11:25:47