-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Publish with confirm listener added #56
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,14 @@ | |
import io.appform.dropwizard.actors.actor.DelayType; | ||
import io.appform.dropwizard.actors.base.utils.NamingUtils; | ||
import io.appform.dropwizard.actors.connectivity.RMQConnection; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentNavigableMap; | ||
import java.util.concurrent.ConcurrentSkipListMap; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import javax.validation.constraints.NotNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.RandomUtils; | ||
|
||
|
@@ -106,6 +114,93 @@ public final long pendingSidelineMessagesCount() { | |
return Long.MAX_VALUE; | ||
} | ||
|
||
/** | ||
* @param messages : Messages to be published | ||
* @param properties | ||
* @param timeout : in MS timeout for waiting on countDownLatch | ||
* @param unit : timeout unit | ||
* @return : List of message nacked | ||
* @throws Exception | ||
*/ | ||
public List<Message> publishWithConfirmListener(List<Message> messages, AMQP.BasicProperties properties, | ||
long timeout, @NotNull TimeUnit unit) throws Exception { | ||
publishChannel.confirmSelect(); | ||
ConcurrentNavigableMap<Long, Message> outstandingConfirms = new ConcurrentSkipListMap<>(); | ||
List<Message> nackedMessages = new ArrayList<>(); | ||
CountDownLatch publishAckLatch = new CountDownLatch(messages.size()); | ||
|
||
publishChannel.addConfirmListener((sequenceNumber, multiple) -> { | ||
messagesAck(sequenceNumber, multiple, outstandingConfirms, publishAckLatch); | ||
}, (sequenceNumber, multiple) -> { | ||
nackedMessages.addAll(messagesNack(sequenceNumber, multiple, outstandingConfirms, publishAckLatch)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just assign the return value rather than this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because there can be multiple callback with multiple = true There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, my bad |
||
}); | ||
|
||
|
||
long startTime = System.nanoTime(); | ||
|
||
for (Message message : messages) { | ||
try { | ||
String routingKey = NamingUtils.getRoutingKey(queueName, config); | ||
outstandingConfirms.put(publishChannel.getNextPublishSeqNo(), message); | ||
publishChannel.basicPublish(config.getExchange(), routingKey, properties, | ||
mapper().writeValueAsBytes(message)); | ||
} catch (Exception e) { | ||
log.error(String.format("Failed to publish Message : %s with exception %s", message, e)); | ||
publishAckLatch.countDown(); | ||
} | ||
} | ||
|
||
if (!publishAckLatch.await(timeout, unit)) { | ||
log.error("Timed out waiting for publish acks"); | ||
} | ||
|
||
long endTime = System.nanoTime(); | ||
|
||
log.info(String.format("Published %d messages with confirmListener in %d ms. Total Messages : %d", messages.size() - outstandingConfirms.size(), | ||
Duration.ofNanos(startTime - endTime).toMillis(), messages.size())); | ||
nackedMessages.addAll(outstandingConfirms.values()); | ||
return nackedMessages; | ||
} | ||
|
||
|
||
private void messagesAck(long sequenceNumber, boolean multiple, ConcurrentNavigableMap<Long, Message> outstandingConfirms, CountDownLatch publishAckLatch) | ||
{ | ||
if (multiple) { | ||
ConcurrentNavigableMap<Long, Message> confirmed = outstandingConfirms.headMap( | ||
sequenceNumber, true | ||
); | ||
for(int i =0;i<confirmed.size();i++) | ||
publishAckLatch.countDown(); | ||
confirmed.clear(); | ||
} else { | ||
publishAckLatch.countDown(); | ||
outstandingConfirms.remove(sequenceNumber); | ||
} | ||
} | ||
|
||
private List<Message> messagesNack(long sequenceNumber, boolean multiple, ConcurrentNavigableMap<Long, Message> outstandingConfirms, CountDownLatch publishAckLatch) | ||
{ | ||
List<Message> nackedMessages = new ArrayList<>(); | ||
if(multiple == true) | ||
{ | ||
ConcurrentNavigableMap<Long, Message> nacked = outstandingConfirms.headMap( | ||
sequenceNumber, true | ||
); | ||
for(int i =0;i<nacked.size();i++) | ||
publishAckLatch.countDown(); | ||
nackedMessages.addAll(nacked.values()); | ||
nacked.clear(); | ||
} | ||
else | ||
{ | ||
publishAckLatch.countDown(); | ||
nackedMessages.add(outstandingConfirms.get(sequenceNumber)); | ||
outstandingConfirms.remove(sequenceNumber); | ||
} | ||
return nackedMessages; | ||
} | ||
|
||
|
||
public void start() throws Exception { | ||
final String exchange = config.getExchange(); | ||
final String dlx = config.getExchange() + "_SIDELINE"; | ||
|
@@ -193,4 +288,8 @@ protected final RMQConnection connection() { | |
protected final ObjectMapper mapper() { | ||
return mapper; | ||
} | ||
|
||
public void setPublishChannel(Channel publishChannel){ | ||
this.publishChannel = publishChannel; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why changing versions manually