use of org.apache.pulsar.client.api.Message in project incubator-pulsar by apache.
the class TopicTerminationTest method testSimpleTerminationReaderListener.
@Test(timeOut = 20000)
public void testSimpleTerminationReaderListener() throws Exception {
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
CountDownLatch latch = new CountDownLatch(1);
Reader<byte[]> reader = pulsarClient.newReader().topic(topicName).startMessageId(MessageId.latest).readerListener(new ReaderListener<byte[]>() {
@Override
public void received(Reader<byte[]> reader, Message<byte[]> msg) {
// do nothing
}
@Override
public void reachedEndOfTopic(Reader<byte[]> reader) {
latch.countDown();
assertTrue(reader.hasReachedEndOfTopic());
}
}).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());
Thread.sleep(100);
assertFalse(reader.hasReachedEndOfTopic());
MessageId lastMessageId = admin.persistentTopics().terminateTopicAsync(topicName).get();
assertEquals(lastMessageId, msgId3);
assertTrue(latch.await(3, TimeUnit.SECONDS));
assertTrue(reader.hasReachedEndOfTopic());
}
use of org.apache.pulsar.client.api.Message in project incubator-pulsar by apache.
the class AdminApiTest method persistentTopicsInvalidCursorReset.
@Test
public void persistentTopicsInvalidCursorReset() throws Exception {
admin.namespaces().setRetention("prop-xyz/use/ns1", new RetentionPolicies(10, 10));
assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
String topicName = "persistent://prop-xyz/use/ns1/invalidcursorreset";
// Force to create a topic
publishMessagesOnPersistentTopic(topicName, 0);
assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList(topicName));
// create consumer and subscription
URL pulsarUrl = new URL("http://127.0.0.1" + ":" + BROKER_WEBSERVICE_PORT);
PulsarClient client = PulsarClient.builder().serviceUrl(pulsarUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
Consumer<byte[]> consumer = client.newConsumer().topic(topicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Exclusive).subscribe();
assertEquals(admin.persistentTopics().getSubscriptions(topicName), Lists.newArrayList("my-sub"));
publishMessagesOnPersistentTopic(topicName, 10);
List<Message<byte[]>> messages = admin.persistentTopics().peekMessages(topicName, "my-sub", 10);
assertEquals(messages.size(), 10);
for (int i = 0; i < 10; i++) {
Message<byte[]> message = consumer.receive();
consumer.acknowledge(message);
}
// use invalid timestamp
try {
admin.persistentTopics().resetCursor(topicName, "my-sub", System.currentTimeMillis() - 190000);
} catch (Exception e) {
// fail the test
throw e;
}
admin.persistentTopics().resetCursor(topicName, "my-sub", System.currentTimeMillis() + 90000);
consumer = client.newConsumer().topic(topicName).subscriptionName("my-sub").subscribe();
consumer.close();
client.close();
admin.persistentTopics().deleteSubscription(topicName, "my-sub");
assertEquals(admin.persistentTopics().getSubscriptions(topicName), Lists.newArrayList());
admin.persistentTopics().delete(topicName);
}
Aggregations