Search in sources :

Example 56 with MessageId

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

the class RawReaderTest method testRawReader.

@Test
public void testRawReader() 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());
}
Also used : RawReader(org.apache.pulsar.client.api.RawReader) 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)

Example 57 with MessageId

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

the class RawReaderTest method testBatchingRebatch.

@Test
public void testBatchingRebatch() throws Exception {
    String topic = "persistent://my-property/use/my-ns/my-raw-topic";
    try (Producer producer = pulsarClient.newProducer().topic(topic).maxPendingMessages(3).enableBatching(true).batchingMaxMessages(3).batchingMaxPublishDelay(1, TimeUnit.HOURS).create()) {
        producer.sendAsync(MessageBuilder.create().setKey("key1").setContent("my-content-1".getBytes()).build());
        producer.sendAsync(MessageBuilder.create().setKey("key2").setContent("my-content-2".getBytes()).build());
        producer.sendAsync(MessageBuilder.create().setKey("key3").setContent("my-content-3".getBytes()).build()).get();
    }
    RawReader reader = RawReader.create(pulsarClient, topic, subscription).get();
    try {
        RawMessage m1 = reader.readNextAsync().get();
        RawMessage m2 = RawBatchConverter.rebatchMessage(m1, (key, id) -> key.equals("key2")).get();
        List<ImmutablePair<MessageId, String>> idsAndKeys = RawBatchConverter.extractIdsAndKeys(m2);
        Assert.assertEquals(idsAndKeys.size(), 1);
        Assert.assertEquals(idsAndKeys.get(0).getRight(), "key2");
        m2.close();
    } finally {
        reader.closeAsync().get();
    }
}
Also used : RawMessage(org.apache.pulsar.client.api.RawMessage) Producer(org.apache.pulsar.client.api.Producer) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) RawReader(org.apache.pulsar.client.api.RawReader) Test(org.testng.annotations.Test) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) AfterMethod(org.testng.annotations.AfterMethod) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) Assert(org.testng.Assert) ByteBuf(io.netty.buffer.ByteBuf) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Map(java.util.Map) MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) Commands(org.apache.pulsar.common.api.Commands) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) CancellationException(java.util.concurrent.CancellationException) MessageBuilder(org.apache.pulsar.client.api.MessageBuilder) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest) List(java.util.List) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) MessageId(org.apache.pulsar.client.api.MessageId) Producer(org.apache.pulsar.client.api.Producer) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) RawReader(org.apache.pulsar.client.api.RawReader) RawMessage(org.apache.pulsar.client.api.RawMessage) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 58 with MessageId

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

the class RawReaderTest method testSeekToStart.

@Test
public void testSeekToStart() 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();
    MessageId lastMessageId = reader.getLastMessageIdAsync().get();
    while (true) {
        try (RawMessage m = reader.readNextAsync().get()) {
            readKeys.add(extractKey(m));
            if (lastMessageId.compareTo(m.getMessageId()) == 0) {
                break;
            }
        }
    }
    Assert.assertEquals(readKeys.size(), numKeys);
    // seek to start, read all keys again,
    // assert that we read all keys we had read previously
    reader.seekAsync(MessageId.earliest).get();
    while (true) {
        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 59 with MessageId

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

the class TopicTerminationTest method testDoubleTerminate.

@Test
public void testDoubleTerminate() throws Exception {
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    /* MessageId msgId1 = */
    producer.send("test-msg-1".getBytes());
    /* MessageId msgId2 = */
    producer.send("test-msg-2".getBytes());
    MessageId msgId3 = producer.send("test-msg-3".getBytes());
    MessageId lastMessageId = admin.persistentTopics().terminateTopicAsync(topicName).get();
    assertEquals(lastMessageId, msgId3);
    // Terminate it again
    lastMessageId = admin.persistentTopics().terminateTopicAsync(topicName).get();
    assertEquals(lastMessageId, msgId3);
}
Also used : MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 60 with MessageId

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

the class TopicTerminationTest method testSimpleTerminationConsumer.

@Test(timeOut = 20000)
public void testSimpleTerminationConsumer() throws Exception {
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    org.apache.pulsar.client.api.Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-sub").subscribe();
    MessageId msgId1 = producer.send("test-msg-1".getBytes());
    MessageId msgId2 = producer.send("test-msg-2".getBytes());
    Message<byte[]> msg1 = consumer.receive();
    assertEquals(msg1.getMessageId(), msgId1);
    consumer.acknowledge(msg1);
    MessageId msgId3 = producer.send("test-msg-3".getBytes());
    assertFalse(consumer.hasReachedEndOfTopic());
    MessageId lastMessageId = admin.persistentTopics().terminateTopicAsync(topicName).get();
    assertEquals(lastMessageId, msgId3);
    Message<byte[]> msg2 = consumer.receive();
    assertEquals(msg2.getMessageId(), msgId2);
    consumer.acknowledge(msg2);
    Message<byte[]> msg3 = consumer.receive();
    assertEquals(msg3.getMessageId(), msgId3);
    consumer.acknowledge(msg3);
    Message<byte[]> msg4 = consumer.receive(100, TimeUnit.MILLISECONDS);
    assertNull(msg4);
    Thread.sleep(100);
    assertTrue(consumer.hasReachedEndOfTopic());
}
Also used : MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

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