Skip to content

Commit

Permalink
Fix consume message order issue when use listener. (apache#13023)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technoboy- authored Dec 13, 2021
1 parent 29c7dd9 commit e134e37
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -913,30 +913,32 @@ private void doPendingBatchReceiveTask(Timeout timeout) {
protected void triggerListener() {
// Trigger the notification on the message listener in a separate thread to avoid blocking the networking
// thread while the message processing happens
try {
// Control executor to call MessageListener one by one.
if (executorQueueSize.get() < 1) {
final Message<T> msg = internalReceive(0, TimeUnit.MILLISECONDS);
if (msg != null) {
executorQueueSize.incrementAndGet();
if (SubscriptionType.Key_Shared == conf.getSubscriptionType()) {
executorProvider.getExecutor(peekMessageKey(msg)).execute(() ->
callMessageListener(msg));
} else {
getExternalExecutor(msg).execute(() -> {
callMessageListener(msg);
});
internalPinnedExecutor.execute(() -> {
try {
// Control executor to call MessageListener one by one.
if (executorQueueSize.get() < 1) {
final Message<T> msg = internalReceive(0, TimeUnit.MILLISECONDS);
if (msg != null) {
executorQueueSize.incrementAndGet();
if (SubscriptionType.Key_Shared == conf.getSubscriptionType()) {
executorProvider.getExecutor(peekMessageKey(msg)).execute(() ->
callMessageListener(msg));
} else {
getExternalExecutor(msg).execute(() -> {
callMessageListener(msg);
});
}
}
}
} catch (PulsarClientException e) {
log.warn("[{}] [{}] Failed to dequeue the message for listener", topic, subscription, e);
return;
}
} catch (PulsarClientException e) {
log.warn("[{}] [{}] Failed to dequeue the message for listener", topic, subscription, e);
return;
}

if (log.isDebugEnabled()) {
log.debug("[{}] [{}] Message has been cleared from the queue", topic, subscription);
}
if (log.isDebugEnabled()) {
log.debug("[{}] [{}] Message has been cleared from the queue", topic, subscription);
}
});
}

protected void callMessageListener(Message<T> msg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1243,8 +1243,7 @@ void messageReceived(MessageIdData messageId, int redeliveryCount, List<Long> ac

uncompressedPayload.release();
}
internalPinnedExecutor.execute(()
-> tryTriggerListener());
tryTriggerListener();

}

Expand Down

0 comments on commit e134e37

Please sign in to comment.