Search in sources :

Example 11 with RawMessage

use of org.apache.pulsar.client.api.RawMessage 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)

Example 12 with RawMessage

use of org.apache.pulsar.client.api.RawMessage 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 13 with RawMessage

use of org.apache.pulsar.client.api.RawMessage 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 14 with RawMessage

use of org.apache.pulsar.client.api.RawMessage 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 15 with RawMessage

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

the class CompactedTopicImpl method readEntries.

private static CompletableFuture<List<Entry>> readEntries(LedgerHandle lh, long from, long to) {
    CompletableFuture<Enumeration<LedgerEntry>> promise = new CompletableFuture<>();
    lh.asyncReadEntries(from, to, (rc, _lh, seq, ctx) -> {
        if (rc != BKException.Code.OK) {
            promise.completeExceptionally(BKException.create(rc));
        } else {
            promise.complete(seq);
        }
    }, null);
    return promise.thenApply((seq) -> {
        List<Entry> entries = new ArrayList<Entry>();
        while (seq.hasMoreElements()) {
            ByteBuf buf = seq.nextElement().getEntryBuffer();
            try (RawMessage m = RawMessageImpl.deserializeFrom(buf)) {
                entries.add(EntryImpl.create(m.getMessageIdData().getLedgerId(), m.getMessageIdData().getEntryId(), m.getHeadersAndPayload()));
            } finally {
                buf.release();
            }
        }
        return entries;
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Entry(org.apache.bookkeeper.mledger.Entry) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) Enumeration(java.util.Enumeration) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) RawMessage(org.apache.pulsar.client.api.RawMessage)

Aggregations

RawMessage (org.apache.pulsar.client.api.RawMessage)16 Test (org.testng.annotations.Test)11 RawReader (org.apache.pulsar.client.api.RawReader)10 ByteBuf (io.netty.buffer.ByteBuf)8 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)8 MessageId (org.apache.pulsar.client.api.MessageId)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Future (java.util.concurrent.Future)3 TimeoutException (java.util.concurrent.TimeoutException)3 BookKeeper (org.apache.bookkeeper.client.BookKeeper)3 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)3 MessageIdData (org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData)3 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2