Search in sources :

Example 1 with CommandCloseConsumer

use of org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseConsumer in project incubator-pulsar by apache.

the class ServerCnx method handleCloseConsumer.

@Override
protected void handleCloseConsumer(CommandCloseConsumer closeConsumer) {
    checkArgument(state == State.Connected);
    log.info("[{}] Closing consumer: {}", remoteAddress, closeConsumer.getConsumerId());
    long requestId = closeConsumer.getRequestId();
    long consumerId = closeConsumer.getConsumerId();
    CompletableFuture<Consumer> consumerFuture = consumers.get(consumerId);
    if (consumerFuture == null) {
        log.warn("[{}] Consumer was not registered on the connection: {}", consumerId, remoteAddress);
        ctx.writeAndFlush(Commands.newError(requestId, ServerError.MetadataError, "Consumer not found"));
        return;
    }
    if (!consumerFuture.isDone() && consumerFuture.completeExceptionally(new IllegalStateException("Closed consumer before creation was complete"))) {
        // We have received a request to close the consumer before it was actually completed, we have marked the
        // consumer future as failed and we can tell the client the close operation was successful. When the actual
        // create operation will complete, the new consumer will be discarded.
        log.info("[{}] Closed consumer {} before its creation was completed", remoteAddress, consumerId);
        ctx.writeAndFlush(Commands.newSuccess(requestId));
        return;
    }
    if (consumerFuture.isCompletedExceptionally()) {
        log.info("[{}] Closed consumer {} that already failed to be created", remoteAddress, consumerId);
        ctx.writeAndFlush(Commands.newSuccess(requestId));
        return;
    }
    // Proceed with normal consumer close
    Consumer consumer = consumerFuture.getNow(null);
    try {
        consumer.close();
        consumers.remove(consumerId, consumerFuture);
        ctx.writeAndFlush(Commands.newSuccess(requestId));
        log.info("[{}] Closed consumer {}", remoteAddress, consumer);
    } catch (BrokerServiceException e) {
        log.warn("[{]] Error closing consumer: ", remoteAddress, consumer, e);
        ctx.writeAndFlush(Commands.newError(requestId, BrokerServiceException.getClientErrorCode(e), e.getMessage()));
    }
}
Also used : CommandCloseConsumer(org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseConsumer)

Example 2 with CommandCloseConsumer

use of org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseConsumer in project incubator-pulsar by apache.

the class Commands method newCloseConsumer.

public static ByteBuf newCloseConsumer(long consumerId, long requestId) {
    CommandCloseConsumer.Builder closeConsumerBuilder = CommandCloseConsumer.newBuilder();
    closeConsumerBuilder.setConsumerId(consumerId);
    closeConsumerBuilder.setRequestId(requestId);
    CommandCloseConsumer closeConsumer = closeConsumerBuilder.build();
    ByteBuf res = serializeWithSize(BaseCommand.newBuilder().setType(Type.CLOSE_CONSUMER).setCloseConsumer(closeConsumer));
    closeConsumerBuilder.recycle();
    closeConsumer.recycle();
    return res;
}
Also used : CommandCloseConsumer(org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseConsumer) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

CommandCloseConsumer (org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseConsumer)2 ByteBuf (io.netty.buffer.ByteBuf)1