Search in sources :

Example 41 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class ConsumerImpl method messageReceived.

void messageReceived(MessageIdData messageId, ByteBuf headersAndPayload, ClientCnx cnx) {
    if (log.isDebugEnabled()) {
        log.debug("[{}][{}] Received message: {}/{}", topic, subscription, messageId.getLedgerId(), messageId.getEntryId());
    }
    MessageMetadata msgMetadata = null;
    ByteBuf payload = headersAndPayload;
    if (!verifyChecksum(headersAndPayload, messageId)) {
        // discard message with checksum error
        discardCorruptedMessage(messageId, cnx, ValidationError.ChecksumMismatch);
        return;
    }
    try {
        msgMetadata = Commands.parseMessageMetadata(payload);
    } catch (Throwable t) {
        discardCorruptedMessage(messageId, cnx, ValidationError.ChecksumMismatch);
        return;
    }
    ByteBuf decryptedPayload = decryptPayloadIfNeeded(messageId, msgMetadata, payload, cnx);
    if (decryptedPayload == null) {
        // Message was discarded or CryptoKeyReader isn't implemented
        return;
    }
    ByteBuf uncompressedPayload = uncompressPayloadIfNeeded(messageId, msgMetadata, decryptedPayload, cnx);
    decryptedPayload.release();
    if (uncompressedPayload == null) {
        // Message was discarded on decompression error
        return;
    }
    final int numMessages = msgMetadata.getNumMessagesInBatch();
    if (numMessages == 1 && !msgMetadata.hasNumMessagesInBatch()) {
        final MessageImpl<T> message = new MessageImpl<>(messageId, msgMetadata, uncompressedPayload, getPartitionIndex(), cnx, schema);
        uncompressedPayload.release();
        msgMetadata.recycle();
        lock.readLock().lock();
        try {
            // Enqueue the message so that it can be retrieved when application calls receive()
            // if the conf.getReceiverQueueSize() is 0 then discard message if no one is waiting for it.
            // if asyncReceive is waiting then notify callback without adding to incomingMessages queue
            unAckedMessageTracker.add((MessageIdImpl) message.getMessageId());
            boolean asyncReceivedWaiting = !pendingReceives.isEmpty();
            if ((conf.getReceiverQueueSize() != 0 || waitingOnReceiveForZeroQueueSize) && !asyncReceivedWaiting) {
                incomingMessages.add(message);
            }
            if (asyncReceivedWaiting) {
                notifyPendingReceivedCallback(message, null);
            }
        } finally {
            lock.readLock().unlock();
        }
    } else {
        if (conf.getReceiverQueueSize() == 0) {
            log.warn("Closing consumer [{}]-[{}] due to unsupported received batch-message with zero receiver queue size", subscription, consumerName);
            // close connection
            closeAsync().handle((ok, e) -> {
                // notify callback with failure result
                notifyPendingReceivedCallback(null, new PulsarClientException.InvalidMessageException(format("Unsupported Batch message with 0 size receiver queue for [%s]-[%s] ", subscription, consumerName)));
                return null;
            });
        } else {
            // handle batch message enqueuing; uncompressed payload has all messages in batch
            receiveIndividualMessagesFromBatch(msgMetadata, uncompressedPayload, messageId, cnx);
        }
        uncompressedPayload.release();
        msgMetadata.recycle();
    }
    if (listener != null) {
        // Trigger the notification on the message listener in a separate thread to avoid blocking the networking
        // thread while the message processing happens
        listenerExecutor.execute(() -> {
            for (int i = 0; i < numMessages; i++) {
                try {
                    Message<T> msg = internalReceive(0, TimeUnit.MILLISECONDS);
                    // complete the callback-loop in case queue is cleared up
                    if (msg == null) {
                        if (log.isDebugEnabled()) {
                            log.debug("[{}] [{}] Message has been cleared from the queue", topic, subscription);
                        }
                        break;
                    }
                    try {
                        if (log.isDebugEnabled()) {
                            log.debug("[{}][{}] Calling message listener for message {}", topic, subscription, msg.getMessageId());
                        }
                        listener.received(ConsumerImpl.this, msg);
                    } catch (Throwable t) {
                        log.error("[{}][{}] Message listener error in processing message: {}", topic, subscription, msg.getMessageId(), t);
                    }
                } catch (PulsarClientException e) {
                    log.warn("[{}] [{}] Failed to dequeue the message for listener", topic, subscription, e);
                    return;
                }
            }
        });
    }
}
Also used : MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ByteBuf(io.netty.buffer.ByteBuf)

Example 42 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class ConsumerImpl method internalReceive.

@Override
protected Message<T> internalReceive() throws PulsarClientException {
    if (conf.getReceiverQueueSize() == 0) {
        checkArgument(zeroQueueLock != null, "Receiver queue size can't be modified");
        zeroQueueLock.writeLock().lock();
        try {
            return fetchSingleMessageFromBroker();
        } finally {
            zeroQueueLock.writeLock().unlock();
        }
    }
    Message<T> message;
    try {
        message = incomingMessages.take();
        messageProcessed(message);
        return message;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        stats.incrementNumReceiveFailed();
        throw new PulsarClientException(e);
    }
}
Also used : PulsarClientException(org.apache.pulsar.client.api.PulsarClientException)

Example 43 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class ConsumerImpl method seekAsync.

@Override
public CompletableFuture<Void> seekAsync(MessageId messageId) {
    if (getState() == State.Closing || getState() == State.Closed) {
        return FutureUtil.failedFuture(new PulsarClientException.AlreadyClosedException("Consumer was already closed"));
    }
    if (!isConnected()) {
        return FutureUtil.failedFuture(new PulsarClientException("Not connected to broker"));
    }
    final CompletableFuture<Void> seekFuture = new CompletableFuture<>();
    long requestId = client.newRequestId();
    MessageIdImpl msgId = (MessageIdImpl) messageId;
    ByteBuf seek = Commands.newSeek(consumerId, requestId, msgId.getLedgerId(), msgId.getEntryId());
    ClientCnx cnx = cnx();
    log.info("[{}][{}] Seek subscription to message id {}", topic, subscription, messageId);
    cnx.sendRequestWithId(seek, requestId).thenRun(() -> {
        log.info("[{}][{}] Successfully reset subscription to message id {}", topic, subscription, messageId);
        seekFuture.complete(null);
    }).exceptionally(e -> {
        log.error("[{}][{}] Failed to reset subscription: {}", topic, subscription, e.getCause().getMessage());
        seekFuture.completeExceptionally(e.getCause());
        return null;
    });
    return seekFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ByteBuf(io.netty.buffer.ByteBuf)

Example 44 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class ClientCnx method channelInactive.

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    super.channelInactive(ctx);
    log.info("{} Disconnected", ctx.channel());
    if (!connectionFuture.isDone()) {
        connectionFuture.completeExceptionally(new PulsarClientException("Connection already closed"));
    }
    PulsarClientException e = new PulsarClientException("Disconnected from server at " + ctx.channel().remoteAddress());
    // Fail out all the pending ops
    pendingRequests.forEach((key, future) -> future.completeExceptionally(e));
    pendingLookupRequests.forEach((key, future) -> future.completeExceptionally(e));
    pendingGetLastMessageIdRequests.forEach((key, future) -> future.completeExceptionally(e));
    pendingGetTopicsRequests.forEach((key, future) -> future.completeExceptionally(e));
    // Notify all attached producers/consumers so they have a chance to reconnect
    producers.forEach((id, producer) -> producer.connectionClosed(this));
    consumers.forEach((id, consumer) -> consumer.connectionClosed(this));
}
Also used : PulsarClientException(org.apache.pulsar.client.api.PulsarClientException)

Example 45 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class CmdConsume method run.

/**
 * Run the consume command.
 *
 * @return 0 for success, < 0 otherwise
 */
public int run() throws PulsarClientException, IOException {
    if (mainOptions.size() != 1)
        throw (new ParameterException("Please provide one and only one topic name."));
    if (this.subscriptionName == null || this.subscriptionName.isEmpty())
        throw (new ParameterException("Subscription name is not provided."));
    if (this.numMessagesToConsume < 0)
        throw (new ParameterException("Number of messages should be zero or positive."));
    String topic = this.mainOptions.get(0);
    int numMessagesConsumed = 0;
    int returnCode = 0;
    try {
        PulsarClient client = clientBuilder.build();
        Consumer<byte[]> consumer = client.newConsumer().topic(topic).subscriptionName(this.subscriptionName).subscriptionType(subscriptionType).subscribe();
        RateLimiter limiter = (this.consumeRate > 0) ? RateLimiter.create(this.consumeRate) : null;
        while (this.numMessagesToConsume == 0 || numMessagesConsumed < this.numMessagesToConsume) {
            if (limiter != null) {
                limiter.acquire();
            }
            Message<byte[]> msg = consumer.receive(5, TimeUnit.SECONDS);
            if (msg == null) {
                LOG.warn("No message to consume after waiting for 20 seconds.");
            } else {
                numMessagesConsumed += 1;
                System.out.println(MESSAGE_BOUNDARY);
                String output = this.interpretMessage(msg, displayHex);
                System.out.println(output);
                consumer.acknowledge(msg);
            }
        }
        client.close();
    } catch (Exception e) {
        LOG.error("Error while consuming messages");
        LOG.error(e.getMessage(), e);
        returnCode = -1;
    } finally {
        LOG.info("{} messages successfully consumed", numMessagesConsumed);
    }
    return returnCode;
}
Also used : ParameterException(com.beust.jcommander.ParameterException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) RateLimiter(com.google.common.util.concurrent.RateLimiter) ParameterException(com.beust.jcommander.ParameterException) IOException(java.io.IOException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException)

Aggregations

PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)65 Test (org.testng.annotations.Test)24 CompletableFuture (java.util.concurrent.CompletableFuture)17 Message (org.apache.pulsar.client.api.Message)15 IOException (java.io.IOException)14 PulsarClient (org.apache.pulsar.client.api.PulsarClient)13 ExecutionException (java.util.concurrent.ExecutionException)12 Consumer (org.apache.pulsar.client.api.Consumer)12 MessageId (org.apache.pulsar.client.api.MessageId)12 Producer (org.apache.pulsar.client.api.Producer)12 ByteBuf (io.netty.buffer.ByteBuf)11 List (java.util.List)8 ConsumerConfiguration (org.apache.pulsar.client.api.ConsumerConfiguration)8 ProducerConfiguration (org.apache.pulsar.client.api.ProducerConfiguration)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 Map (java.util.Map)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 TimeUnit (java.util.concurrent.TimeUnit)5