Search in sources :

Example 76 with ConsumerConfiguration

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

the class PersistentTopicE2ETest method testConsumerFlowControl.

@Test
public void testConsumerFlowControl() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic2";
    final String subName = "sub2";
    Message msg;
    int recvQueueSize = 4;
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    conf.setReceiverQueueSize(recvQueueSize);
    Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
    Producer producer = pulsarClient.createProducer(topicName);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    assertNotNull(topicRef);
    PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
    assertNotNull(subRef);
    // 1. initial receive queue size recorded
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    assertEquals(getAvailablePermits(subRef), recvQueueSize);
    for (int i = 0; i < recvQueueSize / 2; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
        msg = consumer.receive();
        consumer.acknowledge(msg);
    }
    // 2. queue size re-adjusted after successful receive of half of window size
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    assertEquals(getAvailablePermits(subRef), recvQueueSize);
    consumer.close();
    assertFalse(subRef.getDispatcher().isConsumerConnected());
}
Also used : Message(com.yahoo.pulsar.client.api.Message) Consumer(com.yahoo.pulsar.client.api.Consumer) Producer(com.yahoo.pulsar.client.api.Producer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Example 77 with ConsumerConfiguration

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

the class PersistentTopicE2ETest method testSimpleCloseTopic.

@Test
public void testSimpleCloseTopic() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic5";
    final String subName = "sub5";
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
    Producer producer = pulsarClient.createProducer(topicName);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    assertNotNull(topicRef);
    PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
    assertNotNull(subRef);
    Message msg;
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
        msg = consumer.receive();
        consumer.acknowledge(msg);
    }
    producer.close();
    consumer.close();
    topicRef.close().get();
    assertNull(pulsar.getBrokerService().getTopicReference(topicName));
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) Producer(com.yahoo.pulsar.client.api.Producer) Message(com.yahoo.pulsar.client.api.Message) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Example 78 with ConsumerConfiguration

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

the class ResendRequestTest method testExclusiveSingleAckedPartitionedTopic.

@Test(timeOut = testTimeout)
public void testExclusiveSingleAckedPartitionedTopic() throws Exception {
    String key = "testExclusiveSingleAckedPartitionedTopic";
    final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
    final String subscriptionName = "my-ex-subscription-" + key;
    final String messagePredicate = "my-message-" + key + "-";
    final int totalMessages = 10;
    final int numberOfPartitions = 4;
    admin.persistentTopics().createPartitionedTopic(topicName, numberOfPartitions);
    // Special step to create partitioned topic
    // 1. producer connect
    ProducerConfiguration prodConfig = new ProducerConfiguration();
    prodConfig.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
    Producer producer = pulsarClient.createProducer(topicName, prodConfig);
    // 2. Create consumer
    ConsumerConfiguration consumerConfig = new ConsumerConfiguration();
    consumerConfig.setReceiverQueueSize(7);
    Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
    // 3. producer publish messages
    for (int i = 0; i < totalMessages; i++) {
        String message = messagePredicate + i;
        log.info("Message produced: " + message);
        producer.send(message.getBytes());
    }
    // 4. Receive messages
    Message message = consumer.receive();
    int messageCount = 0;
    log.info("Message received " + new String(message.getData()));
    do {
        messageCount += 1;
        log.info("Message received " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(messageCount, totalMessages);
    // 5. Ask for redeliver
    consumer.redeliverUnacknowledgedMessages();
    // 6. Check if Messages redelivered again
    message = consumer.receive();
    messageCount = 0;
    log.info("Message received " + new String(message.getData()));
    do {
        messageCount += 1;
        log.info("Message received " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(messageCount, totalMessages);
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) Consumer(com.yahoo.pulsar.client.api.Consumer) Message(com.yahoo.pulsar.client.api.Message) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) ProducerConfiguration(com.yahoo.pulsar.client.api.ProducerConfiguration) Test(org.testng.annotations.Test)

Example 79 with ConsumerConfiguration

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

the class ClientErrorsTest method subscribeFailAfterRetryTimeout.

private void subscribeFailAfterRetryTimeout(String topic) throws Exception {
    ClientConfiguration conf = new ClientConfiguration();
    conf.setOperationTimeout(200, TimeUnit.MILLISECONDS);
    PulsarClient client = PulsarClient.create("http://127.0.0.1:" + WEB_SERVICE_PORT, conf);
    final AtomicInteger counter = new AtomicInteger(0);
    mockBrokerService.setHandleSubscribe((ctx, subscribe) -> {
        if (counter.incrementAndGet() == 2) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            // do nothing
            }
        }
        ctx.writeAndFlush(Commands.newError(subscribe.getRequestId(), ServerError.ServiceNotReady, "msg"));
    });
    try {
        ConsumerConfiguration cConf = new ConsumerConfiguration();
        cConf.setSubscriptionType(SubscriptionType.Exclusive);
        client.subscribe(topic, "sub1", cConf);
        fail("Should have failed");
    } catch (Exception e) {
        // we fail even on the retriable error
        assertEquals(e.getClass(), LookupException.class);
    }
    mockBrokerService.resetHandleSubscribe();
    client.close();
}
Also used : LookupException(com.yahoo.pulsar.client.api.PulsarClientException.LookupException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration) LookupException(com.yahoo.pulsar.client.api.PulsarClientException.LookupException) ExecutionException(java.util.concurrent.ExecutionException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Example 80 with ConsumerConfiguration

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

the class ClientErrorsTest method testMockBrokerService.

@Test
public void testMockBrokerService() throws Exception {
    // test default actions of mock broker service
    try {
        PulsarClient client = PulsarClient.create("http://127.0.0.1:" + WEB_SERVICE_PORT);
        ConsumerConfiguration conf = new ConsumerConfiguration();
        conf.setSubscriptionType(SubscriptionType.Exclusive);
        Consumer consumer = client.subscribe("persistent://prop/use/ns/t1", "sub1", conf);
        Producer producer = client.createProducer("persistent://prop/use/ns/t1");
        Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
        producer.send("message".getBytes());
        Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
        consumer.unsubscribe();
        producer.close();
        consumer.close();
        client.close();
    } catch (Exception e) {
        fail("None of the mocked operations should throw a client side exception");
    }
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) Producer(com.yahoo.pulsar.client.api.Producer) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) LookupException(com.yahoo.pulsar.client.api.PulsarClientException.LookupException) ExecutionException(java.util.concurrent.ExecutionException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) Test(org.testng.annotations.Test)

Aggregations

ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)92 Test (org.testng.annotations.Test)82 Consumer (com.yahoo.pulsar.client.api.Consumer)74 Producer (com.yahoo.pulsar.client.api.Producer)51 Message (com.yahoo.pulsar.client.api.Message)47 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)26 PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)25 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)24 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)15 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)14 PersistentSubscription (com.yahoo.pulsar.broker.service.persistent.PersistentSubscription)12 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)11 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)8 CompletableFuture (java.util.concurrent.CompletableFuture)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 LookupException (com.yahoo.pulsar.client.api.PulsarClientException.LookupException)6 PersistentTopicStats (com.yahoo.pulsar.common.policies.data.PersistentTopicStats)6 Field (java.lang.reflect.Field)6