Search in sources :

Example 31 with ProducerConfiguration

use of com.yahoo.pulsar.client.api.ProducerConfiguration 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 32 with ProducerConfiguration

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

the class ResendRequestTest method testSharedSingleAckedPartitionedTopic.

@Test(timeOut = testTimeout)
public void testSharedSingleAckedPartitionedTopic() throws Exception {
    String key = "testSharedSingleAckedPartitionedTopic";
    final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
    final String subscriptionName = "my-shared-subscription-" + key;
    final String messagePredicate = "my-message-" + key + "-";
    final int totalMessages = 10;
    final int numberOfPartitions = 3;
    admin.persistentTopics().createPartitionedTopic(topicName, numberOfPartitions);
    Random rn = new Random();
    // 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.Shared);
    Consumer consumer1 = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
    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 (rn.nextInt() % 3 == 0) {
                consumer1.acknowledge(message1);
                log.info("Consumer1 acked " + new String(message1.getData()));
                ackCount1 += 1;
            }
        }
        if (message2 != null) {
            log.info("Consumer2 received " + new String(message2.getData()));
            messageCount2 += 1;
            if (rn.nextInt() % 3 == 0) {
                consumer2.acknowledge(message2);
                log.info("Consumer2 acked " + new String(message2.getData()));
                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. Ask for redeliver
    log.info(key + ": Sent a Redeliver Message Request");
    consumer1.redeliverUnacknowledgedMessages();
    if ((ackCount1 + ackCount2) == totalMessages) {
        return;
    }
    // 6. Check if Messages redelivered again
    message1 = consumer1.receive(5000, TimeUnit.MILLISECONDS);
    message2 = consumer2.receive(5000, TimeUnit.MILLISECONDS);
    messageCount1 = 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;
        }
        message1 = consumer1.receive(1000, TimeUnit.MILLISECONDS);
        message2 = consumer2.receive(1000, 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 + ackCount1, totalMessages);
}
Also used : Random(java.util.Random) 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 33 with ProducerConfiguration

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

the class ResendRequestTest 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);
    Random rn = new Random();
    // 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.setConsumerName("Consumer-1");
    Consumer consumer1 = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
    consumerConfig.setConsumerName("Consumer-2");
    Consumer consumer2 = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
    Thread.sleep(1000);
    // 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 (rn.nextInt() % 3 == 0) {
                consumer1.acknowledge(message1);
                ackCount1 += 1;
            }
        }
        if (message2 != null) {
            log.info("Consumer2 received " + new String(message2.getData()));
            messageCount2 += 1;
            if (rn.nextInt() % 3 == 0) {
                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);
    if ((ackCount1 + ackCount2) == totalMessages) {
        return;
    }
    // 5. Ask for redeliver
    log.info(key + ": Sent a Redeliver Message Request");
    consumer1.redeliverUnacknowledgedMessages();
    consumer1.close();
    // 6. Check if Messages redelivered again
    message2 = consumer2.receive();
    messageCount1 = 0;
    do {
        if (message2 != null) {
            log.info("Consumer2 received " + new String(message2.getData()));
            messageCount2 += 1;
        }
        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(messageCount2 + ackCount1, totalMessages);
}
Also used : Random(java.util.Random) 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 34 with ProducerConfiguration

use of com.yahoo.pulsar.client.api.ProducerConfiguration 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 35 with ProducerConfiguration

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

the class SampleAsyncProducer method main.

public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
    PulsarClient pulsarClient = PulsarClient.create("http://127.0.0.1:8080");
    ProducerConfiguration conf = new ProducerConfiguration();
    conf.setSendTimeout(3, TimeUnit.SECONDS);
    Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic", conf);
    List<CompletableFuture<MessageId>> futures = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        final String content = "my-message-" + i;
        CompletableFuture<MessageId> future = producer.sendAsync(content.getBytes());
        future.handle((v, ex) -> {
            if (ex == null) {
                log.info("Message persisted: {}", content);
            } else {
                log.error("Error persisting message: {}", content, ex);
            }
            return null;
        });
        futures.add(future);
    }
    log.info("Waiting for async ops to complete");
    for (CompletableFuture<MessageId> future : futures) {
        future.join();
    }
    log.info("All operations completed");
    pulsarClient.close();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Producer(com.yahoo.pulsar.client.api.Producer) ProducerConfiguration(com.yahoo.pulsar.client.api.ProducerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) MessageId(com.yahoo.pulsar.client.api.MessageId)

Aggregations

ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)35 Test (org.testng.annotations.Test)32 Producer (com.yahoo.pulsar.client.api.Producer)30 Consumer (com.yahoo.pulsar.client.api.Consumer)25 Message (com.yahoo.pulsar.client.api.Message)20 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)13 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)10 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)7 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)6 MessageId (com.yahoo.pulsar.client.api.MessageId)4 BacklogQuota (com.yahoo.pulsar.common.policies.data.BacklogQuota)4 PersistentTopicStats (com.yahoo.pulsar.common.policies.data.PersistentTopicStats)4 Field (java.lang.reflect.Field)4 Random (java.util.Random)4 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)2 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)2 ProducerImpl (com.yahoo.pulsar.client.impl.ProducerImpl)2 PartitionedTopicStats (com.yahoo.pulsar.common.policies.data.PartitionedTopicStats)2