我使用ActiveMQ作爲代理來傳遞消息。這些消息是爲了寫在數據庫中。有時,數據庫無法訪問或關閉。在這種情況下,我想回滾我的郵件以便稍後重試此郵件,並且我想繼續閱讀其他郵件。ActiveMQ:死信隊列保持我的消息順序
此代碼工作正常,除了一點:rollbacked消息從閱讀別人阻止我:
private Connection getConnection() throws JMSException {
RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
redeliveryPolicy.setMaximumRedeliveries(3); // will retry 3 times to dequeue rollbacked messages
redeliveryPolicy.setInitialRedeliveryDelay(5 *1000); // will wait 5s to read that message
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
Connection connection = connectionFactory.createConnection();
((ActiveMQConnection)connection).setUseAsyncSend(true);
((ActiveMQConnection)connection).setDispatchAsync(true);
((ActiveMQConnection)connection).setRedeliveryPolicy(redeliveryPolicy);
((ActiveMQConnection)connection).setStatsEnabled(true);
connection.setClientID("myClientID");
return connection;
}
創建我會這樣說:
session = connection.createSession(true, Session.SESSION_TRANSACTED);
回滾是求人:
session.rollback();
讓我們想象我的隊列中有3條消息:
1: ok
2: KO (will need to be treated again : the message I want to rollback)
3: ok
4: ok
我的消費者會做(線性序列):
commit 1
rollback 2
wait 5s
rollback 2
wait 5s
rollback 2
put 2 in dead letter queue (ActiveMQ.DLQ)
commit 3
commit 4
但我想:
commit 1
rollback 2
commit 3
commit 4
wait 5s
rollback 2
wait 5s
rollback 2
wait 5s
put 2 in dead letter queue (ActiveMQ.DLQ)
所以,我如何配置我的消費者後來耽誤我rollbacked消息?
感謝您對行爲的明確解釋。我們終於選擇了RabbitMQ作爲經紀人,並自行實施了DLQ。 – 2011-03-19 14:54:47