Search in sources :

Example 51 with MessageId

use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.

the class V1_ProducerConsumerTest method testMessageListener.

@Test(dataProvider = "batch", timeOut = 100000)
public void testMessageListener(int batchMessageDelayMs) throws Exception {
    log.info("-- Starting {} test --", methodName);
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    int numMessages = 100;
    final CountDownLatch latch = new CountDownLatch(numMessages);
    conf.setMessageListener((consumer, msg) -> {
        Assert.assertNotNull(msg, "Message cannot be null");
        String receivedMessage = new String(msg.getData());
        log.debug("Received message [{}] in the listener", receivedMessage);
        consumer.acknowledgeAsync(msg);
        latch.countDown();
    });
    Consumer consumer = pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic3", "my-subscriber-name", conf);
    ProducerConfiguration producerConf = new ProducerConfiguration();
    if (batchMessageDelayMs != 0) {
        producerConf.setBatchingMaxPublishDelay(batchMessageDelayMs, TimeUnit.MILLISECONDS);
        producerConf.setBatchingMaxMessages(5);
        producerConf.setBatchingEnabled(true);
    }
    Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic3", producerConf);
    List<Future<MessageId>> futures = Lists.newArrayList();
    // Asynchronously produce messages
    for (int i = 0; i < numMessages; i++) {
        final String message = "my-message-" + i;
        Future<MessageId> future = producer.sendAsync(message.getBytes());
        futures.add(future);
    }
    log.info("Waiting for async publish to complete");
    for (Future<MessageId> future : futures) {
        future.get();
    }
    log.info("Waiting for message listener to ack all messages");
    assertEquals(latch.await(numMessages, TimeUnit.SECONDS), true, "Timed out waiting for message listener acks");
    consumer.close();
    log.info("-- Exiting {} test --", methodName);
}
Also used : Consumer(org.apache.pulsar.client.api.Consumer) Producer(org.apache.pulsar.client.api.Producer) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) ProducerConfiguration(org.apache.pulsar.client.api.ProducerConfiguration) Future(java.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) CountDownLatch(java.util.concurrent.CountDownLatch) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 52 with MessageId

use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.

the class BatchMessageTest method testSimpleBatchProducerConsumer1kMessages.

@Test
public void testSimpleBatchProducerConsumer1kMessages() throws Exception {
    int numMsgs = 2000;
    int numMsgsInBatch = 4;
    final String topicName = "persistent://prop/use/ns-abc/testSimpleBatchProducerConsumer1kMessages";
    final String subscriptionName = "pc1k-sub-1";
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
    consumer.close();
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).maxPendingMessages(numMsgs + 1).batchingMaxPublishDelay(30, TimeUnit.SECONDS).batchingMaxMessages(numMsgsInBatch).enableBatching(true).create();
    List<CompletableFuture<MessageId>> sendFutureList = Lists.newArrayList();
    for (int i = 0; i < numMsgs; i++) {
        byte[] message = ("msg-" + i).getBytes();
        Message<byte[]> msg = MessageBuilder.create().setContent(message).build();
        sendFutureList.add(producer.sendAsync(msg));
    }
    FutureUtil.waitForAll(sendFutureList).get();
    int sendError = 0;
    for (CompletableFuture<MessageId> sendFuture : sendFutureList) {
        if (sendFuture.isCompletedExceptionally()) {
            ++sendError;
        }
    }
    if (sendError != 0) {
        LOG.warn("[{}] Error sending {} messages", subscriptionName, sendError);
        numMsgs = numMsgs - sendError;
    }
    LOG.info("[{}] sent {} messages", subscriptionName, numMsgs);
    PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    // allow stats to be updated..
    Thread.sleep(5000);
    LOG.info("[{}] checking backlog stats..");
    rolloverPerIntervalStats();
    assertEquals(topic.getSubscription(subscriptionName).getNumberOfEntriesInBacklog(), numMsgs / numMsgsInBatch);
    consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
    Message<byte[]> lastunackedMsg = null;
    for (int i = 0; i < numMsgs; i++) {
        Message<byte[]> msg = consumer.receive(1, TimeUnit.SECONDS);
        assertNotNull(msg);
        lastunackedMsg = msg;
    }
    if (lastunackedMsg != null) {
        consumer.acknowledgeCumulative(lastunackedMsg);
    }
    Thread.sleep(100);
    assertEquals(topic.getSubscription(subscriptionName).getNumberOfEntriesInBacklog(), 0);
    consumer.close();
    producer.close();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 53 with MessageId

use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.

the class MessageIdTest method partitionedProducerSendAsync.

@Test(timeOut = 10000)
public void partitionedProducerSendAsync() throws PulsarClientException, PulsarAdminException {
    // 1. Basic Config
    String key = "partitionedProducerSendAsync";
    final String topicName = "persistent://prop/cluster/namespace/topic-" + key;
    final String subscriptionName = "my-subscription-" + key;
    final String messagePredicate = "my-message-" + key + "-";
    final int numberOfMessages = 30;
    int numberOfPartitions = 3;
    admin.persistentTopics().createPartitionedTopic(topicName, numberOfPartitions);
    // 2. Create Producer
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    // 3. Create Consumer
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
    // 4. Publish message and get message id
    Set<MessageId> messageIds = new HashSet<>();
    Set<Future<MessageId>> futures = new HashSet<>();
    for (int i = 0; i < numberOfMessages; i++) {
        String message = messagePredicate + i;
        futures.add(producer.sendAsync(message.getBytes()));
    }
    futures.forEach(f -> {
        try {
            messageIds.add(f.get());
        } catch (Exception e) {
            Assert.fail("Failed to publish message, Exception: " + e.getMessage());
        }
    });
    // 4. Check if message Ids are correct
    log.info("Message IDs = " + messageIds);
    Assert.assertEquals(messageIds.size(), numberOfMessages, "Not all messages published successfully");
    for (int i = 0; i < numberOfMessages; i++) {
        MessageId messageId = consumer.receive().getMessageId();
        log.info("Message ID Received = " + messageId);
        Assert.assertTrue(messageIds.remove(messageId), "Failed to receive Message");
    }
    log.info("Message IDs = " + messageIds);
    Assert.assertEquals(messageIds.size(), 0, "Not all messages received successfully");
    consumer.unsubscribe();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) MessageId(org.apache.pulsar.client.api.MessageId) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 54 with MessageId

use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.

the class RawReaderTest method testSeekToMiddle.

@Test
public void testSeekToMiddle() throws Exception {
    int numKeys = 10;
    String topic = "persistent://my-property/use/my-ns/my-raw-topic";
    publishMessages(topic, numKeys);
    Set<String> readKeys = new HashSet<>();
    RawReader reader = RawReader.create(pulsarClient, topic, subscription).get();
    int i = 0;
    MessageId seekTo = null;
    MessageId lastMessageId = reader.getLastMessageIdAsync().get();
    while (true) {
        try (RawMessage m = reader.readNextAsync().get()) {
            i++;
            if (i > numKeys / 2) {
                if (seekTo == null) {
                    seekTo = m.getMessageId();
                }
                readKeys.add(extractKey(m));
            }
            if (lastMessageId.compareTo(m.getMessageId()) == 0) {
                break;
            }
        }
    }
    Assert.assertEquals(readKeys.size(), numKeys / 2);
    // seek to middle, read all keys again,
    // assert that we read all keys we had read previously
    reader.seekAsync(seekTo).get();
    while (true) {
        // should break out with TimeoutException
        try (RawMessage m = reader.readNextAsync().get()) {
            Assert.assertTrue(readKeys.remove(extractKey(m)));
            if (lastMessageId.compareTo(m.getMessageId()) == 0) {
                break;
            }
        }
    }
    Assert.assertTrue(readKeys.isEmpty());
}
Also used : RawReader(org.apache.pulsar.client.api.RawReader) RawMessage(org.apache.pulsar.client.api.RawMessage) HashSet(java.util.HashSet) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 55 with MessageId

use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.

the class RawReaderTest method testAcknowledgeWithProperties.

@Test
public void testAcknowledgeWithProperties() throws Exception {
    int numKeys = 10;
    String topic = "persistent://my-property/use/my-ns/my-raw-topic";
    Set<String> keys = publishMessages(topic, numKeys);
    RawReader reader = RawReader.create(pulsarClient, topic, subscription).get();
    MessageId lastMessageId = reader.getLastMessageIdAsync().get();
    while (true) {
        try (RawMessage m = reader.readNextAsync().get()) {
            Assert.assertTrue(keys.remove(extractKey(m)));
            if (lastMessageId.compareTo(m.getMessageId()) == 0) {
                break;
            }
        }
    }
    Assert.assertTrue(keys.isEmpty());
    Map<String, Long> properties = new HashMap<>();
    properties.put("foobar", 0xdeadbeefdecaL);
    reader.acknowledgeCumulativeAsync(lastMessageId, properties).get();
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topic);
    ManagedLedger ledger = topicRef.getManagedLedger();
    for (int i = 0; i < 30; i++) {
        if (ledger.openCursor(subscription).getProperties().get("foobar") == Long.valueOf(0xdeadbeefdecaL)) {
            break;
        }
        Thread.sleep(100);
    }
    Assert.assertEquals(ledger.openCursor(subscription).getProperties().get("foobar"), Long.valueOf(0xdeadbeefdecaL));
}
Also used : HashMap(java.util.HashMap) RawReader(org.apache.pulsar.client.api.RawReader) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) RawMessage(org.apache.pulsar.client.api.RawMessage) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

MessageId (org.apache.pulsar.client.api.MessageId)65 Test (org.testng.annotations.Test)42 CompletableFuture (java.util.concurrent.CompletableFuture)25 Message (org.apache.pulsar.client.api.Message)22 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)16 List (java.util.List)14 TimeUnit (java.util.concurrent.TimeUnit)14 Producer (org.apache.pulsar.client.api.Producer)14 Future (java.util.concurrent.Future)13 Consumer (org.apache.pulsar.client.api.Consumer)13 MessageIdImpl (org.apache.pulsar.client.impl.MessageIdImpl)13 ExecutorService (java.util.concurrent.ExecutorService)11 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 ByteBuf (io.netty.buffer.ByteBuf)10 HashSet (java.util.HashSet)10 Map (java.util.Map)10 Lists (com.google.common.collect.Lists)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8