Java Code Examples for org.apache.pulsar.client.api.PulsarClientException#TimeoutException

The following examples show how to use org.apache.pulsar.client.api.PulsarClientException#TimeoutException . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageChunkingTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testPublishWithFailure() throws Exception {
    log.info("-- Starting {} test --", methodName);
    this.conf.setMaxMessageSize(5);
    final String topicName = "persistent://my-property/my-ns/my-topic1";

    ProducerBuilder<byte[]> producerBuilder = pulsarClient.newProducer().topic(topicName);

    Producer<byte[]> producer = producerBuilder.enableChunking(true).enableBatching(false)
            .create();

    stopBroker();

    try {
        producer.send(createMessagePayload(100).getBytes());
        fail("should have failed with timeout exception");
    } catch (PulsarClientException.TimeoutException e) {
        // Ok
    }
    producer.close();
}
 
Example 2
Source File: BrokerClientIntegrationTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * It verifies that if broker fails to complete producer/consumer operation then client times out rather waiting
 * forever.
 *
 * @throws PulsarClientException
 */
@Test(expectedExceptions = PulsarClientException.TimeoutException.class)
public void testOperationTimeout() throws PulsarClientException {
    final String topicName = "persistent://my-property/my-ns/my-topic1";
    ConcurrentOpenHashMap<String, CompletableFuture<Optional<Topic>>> topics = pulsar.getBrokerService()
            .getTopics();
    // non-complete topic future so, create topic should timeout
    topics.put(topicName, new CompletableFuture<>());
    PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(lookupUrl.toString())
            .operationTimeout(2, TimeUnit.SECONDS).statsInterval(0, TimeUnit.SECONDS).build();
    try {
        Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    } finally {
        topics.clear();
        pulsarClient.close();
    }
}
 
Example 3
Source File: BacklogQuotaManagerTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAheadProducerOnHold() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/quotahold"),
            ConfigHelper.backlogQuotaMap(config));
    admin.namespaces().setBacklogQuota("prop/quotahold",
            new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_request_hold));
    final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString())
            .statsInterval(0, TimeUnit.SECONDS).build();
    final String topic1 = "persistent://prop/quotahold/hold";
    final String subName1 = "c1hold";
    final int numMsgs = 10;

    Consumer<byte[]> consumer = client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();

    byte[] content = new byte[1024];
    Producer<byte[]> producer = client.newProducer().topic(topic1).sendTimeout(2, TimeUnit.SECONDS).create();
    for (int i = 0; i <= numMsgs; i++) {
        try {
            producer.send(content);
            LOG.info("sent [{}]", i);
        } catch (PulsarClientException.TimeoutException cte) {
            // producer close may cause a timeout on send
            LOG.info("timeout on [{}]", i);
        }
    }

    for (int i = 0; i < numMsgs; i++) {
        consumer.receive();
        LOG.info("received [{}]", i);
    }

    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
    rolloverStats();
    TopicStats stats = admin.topics().getStats(topic1);
    assertEquals(stats.publishers.size(), 0,
            "Number of producers on topic " + topic1 + " are [" + stats.publishers.size() + "]");
    client.close();
}
 
Example 4
Source File: BacklogQuotaManagerTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAheadProducerOnHoldTimeout() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/quotahold"),
            ConfigHelper.backlogQuotaMap(config));
    admin.namespaces().setBacklogQuota("prop/quotahold",
            new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_request_hold));
    final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString())
            .statsInterval(0, TimeUnit.SECONDS).build();
    final String topic1 = "persistent://prop/quotahold/holdtimeout";
    final String subName1 = "c1holdtimeout";
    boolean gotException = false;

    client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();

    byte[] content = new byte[1024];
    Producer<byte[]> producer = client.newProducer().topic(topic1).sendTimeout(2, TimeUnit.SECONDS).create();
    for (int i = 0; i < 10; i++) {
        producer.send(content);
    }

    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);

    try {
        // try to send over backlog quota and make sure it fails
        producer.send(content);
        producer.send(content);
        fail("backlog quota did not exceed");
    } catch (PulsarClientException.TimeoutException te) {
        gotException = true;
    }

    assertTrue(gotException, "timeout did not occur");
    client.close();
}
 
Example 5
Source File: ProducerImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
/**
 * Process sendTimeout events
 */
@Override
public void run(Timeout timeout) throws Exception {
    if (timeout.isCancelled()) {
        return;
    }

    long timeToWaitMs;

    synchronized (this) {
        // If it's closing/closed we need to ignore this timeout and not schedule next timeout.
        if (getState() == State.Closing || getState() == State.Closed) {
            return;
        }

        OpSendMsg firstMsg = pendingMessages.peek();
        if (firstMsg == null) {
            // If there are no pending messages, reset the timeout to the configured value.
            timeToWaitMs = conf.getSendTimeoutMs();
        } else {
            // If there is at least one message, calculate the diff between the message timeout and the elapsed
            // time since first message was created.
            long diff = conf.getSendTimeoutMs()
                    - TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - firstMsg.createdAt);
            if (diff <= 0) {
                // The diff is less than or equal to zero, meaning that the message has been timed out.
                // Set the callback to timeout on every message, then clear the pending queue.
                log.info("[{}] [{}] Message send timed out. Failing {} messages", topic, producerName,
                        pendingMessages.size());

                PulsarClientException te = new PulsarClientException.TimeoutException(
                    format("The producer %s can not send message to the topic %s within given timeout",
                        producerName, topic), firstMsg.sequenceId);
                failPendingMessages(cnx(), te);
                stats.incrementSendFailed(pendingMessages.size());
                // Since the pending queue is cleared now, set timer to expire after configured value.
                timeToWaitMs = conf.getSendTimeoutMs();
            } else {
                // The diff is greater than zero, set the timeout to the diff value
                timeToWaitMs = diff;
            }
        }

        sendTimeout = client.timer().newTimeout(this, timeToWaitMs, TimeUnit.MILLISECONDS);
    }
}