这是配置:
The intended flow is:
1. Select records from table that have a certain status
2. Mark them as in progress
3. The record is used to create a RabbitMQ message using RabbitTemplate with confirm ack.
4. When message is acked, update status in record.
What is happening, randomly, is that the "confirm ack" is being called twice, so the message is duplicated in the rabbit queue, exactly the same, but with different messageId.
How could I fix this?
Thanks.
<!-- begin snippet: js hide: false console: true babel: false -->
package com.xoom.platform.api.integration.publisher;
import com.xoom.domain.dao.event.PaymentSourceEventDao;
import com.xoom.domain.model.constant.MessagePublishingStatus;
import com.xoom.platform.api.integration.exception.PaymentSourcePublishingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.util.Assert;
public class PaymentSourceEventPublisherConfirm implements RabbitTemplate.ConfirmCallback {
private final Logger logger = LoggerFactory.getLogger(PaymentSourceEventPublisherConfirm.class);
private final PaymentSourceEventDao paymentSourceEventDao;
public PaymentSourceEventPublisherConfirm(final PaymentSourceEventDao paymentSourceEventDao) {
this.paymentSourceEventDao = paymentSourceEventDao;
}
@Override
public void confirm(final CorrelationData correlationData, final boolean ack, final String cause) {
logger.debug("Confirming payment source event publishing: correlationData=[{}], ack=[{}], cause=[{}].", correlationData, ack, cause);
if (ack) {
handleAck(correlationData);
} else {
handleNack(correlationData, cause);
}
}
private void handleNack(final CorrelationData correlationData, final String cause) {
logger.warn("Unconfirmed payment source event publishing correlationData=[{}], cause=[{}].", correlationData, cause);
}
private void handleAck(final CorrelationData correlationData) {
final long eventId = getEventId(correlationData);
try {
paymentSourceEventDao.updateStatus(eventId, MessagePublishingStatus.PUBLISHED);
} catch (final Exception e) {
throw new PaymentSourcePublishingException("Unable to confirm the payment source event publishing, correlationData=[" + correlationData + "].", e);
}
}
private long getEventId(final CorrelationData correlationData) {
final String correlationId = (correlationData == null) ? null : correlationData.getId();
Assert.notNull(correlationId, "Received null correlationId, correlationData=[" + correlationData + "].");
try {
return Long.parseLong(correlationId);
} catch (final NumberFormatException expected) {
throw new IllegalArgumentException("Received invalid correlationId, correlationData=[" + correlationData + "].");
}
}
}