Search in sources :

Example 36 with PulsarClientException

use of com.yahoo.pulsar.client.api.PulsarClientException in project pulsar by yahoo.

the class CmdProduce method run.

/**
     * Run the producer.
     *
     * @return 0 for success, < 0 otherwise
     * @throws Exception
     */
public int run() throws PulsarClientException {
    if (mainOptions.size() != 1)
        throw (new ParameterException("Please provide one and only one topic name."));
    if (this.serviceURL == null || this.serviceURL.isEmpty())
        throw (new ParameterException("Broker URL is not provided."));
    if (this.numTimesProduce <= 0)
        throw (new ParameterException("Number of times need to be positive number."));
    if (messages.size() == 0 && messageFileNames.size() == 0)
        throw (new ParameterException("Please supply message content with either --messages or --files"));
    int totalMessages = (messages.size() + messageFileNames.size()) * numTimesProduce;
    if (totalMessages > MAX_MESSAGES) {
        String msg = "Attempting to send " + totalMessages + " messages. Please do not send more than " + MAX_MESSAGES + " messages";
        throw new ParameterException(msg);
    }
    String topic = this.mainOptions.get(0);
    int numMessagesSent = 0;
    int returnCode = 0;
    try {
        PulsarClient client = PulsarClient.create(this.serviceURL, this.clientConfig);
        Producer producer = client.createProducer(topic);
        List<byte[]> messageBodies = generateMessageBodies(this.messages, this.messageFileNames);
        RateLimiter limiter = (this.publishRate > 0) ? RateLimiter.create(this.publishRate) : null;
        for (int i = 0; i < this.numTimesProduce; i++) {
            List<Message> messages = generateMessages(messageBodies);
            for (Message msg : messages) {
                if (limiter != null)
                    limiter.acquire();
                producer.send(msg);
                numMessagesSent++;
            }
        }
        client.close();
    } catch (Exception e) {
        LOG.error("Error while producing messages");
        LOG.error(e.getMessage(), e);
        returnCode = -1;
    } finally {
        LOG.info("{} messages successfully produced", numMessagesSent);
    }
    return returnCode;
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) Message(com.yahoo.pulsar.client.api.Message) ParameterException(com.beust.jcommander.ParameterException) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) RateLimiter(com.google.common.util.concurrent.RateLimiter) ParameterException(com.beust.jcommander.ParameterException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Example 37 with PulsarClientException

use of com.yahoo.pulsar.client.api.PulsarClientException in project pulsar by yahoo.

the class PulsarSpout method nextTuple.

/**
     * Emits a tuple received from the Pulsar consumer unless there are any failed messages
     */
@Override
public void nextTuple() {
    Message msg;
    // check if there are any failed messages to re-emit in the topology
    msg = failedMessages.peek();
    if (msg != null) {
        MessageRetries messageRetries = pendingMessageRetries.get(msg.getMessageId());
        if (Backoff.shouldBackoff(messageRetries.getTimeStamp(), TimeUnit.NANOSECONDS, messageRetries.getNumRetries())) {
            Utils.sleep(100);
        } else {
            // remove the message from the queue and emit to the topology, only if it should not be backedoff
            LOG.info("[{}] Retrying failed message {}", spoutId, msg.getMessageId());
            failedMessages.remove();
            mapToValueAndEmit(msg);
        }
        return;
    }
    // receive from consumer if no failed messages
    if (consumer != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("[{}] Receiving the next message from pulsar consumer to emit to the collector", spoutId);
        }
        try {
            msg = consumer.receive(1, TimeUnit.SECONDS);
            if (msg != null) {
                ++messagesReceived;
                messageSizeReceived += msg.getData().length;
            }
            mapToValueAndEmit(msg);
        } catch (PulsarClientException e) {
            LOG.error("[{}] Error receiving message from pulsar consumer", spoutId, e);
        }
    }
}
Also used : Message(com.yahoo.pulsar.client.api.Message) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Aggregations

PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)37 Message (com.yahoo.pulsar.client.api.Message)18 Test (org.testng.annotations.Test)14 Consumer (com.yahoo.pulsar.client.api.Consumer)12 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)12 Producer (com.yahoo.pulsar.client.api.Producer)11 CompletableFuture (java.util.concurrent.CompletableFuture)11 PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)7 ByteBuf (io.netty.buffer.ByteBuf)6 IOException (java.io.IOException)5 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)4 PersistentSubscription (com.yahoo.pulsar.broker.service.persistent.PersistentSubscription)3 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)3 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)3 MessageId (com.yahoo.pulsar.client.api.MessageId)3 ProducerConsumerBase (com.yahoo.pulsar.client.api.ProducerConsumerBase)3 DoubleByteBuf (com.yahoo.pulsar.common.api.DoubleByteBuf)3 MessageMetadata (com.yahoo.pulsar.common.api.proto.PulsarApi.MessageMetadata)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ParameterException (com.beust.jcommander.ParameterException)2