Search in sources :

Example 36 with Message

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

the class PerMessageUnAcknowledgedRedeliveryTest method testSharedAckedNormalTopic.

@Test(timeOut = testTimeout)
public void testSharedAckedNormalTopic() throws Exception {
    String key = "testSharedAckedNormalTopic";
    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 = 15;
    // 1. producer connect
    Producer producer = pulsarClient.createProducer(topicName);
    // 2. Create consumer
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setReceiverQueueSize(50);
    conf.setAckTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS);
    conf.setSubscriptionType(SubscriptionType.Shared);
    Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName, conf);
    // 3. producer publish messages
    for (int i = 0; i < totalMessages / 3; i++) {
        String message = messagePredicate + i;
        log.info("Producer produced: " + message);
        producer.send(message.getBytes());
    }
    // 4. Receiver receives the message, doesn't ack
    Message message = consumer.receive();
    while (message != null) {
        String data = new String(message.getData());
        log.info("Consumer received : " + data);
        message = consumer.receive(100, TimeUnit.MILLISECONDS);
    }
    long size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 5);
    // 5. producer publish more messages
    for (int i = 0; i < totalMessages / 3; i++) {
        String m = messagePredicate + i;
        log.info("Producer produced: " + m);
        producer.send(m.getBytes());
    }
    // 6. Receiver receives the message, ack them
    message = consumer.receive();
    int received = 0;
    while (message != null) {
        received++;
        String data = new String(message.getData());
        log.info("Consumer received : " + data);
        consumer.acknowledge(message);
        message = consumer.receive(100, TimeUnit.MILLISECONDS);
    }
    size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 5);
    assertEquals(received, 5);
    // 7. Simulate ackTimeout
    ((ConsumerImpl) consumer).getUnAckedMessageTracker().toggle();
    // 8. producer publish more messages
    for (int i = 0; i < totalMessages / 3; i++) {
        String m = messagePredicate + i;
        log.info("Producer produced: " + m);
        producer.send(m.getBytes());
    }
    // 9. Receiver receives the message, doesn't ack
    message = consumer.receive();
    while (message != null) {
        String data = new String(message.getData());
        log.info("Consumer received : " + data);
        message = consumer.receive(100, TimeUnit.MILLISECONDS);
    }
    size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 10);
    Thread.sleep(ackTimeOutMillis);
    // 10. Receiver receives redelivered messages
    message = consumer.receive();
    int redelivered = 0;
    while (message != null) {
        redelivered++;
        String data = new String(message.getData());
        log.info("Consumer received : " + data);
        consumer.acknowledge(message);
        message = consumer.receive(100, TimeUnit.MILLISECONDS);
    }
    assertEquals(redelivered, 5);
    size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 5);
}
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) Test(org.testng.annotations.Test)

Example 37 with Message

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

the class PerMessageUnAcknowledgedRedeliveryTest method testFailoverAckedNormalTopic.

@Test(timeOut = testTimeout)
public void testFailoverAckedNormalTopic() throws Exception {
    String key = "testFailoverAckedNormalTopic";
    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 = 15;
    // 1. producer connect
    Producer producer = pulsarClient.createProducer(topicName);
    // 2. Create consumer
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setReceiverQueueSize(50);
    conf.setAckTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS);
    conf.setSubscriptionType(SubscriptionType.Failover);
    Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName, conf);
    // 3. producer publish messages
    for (int i = 0; i < totalMessages / 3; i++) {
        String message = messagePredicate + i;
        log.info("Producer produced: " + message);
        producer.send(message.getBytes());
    }
    // 4. Receiver receives the message, doesn't ack
    Message message = consumer.receive();
    while (message != null) {
        String data = new String(message.getData());
        log.info("Consumer received : " + data);
        message = consumer.receive(100, TimeUnit.MILLISECONDS);
    }
    long size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 5);
    // 5. producer publish more messages
    for (int i = 0; i < totalMessages / 3; i++) {
        String m = messagePredicate + i;
        log.info("Producer produced: " + m);
        producer.send(m.getBytes());
    }
    // 6. Receiver receives the message, ack them
    message = consumer.receive();
    int received = 0;
    while (message != null) {
        received++;
        String data = new String(message.getData());
        log.info("Consumer received : " + data);
        consumer.acknowledge(message);
        message = consumer.receive(100, TimeUnit.MILLISECONDS);
    }
    size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 5);
    assertEquals(received, 5);
    // 7. Simulate ackTimeout
    ((ConsumerImpl) consumer).getUnAckedMessageTracker().toggle();
    // 8. producer publish more messages
    for (int i = 0; i < totalMessages / 3; i++) {
        String m = messagePredicate + i;
        log.info("Producer produced: " + m);
        producer.send(m.getBytes());
    }
    // 9. Receiver receives the message, doesn't ack
    message = consumer.receive();
    while (message != null) {
        String data = new String(message.getData());
        log.info("Consumer received : " + data);
        message = consumer.receive(100, TimeUnit.MILLISECONDS);
    }
    size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 10);
    Thread.sleep(ackTimeOutMillis);
    // 10. Receiver receives redelivered messages
    message = consumer.receive();
    int redelivered = 0;
    while (message != null) {
        redelivered++;
        String data = new String(message.getData());
        log.info("Consumer received : " + data);
        consumer.acknowledge(message);
        message = consumer.receive(100, TimeUnit.MILLISECONDS);
    }
    assertEquals(redelivered, 10);
    size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 0);
}
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) Test(org.testng.annotations.Test)

Example 38 with Message

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

the class PerMessageUnAcknowledgedRedeliveryTest method testSharedAckedPartitionedTopic.

@Test(timeOut = testTimeout)
public void testSharedAckedPartitionedTopic() throws Exception {
    String key = "testSharedAckedPartitionedTopic";
    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 = 15;
    final int numberOfPartitions = 3;
    admin.persistentTopics().createPartitionedTopic(topicName, numberOfPartitions);
    // 1. producer connect
    ProducerConfiguration prodConfig = new ProducerConfiguration();
    prodConfig.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
    Producer producer = pulsarClient.createProducer(topicName, prodConfig);
    // 2. Create consumer
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setReceiverQueueSize(50);
    conf.setAckTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS);
    conf.setSubscriptionType(SubscriptionType.Shared);
    Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName, conf);
    // 3. producer publish messages
    for (int i = 0; i < totalMessages / 3; i++) {
        String message = messagePredicate + i;
        log.info("Producer produced: " + message);
        producer.send(message.getBytes());
    }
    // 4. Receiver receives the message, doesn't ack
    Message message = consumer.receive();
    while (message != null) {
        String data = new String(message.getData());
        log.info("Consumer received : " + data);
        message = consumer.receive(100, TimeUnit.MILLISECONDS);
    }
    long size = getUnackedMessagesCountInPartitionedConsumer(consumer);
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 5);
    // 5. producer publish more messages
    for (int i = 0; i < totalMessages / 3; i++) {
        String m = messagePredicate + i;
        log.info("Producer produced: " + m);
        producer.send(m.getBytes());
    }
    // 6. Receiver receives the message, ack them
    message = consumer.receive();
    int received = 0;
    while (message != null) {
        received++;
        String data = new String(message.getData());
        log.info("Consumer received : " + data);
        consumer.acknowledge(message);
        message = consumer.receive(100, TimeUnit.MILLISECONDS);
    }
    size = getUnackedMessagesCountInPartitionedConsumer(consumer);
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 5);
    assertEquals(received, 5);
    // 7. Simulate ackTimeout
    ((PartitionedConsumerImpl) consumer).getConsumers().forEach(c -> c.getUnAckedMessageTracker().toggle());
    // 8. producer publish more messages
    for (int i = 0; i < totalMessages / 3; i++) {
        String m = messagePredicate + i;
        log.info("Producer produced: " + m);
        producer.send(m.getBytes());
    }
    // 9. Receiver receives the message, doesn't ack
    message = consumer.receive();
    while (message != null) {
        String data = new String(message.getData());
        log.info("Consumer received : " + data);
        message = consumer.receive(100, TimeUnit.MILLISECONDS);
    }
    size = getUnackedMessagesCountInPartitionedConsumer(consumer);
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 10);
    Thread.sleep(ackTimeOutMillis);
    // 10. Receiver receives redelivered messages
    message = consumer.receive();
    int redelivered = 0;
    while (message != null) {
        redelivered++;
        String data = new String(message.getData());
        log.info("Consumer received : " + data);
        consumer.acknowledge(message);
        message = consumer.receive(100, TimeUnit.MILLISECONDS);
    }
    assertEquals(redelivered, 5);
    size = getUnackedMessagesCountInPartitionedConsumer(consumer);
    log.info(key + " Unacked Message Tracker size is " + size);
    assertEquals(size, 5);
}
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 39 with Message

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

the class UnAcknowledgedMessagesTimeoutTest method testCheckUnAcknowledgedMessageTimer.

@Test(timeOut = testTimeout)
public void testCheckUnAcknowledgedMessageTimer() throws PulsarClientException, InterruptedException {
    String key = "testCheckUnAcknowledgedMessageTimer";
    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 = 3;
    // 1. producer connect
    Producer producer = pulsarClient.createProducer(topicName);
    // 2. Create consumer
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setReceiverQueueSize(7);
    conf.setAckTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS);
    ConsumerImpl consumer = (ConsumerImpl) pulsarClient.subscribe(topicName, subscriptionName, conf);
    // 3. producer publish messages
    for (int i = 0; i < totalMessages; i++) {
        String message = messagePredicate + i;
        log.info("Producer produced: " + message);
        producer.send(message.getBytes());
    }
    Thread.sleep((long) (ackTimeOutMillis * 1.1));
    for (int i = 0; i < totalMessages - 1; i++) {
        Message msg = consumer.receive();
        consumer.acknowledge(msg);
    }
    assertEquals(consumer.getUnAckedMessageTracker().size(), 1);
    Message msg = consumer.receive();
    consumer.acknowledge(msg);
    assertEquals(consumer.getUnAckedMessageTracker().size(), 0);
    Thread.sleep((long) (ackTimeOutMillis * 1.1));
    assertEquals(consumer.getUnAckedMessageTracker().size(), 0);
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) Message(com.yahoo.pulsar.client.api.Message) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) Test(org.testng.annotations.Test)

Example 40 with Message

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

the class UnAcknowledgedMessagesTimeoutTest 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);
    // 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; i++) {
        String message = messagePredicate + i;
        producer.send(message.getBytes());
    }
    // 4. Receiver receives the message
    HashSet<String> hSet = new HashSet<>();
    Message message = consumer.receive();
    Message lastMessage = message;
    while (message != null) {
        lastMessage = message;
        hSet.add(new String(message.getData()));
        log.info("Consumer received " + new String(message.getData()));
        log.info("Message ID details " + ((MessageIdImpl) message.getMessageId()).toString());
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    }
    long size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
    assertEquals(size, totalMessages);
    log.info("Comulative Ack sent for " + new String(lastMessage.getData()));
    log.info("Message ID details " + ((MessageIdImpl) lastMessage.getMessageId()).toString());
    consumer.acknowledgeCumulative(lastMessage);
    size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
    assertEquals(size, 0);
    message = consumer.receive((int) (2 * ackTimeOutMillis), TimeUnit.MILLISECONDS);
    assertEquals(message, null);
}
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)

Aggregations

Message (com.yahoo.pulsar.client.api.Message)90 Test (org.testng.annotations.Test)66 Consumer (com.yahoo.pulsar.client.api.Consumer)61 Producer (com.yahoo.pulsar.client.api.Producer)57 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)47 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)31 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)25 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)22 CompletableFuture (java.util.concurrent.CompletableFuture)17 PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)10 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)9 MessageId (com.yahoo.pulsar.client.api.MessageId)9 PersistentSubscription (com.yahoo.pulsar.broker.service.persistent.PersistentSubscription)8 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)6 Field (java.lang.reflect.Field)6 HashSet (java.util.HashSet)6 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)5 RetentionPolicies (com.yahoo.pulsar.common.policies.data.RetentionPolicies)5 Tuple (backtype.storm.tuple.Tuple)4 ParameterException (com.beust.jcommander.ParameterException)4