Search in sources :

Example 86 with ConsumerConfiguration

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

the class ResendRequestTest method testExclusiveCumulativeAckedNormalTopic.

@Test(timeOut = testTimeout)
public void testExclusiveCumulativeAckedNormalTopic() throws Exception {
    String key = "testExclusiveCumulativeAckedNormalTopic";
    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;
    // 1. producer connect
    Producer producer = pulsarClient.createProducer(topicName);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    assertNotNull(topicRef);
    assertEquals(topicRef.getProducers().size(), 1);
    // 2. Create consumer
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setReceiverQueueSize(7);
    Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName, conf);
    // 3. producer publish messages
    for (int i = 0; i < totalMessages; i++) {
        String message = messagePredicate + i;
        producer.send(message.getBytes());
    }
    // 4. Receive messages
    Message message = consumer.receive();
    log.info("Message received " + new String(message.getData()));
    for (int i = 0; i < 7; i++) {
        printIncomingMessageQueue(consumer);
        message = consumer.receive();
        log.info("Message received " + new String(message.getData()));
    }
    consumer.redeliverUnacknowledgedMessages();
    Thread.sleep(1000);
    consumer.acknowledgeCumulative(message);
    do {
        message = consumer.receive(1000, TimeUnit.MILLISECONDS);
    } while (message != null);
    log.info("Consumer Requests Messages");
    consumer.redeliverUnacknowledgedMessages();
    int numOfReceives = 0;
    message = consumer.receive();
    do {
        numOfReceives += 1;
        log.info("Message received " + new String(message.getData()));
        message = consumer.receive(1000, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(numOfReceives, 2);
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) Consumer(com.yahoo.pulsar.client.api.Consumer) Message(com.yahoo.pulsar.client.api.Message) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) Test(org.testng.annotations.Test)

Example 87 with ConsumerConfiguration

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

the class UnAcknowledgedMessagesTimeoutTest method testFailoverSingleAckedPartitionedTopic.

@Test(timeOut = testTimeout)
public void testFailoverSingleAckedPartitionedTopic() throws Exception {
    String key = "testFailoverSingleAckedPartitionedTopic";
    final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
    final String subscriptionName = "my-failover-subscription-" + key;
    final String messagePredicate = "my-message-" + key + "-";
    final int totalMessages = 10;
    final int numberOfPartitions = 3;
    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);
    consumerConfig.setSubscriptionType(SubscriptionType.Failover);
    consumerConfig.setAckTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS);
    consumerConfig.setConsumerName("Consumer-1");
    Consumer consumer1 = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
    consumerConfig.setConsumerName("Consumer-2");
    Consumer consumer2 = 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 message1 = consumer1.receive();
    Message message2 = consumer2.receive();
    int messageCount1 = 0;
    int messageCount2 = 0;
    int ackCount1 = 0;
    int ackCount2 = 0;
    do {
        if (message1 != null) {
            log.info("Consumer1 received " + new String(message1.getData()));
            messageCount1 += 1;
        }
        if (message2 != null) {
            log.info("Consumer2 received " + new String(message2.getData()));
            messageCount2 += 1;
            consumer2.acknowledge(message2);
            ackCount2 += 1;
        }
        message1 = consumer1.receive(500, TimeUnit.MILLISECONDS);
        message2 = consumer2.receive(500, TimeUnit.MILLISECONDS);
    } while (message1 != null || message2 != null);
    log.info(key + " messageCount1 = " + messageCount1);
    log.info(key + " messageCount2 = " + messageCount2);
    log.info(key + " ackCount1 = " + ackCount1);
    log.info(key + " ackCount2 = " + ackCount2);
    assertEquals(messageCount1 + messageCount2, totalMessages);
    // 5. Check if Messages redelivered again
    // Since receive is a blocking call hoping that timeout will kick in
    log.info(key + " Timeout should be triggered now");
    message1 = consumer1.receive();
    messageCount1 = 0;
    do {
        if (message1 != null) {
            log.info("Consumer1 received " + new String(message1.getData()));
            messageCount1 += 1;
            consumer1.acknowledge(message1);
            ackCount1 += 1;
        }
        if (message2 != null) {
            log.info("Consumer2 received " + new String(message2.getData()));
            messageCount2 += 1;
        }
        message1 = consumer1.receive(500, TimeUnit.MILLISECONDS);
        message2 = consumer2.receive(500, TimeUnit.MILLISECONDS);
    } while (message1 != null || message2 != null);
    log.info(key + " messageCount1 = " + messageCount1);
    log.info(key + " messageCount2 = " + messageCount2);
    log.info(key + " ackCount1 = " + ackCount1);
    log.info(key + " ackCount2 = " + ackCount2);
    assertEquals(ackCount1 + messageCount2, 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 88 with ConsumerConfiguration

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

the class UnAcknowledgedMessagesTimeoutTest method testExclusiveSingleAckedNormalTopic.

@Test(timeOut = testTimeout)
public void testExclusiveSingleAckedNormalTopic() throws Exception {
    String key = "testExclusiveSingleAckedNormalTopic";
    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;
    // 1. producer connect
    Producer producer = pulsarClient.createProducer(topicName);
    // 2. Create consumer
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setReceiverQueueSize(7);
    conf.setAckTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS);
    Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName, conf);
    // 3. producer publish messages
    for (int i = 0; i < totalMessages / 2; i++) {
        String message = messagePredicate + i;
        log.info("Producer produced: " + message);
        producer.send(message.getBytes());
    }
    // 4. Receiver receives the message
    Message message = consumer.receive();
    while (message != null) {
        log.info("Consumer received : " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    }
    long size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, totalMessages / 2);
    // Blocking call, redeliver should kick in
    message = consumer.receive();
    log.info("Consumer received : " + new String(message.getData()));
    HashSet<String> hSet = new HashSet<>();
    for (int i = totalMessages / 2; i < totalMessages; i++) {
        String messageString = messagePredicate + i;
        producer.send(messageString.getBytes());
    }
    do {
        hSet.add(new String(message.getData()));
        consumer.acknowledge(message);
        log.info("Consumer acknowledged : " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    } while (message != null);
    size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 0);
    assertEquals(hSet.size(), 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) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 89 with ConsumerConfiguration

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

the class ZeroQueueSizeTest method zeroQueueSizeReceieveAsyncInCompatibility.

@Test(expectedExceptions = PulsarClientException.InvalidConfigurationException.class)
public void zeroQueueSizeReceieveAsyncInCompatibility() throws PulsarClientException {
    String key = "zeroQueueSizeReceieveAsyncInCompatibility";
    final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
    final String subscriptionName = "my-ex-subscription-" + key;
    ConsumerConfiguration configuration = new ConsumerConfiguration();
    configuration.setReceiverQueueSize(0);
    Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName, configuration);
    consumer.receive(10, TimeUnit.SECONDS);
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) Test(org.testng.annotations.Test)

Example 90 with ConsumerConfiguration

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

the class SampleConsumerListener method main.

public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
    PulsarClient pulsarClient = PulsarClient.create("http://localhost:8080");
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setMessageListener(new MessageListener() {

        public void received(Consumer consumer, Message msg) {
            log.info("Received message: {}", msg);
            consumer.acknowledgeAsync(msg);
        }
    });
    pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic", "my-subscriber-name", conf);
    // Block main thread
    System.in.read();
    pulsarClient.close();
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) Message(com.yahoo.pulsar.client.api.Message) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) MessageListener(com.yahoo.pulsar.client.api.MessageListener) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient)

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