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());
}
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();
}
}
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());
}
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);
}
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());
}
Aggregations