-
Notifications
You must be signed in to change notification settings - Fork 48
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 1 commit
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; | ||
|
||
|
@@ -24,7 +32,7 @@ public class UnmanagedPublisher<Message> { | |
private final ObjectMapper mapper; | ||
private final String queueName; | ||
|
||
private Channel publishChannel; | ||
public Channel publishChannel; | ||
|
||
public UnmanagedPublisher( | ||
String name, | ||
|
@@ -106,17 +114,105 @@ public final long pendingSidelineMessagesCount() { | |
return Long.MAX_VALUE; | ||
} | ||
|
||
/** | ||
* Note: Timeout is in MilliSeconds, Function take a list of message as input and return not ack msg as output | ||
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. We are taking timeunit, so maybe this comment needs to change? 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. done |
||
* @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 |
||
}); | ||
|
||
String routingKey = NamingUtils.getRoutingKey(queueName, config); | ||
|
||
long startTime = System.nanoTime(); | ||
|
||
for (Message message : messages) { | ||
try { | ||
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(unit.toMillis(timeout), TimeUnit.MILLISECONDS)) { | ||
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. What is wrong with publishAckLatch.await(timeout, unit) 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. done |
||
log.error("Timed out waiting for publish acks"); | ||
} | ||
|
||
long endTime = System.nanoTime(); | ||
|
||
log.info(String.format("Published %d messages with confirmListener in %d ms", messages.size() - outstandingConfirms.size(), | ||
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. We won't know how many messages were submitted, i.e. total messages. |
||
Duration.ofNanos(startTime - endTime).toMillis())); | ||
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"; | ||
this.publishChannel = connection.newChannel(); | ||
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 is this change done? 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. reverting this change |
||
if (config.isDelayed()) { | ||
ensureDelayedExchange(exchange); | ||
} else { | ||
ensureExchange(exchange); | ||
} | ||
ensureExchange(dlx); | ||
|
||
this.publishChannel = connection.newChannel(); | ||
connection.ensure(queueName + "_SIDELINE", queueName, dlx, | ||
connection.rmqOpts(config)); | ||
if (config.isSharded()) { | ||
|
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 is this necessary?
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.
reverted it, created a function to set publish channel