Search in sources :

Example 1 with CompletableFutureCancellationHandler

use of org.apache.pulsar.common.util.CompletableFutureCancellationHandler in project pulsar by apache.

the class MultiTopicsConsumerImpl method internalReceiveAsync.

@Override
protected CompletableFuture<Message<T>> internalReceiveAsync() {
    CompletableFutureCancellationHandler cancellationHandler = new CompletableFutureCancellationHandler();
    CompletableFuture<Message<T>> result = cancellationHandler.createFuture();
    internalPinnedExecutor.execute(() -> {
        Message<T> message = incomingMessages.poll();
        if (message == null) {
            pendingReceives.add(result);
            cancellationHandler.setCancelAction(() -> pendingReceives.remove(result));
        } else {
            decreaseIncomingMessageSize(message);
            checkState(message instanceof TopicMessageImpl);
            unAckedMessageTracker.add(message.getMessageId(), message.getRedeliveryCount());
            resumeReceivingFromPausedConsumersIfNeeded();
            result.complete(message);
        }
    });
    return result;
}
Also used : Message(org.apache.pulsar.client.api.Message) CompletableFutureCancellationHandler(org.apache.pulsar.common.util.CompletableFutureCancellationHandler)

Example 2 with CompletableFutureCancellationHandler

use of org.apache.pulsar.common.util.CompletableFutureCancellationHandler in project pulsar by apache.

the class ConsumerImpl method internalBatchReceiveAsync.

@Override
protected CompletableFuture<Messages<T>> internalBatchReceiveAsync() {
    CompletableFutureCancellationHandler cancellationHandler = new CompletableFutureCancellationHandler();
    CompletableFuture<Messages<T>> result = cancellationHandler.createFuture();
    internalPinnedExecutor.execute(() -> {
        if (hasEnoughMessagesForBatchReceive()) {
            MessagesImpl<T> messages = getNewMessagesImpl();
            Message<T> msgPeeked = incomingMessages.peek();
            while (msgPeeked != null && messages.canAdd(msgPeeked)) {
                Message<T> msg = incomingMessages.poll();
                if (msg != null) {
                    messageProcessed(msg);
                    if (!isValidConsumerEpoch(msg)) {
                        msgPeeked = incomingMessages.peek();
                        continue;
                    }
                    Message<T> interceptMsg = beforeConsume(msg);
                    messages.add(interceptMsg);
                }
                msgPeeked = incomingMessages.peek();
            }
            result.complete(messages);
        } else {
            OpBatchReceive<T> opBatchReceive = OpBatchReceive.of(result);
            pendingBatchReceives.add(opBatchReceive);
            cancellationHandler.setCancelAction(() -> pendingBatchReceives.remove(opBatchReceive));
        }
    });
    return result;
}
Also used : Messages(org.apache.pulsar.client.api.Messages) CompletableFutureCancellationHandler(org.apache.pulsar.common.util.CompletableFutureCancellationHandler)

Example 3 with CompletableFutureCancellationHandler

use of org.apache.pulsar.common.util.CompletableFutureCancellationHandler in project pulsar by apache.

the class ConsumerImpl method internalReceiveAsync.

@Override
protected CompletableFuture<Message<T>> internalReceiveAsync() {
    CompletableFutureCancellationHandler cancellationHandler = new CompletableFutureCancellationHandler();
    CompletableFuture<Message<T>> result = cancellationHandler.createFuture();
    internalPinnedExecutor.execute(() -> {
        Message<T> message = incomingMessages.poll();
        if (message == null) {
            pendingReceives.add(result);
            cancellationHandler.setCancelAction(() -> pendingReceives.remove(result));
        } else {
            messageProcessed(message);
            if (!isValidConsumerEpoch(message)) {
                pendingReceives.add(result);
                cancellationHandler.setCancelAction(() -> pendingReceives.remove(result));
                return;
            }
            result.complete(beforeConsume(message));
        }
    });
    return result;
}
Also used : CommandMessage(org.apache.pulsar.common.api.proto.CommandMessage) Message(org.apache.pulsar.client.api.Message) CompletableFutureCancellationHandler(org.apache.pulsar.common.util.CompletableFutureCancellationHandler)

Example 4 with CompletableFutureCancellationHandler

use of org.apache.pulsar.common.util.CompletableFutureCancellationHandler in project pulsar by apache.

the class MultiTopicsConsumerImpl method internalBatchReceiveAsync.

@Override
protected CompletableFuture<Messages<T>> internalBatchReceiveAsync() {
    CompletableFutureCancellationHandler cancellationHandler = new CompletableFutureCancellationHandler();
    CompletableFuture<Messages<T>> result = cancellationHandler.createFuture();
    try {
        lock.writeLock().lock();
        if (hasEnoughMessagesForBatchReceive()) {
            MessagesImpl<T> messages = getNewMessagesImpl();
            Message<T> msgPeeked = incomingMessages.peek();
            while (msgPeeked != null && messages.canAdd(msgPeeked)) {
                Message<T> msg = incomingMessages.poll();
                if (msg != null) {
                    decreaseIncomingMessageSize(msg);
                    if (!isValidConsumerEpoch(msg)) {
                        msgPeeked = incomingMessages.peek();
                        continue;
                    }
                    Message<T> interceptMsg = beforeConsume(msg);
                    messages.add(interceptMsg);
                }
                msgPeeked = incomingMessages.peek();
            }
            result.complete(messages);
        } else {
            OpBatchReceive<T> opBatchReceive = OpBatchReceive.of(result);
            pendingBatchReceives.add(opBatchReceive);
            cancellationHandler.setCancelAction(() -> pendingBatchReceives.remove(opBatchReceive));
        }
        resumeReceivingFromPausedConsumersIfNeeded();
    } finally {
        lock.writeLock().unlock();
    }
    return result;
}
Also used : Messages(org.apache.pulsar.client.api.Messages) CompletableFutureCancellationHandler(org.apache.pulsar.common.util.CompletableFutureCancellationHandler)

Aggregations

CompletableFutureCancellationHandler (org.apache.pulsar.common.util.CompletableFutureCancellationHandler)4 Message (org.apache.pulsar.client.api.Message)2 Messages (org.apache.pulsar.client.api.Messages)2 CommandMessage (org.apache.pulsar.common.api.proto.CommandMessage)1