我的消费者在应用服务器中运行。这是我的ActiveMq配置:
optimizeAcknowledge=true
optimizeAcknowledgeTimeOut=5000
destination=queue.test.msg?consumer.prefetchSize=10
发送10条消息后," Web控制台(http://localhost:8161/admin)"可以看到3条消息不要Ack(使用optimizeAcknowledge),此时当我重新启动消费者服务器时,我再次收到3消息,RedeliveryCounter = 0。所以我不确定这是重传消息还是只是由制作人发送。
这个问题也可以在交易中找到。我运行测试类:org \ apache \ activemq \ RedeliveryPolicyTest.java,methoed:testRepeatedRedeliveryReceiveNoCommit。调试" connection.close();"并阻止它。消息的RedeliveryCounter也是" 0"。
问题:因此,当我确实确定邮件是否已重新发送时,我必须在消费者处理完邮件之后使用商店保存所有邮件的ID,并在消费者收到邮件时检查是否存在?
代码如下:
public void testRepeatedRedeliveryReceiveNoCommit() throws Exception {
connection.start();
Session dlqSession = connection.createSession(true, Session.SESSION_TRANSACTED);
ActiveMQQueue destination = new ActiveMQQueue("TEST");
MessageProducer producer = dlqSession.createProducer(destination);
// Send the messages
producer.send(dlqSession.createTextMessage("1st"));
dlqSession.commit();
MessageConsumer dlqConsumer = dlqSession.createConsumer(new ActiveMQQueue("ActiveMQ.DLQ"));
final int maxRedeliveries = 4;
for (int i=0;i<=maxRedeliveries +1;i++) {
connection = (ActiveMQConnection)factory.createConnection(userName, password);
connections.add(connection);
// Receive a message with the JMS API
RedeliveryPolicy policy = connection.getRedeliveryPolicy();
policy.setInitialRedeliveryDelay(0);
policy.setUseExponentialBackOff(false);
policy.setMaximumRedeliveries(maxRedeliveries);
connection.start();
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(destination);
ActiveMQTextMessage m = ((ActiveMQTextMessage)consumer.receive(4000));
if (i<=maxRedeliveries) {
m.setRedeliveryCounter(1);
System.out.println("Text:"+ m.getText());
System.out.println("RedeliveryCounter:" + m.getRedeliveryCounter());
} else {
System.out.println("null on exceeding redelivery count:" + m);
}
connection.close();
connections.remove(connection);
}
// We should be able to get the message off the DLQ now.
TextMessage m = (TextMessage)dlqConsumer.receive(1000);
System.out.println("Got message from DLQ:" + m);
System.out.println("Text:"+ m.getText());
String cause = m.getStringProperty(ActiveMQMessage.DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
System.out.println("cause exception has policy ref:" + cause.contains("RedeliveryPolicy"));
dlqSession.commit();
}