void readToEnd(CompletableFuture<Void> future) { synchronized (this) { if (outstandingReadToEnd != null) { outstandingReadToEnd.whenComplete((result, cause) -> { if (null != cause) { future.completeExceptionally(cause); } else { future.complete(result); } }); // return if the outstanding read has been issued return; } else { outstandingReadToEnd = future; future.whenComplete((result, cause) -> { synchronized (PulsarOffsetBackingStore.this) { outstandingReadToEnd = null; } }); } } producer.flushAsync().whenComplete((ignored, cause) -> { if (null != cause) { future.completeExceptionally(cause); } else { checkAndReadNext(future); } }); }
@Override public Future<Void> set(Map<ByteBuffer, ByteBuffer> values, Callback<Void> callback) { values.forEach((key, value) -> { ByteBuf bb = Unpooled.wrappedBuffer(key); byte[] keyBytes = ByteBufUtil.getBytes(bb); bb = Unpooled.wrappedBuffer(value); byte[] valBytes = ByteBufUtil.getBytes(bb); producer.newMessage() .key(new String(keyBytes, UTF_8)) .value(valBytes) .sendAsync(); }); return producer.flushAsync().whenComplete((ignored, cause) -> { if (null != callback) { callback.onCompletion(cause, ignored); } if (null == cause) { readToEnd(new CompletableFuture<>()); } }); } }