Java Code Examples for org.apache.pulsar.client.api.MessageId#compareTo()

The following examples show how to use org.apache.pulsar.client.api.MessageId#compareTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RawReaderTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testRawReader() throws Exception {
    int numKeys = 10;

    String topic = "persistent://my-property/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());
}
 
Example 2
Source File: UnAckedMessageTracker.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public int removeMessagesTill(MessageId msgId) {
    writeLock.lock();
    try {
        int removed = 0;
        Iterator<MessageId> iterator = messageIdPartitionMap.keySet().iterator();
        while (iterator.hasNext()) {
            MessageId messageId = iterator.next();
            if (messageId.compareTo(msgId) <= 0) {
                ConcurrentOpenHashSet<MessageId> exist = messageIdPartitionMap.get(messageId);
                if (exist != null) {
                    exist.remove(messageId);
                }
                iterator.remove();
                removed ++;
            }
        }
        return removed;
    } finally {
        writeLock.unlock();
    }
}
 
Example 3
Source File: RawReaderTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testSeekToStart() throws Exception {
    int numKeys = 10;
    String topic = "persistent://my-property/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());
}
 
Example 4
Source File: RawReaderTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAcknowledgeWithProperties() throws Exception {
    int numKeys = 10;

    String topic = "persistent://my-property/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).get();
    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));
}
 
Example 5
Source File: ConsumerImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private boolean hasMoreMessages(MessageId lastMessageIdInBroker, MessageId messageId, boolean inclusive) {
    if (inclusive && lastMessageIdInBroker.compareTo(messageId) >= 0 &&
            ((MessageIdImpl)lastMessageIdInBroker).getEntryId() != -1) {
        return true;
    }

    if (!inclusive && lastMessageIdInBroker.compareTo(messageId) > 0 &&
            ((MessageIdImpl)lastMessageIdInBroker).getEntryId() != -1) {
        return true;
    }

    return false;
}
 
Example 6
Source File: PersistentAcknowledgmentsGroupingTracker.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Since the ack are delayed, we need to do some best-effort duplicate check to discard messages that are being
 * resent after a disconnection and for which the user has already sent an acknowledgement.
 */
public boolean isDuplicate(MessageId messageId) {
    if (messageId.compareTo(lastCumulativeAck) <= 0) {
        // Already included in a cumulative ack
        return true;
    } else {
        return pendingIndividualAcks.contains(messageId);
    }
}
 
Example 7
Source File: RawReaderTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testSeekToMiddle() throws Exception {
    int numKeys = 10;
    String topic = "persistent://my-property/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());
}