merlimat commented on a change in pull request #1066: Issue 937: add 
CommandGetLastMessageId to make reader know the end of topic
URL: https://github.com/apache/incubator-pulsar/pull/1066#discussion_r168006084
 
 

 ##########
 File path: 
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java
 ##########
 @@ -1248,6 +1254,103 @@ public void seek(MessageId messageId) throws 
PulsarClientException {
         return seekFuture;
     }
 
+    public boolean hasMessageAvailable() throws PulsarClientException {
+        try {
+            if (lastMessageIdInBroker.compareTo(lastDequeuedMessage) > 0 &&
+                ((MessageIdImpl)lastMessageIdInBroker).getEntryId() != -1) {
+                return true;
+            }
+
+            return hasMessageAvailableAsync().get();
+        } catch (ExecutionException | InterruptedException e) {
+            throw new PulsarClientException(e);
+        }
+    }
+
+    public CompletableFuture<Boolean> hasMessageAvailableAsync() {
+        final CompletableFuture<Boolean> booleanFuture = new 
CompletableFuture<>();
+
+        if (lastMessageIdInBroker.compareTo(lastDequeuedMessage) > 0 &&
+            ((MessageIdImpl)lastMessageIdInBroker).getEntryId() != -1) {
+            booleanFuture.complete(true);
+        } else {
+            getLastMessageIdAsync().thenAccept(messageId -> {
+                lastMessageIdInBroker = messageId;
+                if (lastMessageIdInBroker.compareTo(lastDequeuedMessage) > 0 &&
+                    ((MessageIdImpl)lastMessageIdInBroker).getEntryId() != -1) 
{
+                    booleanFuture.complete(true);
+                } else {
+                    booleanFuture.complete(false);
+                }
+            }).exceptionally(e -> {
+                log.error("[{}][{}] Failed getLastMessageId command", topic, 
subscription);
+                booleanFuture.completeExceptionally(e.getCause());
+                return null;
+            });
+        }
+        return booleanFuture;
+    }
+
+    private CompletableFuture<MessageId> getLastMessageIdAsync() {
+        if (getState() == State.Closing || getState() == State.Closed) {
+            return FutureUtil
+                .failedFuture(new 
PulsarClientException.AlreadyClosedException("Consumer was already closed"));
+        }
+
+        if (!isConnected()) {
+            long opTimeoutMs = 
client.getConfiguration().getOperationTimeoutMs();
+            Backoff backoff = new Backoff(100, TimeUnit.MILLISECONDS,
+                opTimeoutMs * 2, TimeUnit.MILLISECONDS,
+                0 , TimeUnit.MILLISECONDS);
+
+            long delayMs = backoff.firstBackoffTimeInMillis;;
+            while (delayMs < opTimeoutMs && !isConnected()); {
+                log.warn("[{}] [{}] Could not get connection while 
getLastMessageId -- Will try again in {} ms",
+                    topic, getHandlerName(), delayMs);
+                try {
+                    Thread.sleep(delayMs);
+                } catch (InterruptedException e) {
+                    return FutureUtil
+                        .failedFuture(new PulsarClientException
+                            .ConnectException("InterruptedException, could not 
connect"));
+                }
+                delayMs = backoff.next();
+            }
+
+            if (!isConnected()) {
+                return FutureUtil.failedFuture(new PulsarClientException("Not 
connected to broker"));
+            }
+        }
+
+        if (cnx().getRemoteEndpointProtocolVersion() < 
ProtocolVersion.v12.getNumber()) {
 
 Review comment:
   Nit: please wrap this in a method like : 
   
   ```java
   void Commands.peerSupportsGetLastMessageId(ClientCnx cnx);
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to