use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class V1_ProducerConsumerTest method testMessageListener.
@Test(dataProvider = "batch", timeOut = 100000)
public void testMessageListener(int batchMessageDelayMs) throws Exception {
log.info("-- Starting {} test --", methodName);
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
int numMessages = 100;
final CountDownLatch latch = new CountDownLatch(numMessages);
conf.setMessageListener((consumer, msg) -> {
Assert.assertNotNull(msg, "Message cannot be null");
String receivedMessage = new String(msg.getData());
log.debug("Received message [{}] in the listener", receivedMessage);
consumer.acknowledgeAsync(msg);
latch.countDown();
});
Consumer consumer = pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic3", "my-subscriber-name", conf);
ProducerConfiguration producerConf = new ProducerConfiguration();
if (batchMessageDelayMs != 0) {
producerConf.setBatchingMaxPublishDelay(batchMessageDelayMs, TimeUnit.MILLISECONDS);
producerConf.setBatchingMaxMessages(5);
producerConf.setBatchingEnabled(true);
}
Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic3", producerConf);
List<Future<MessageId>> futures = Lists.newArrayList();
// Asynchronously produce messages
for (int i = 0; i < numMessages; i++) {
final String message = "my-message-" + i;
Future<MessageId> future = producer.sendAsync(message.getBytes());
futures.add(future);
}
log.info("Waiting for async publish to complete");
for (Future<MessageId> future : futures) {
future.get();
}
log.info("Waiting for message listener to ack all messages");
assertEquals(latch.await(numMessages, TimeUnit.SECONDS), true, "Timed out waiting for message listener acks");
consumer.close();
log.info("-- Exiting {} test --", methodName);
}
use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class BatchMessageTest method testSimpleBatchProducerConsumer1kMessages.
@Test
public void testSimpleBatchProducerConsumer1kMessages() throws Exception {
int numMsgs = 2000;
int numMsgsInBatch = 4;
final String topicName = "persistent://prop/use/ns-abc/testSimpleBatchProducerConsumer1kMessages";
final String subscriptionName = "pc1k-sub-1";
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
consumer.close();
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).maxPendingMessages(numMsgs + 1).batchingMaxPublishDelay(30, TimeUnit.SECONDS).batchingMaxMessages(numMsgsInBatch).enableBatching(true).create();
List<CompletableFuture<MessageId>> sendFutureList = Lists.newArrayList();
for (int i = 0; i < numMsgs; i++) {
byte[] message = ("msg-" + i).getBytes();
Message<byte[]> msg = MessageBuilder.create().setContent(message).build();
sendFutureList.add(producer.sendAsync(msg));
}
FutureUtil.waitForAll(sendFutureList).get();
int sendError = 0;
for (CompletableFuture<MessageId> sendFuture : sendFutureList) {
if (sendFuture.isCompletedExceptionally()) {
++sendError;
}
}
if (sendError != 0) {
LOG.warn("[{}] Error sending {} messages", subscriptionName, sendError);
numMsgs = numMsgs - sendError;
}
LOG.info("[{}] sent {} messages", subscriptionName, numMsgs);
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
// allow stats to be updated..
Thread.sleep(5000);
LOG.info("[{}] checking backlog stats..");
rolloverPerIntervalStats();
assertEquals(topic.getSubscription(subscriptionName).getNumberOfEntriesInBacklog(), numMsgs / numMsgsInBatch);
consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
Message<byte[]> lastunackedMsg = null;
for (int i = 0; i < numMsgs; i++) {
Message<byte[]> msg = consumer.receive(1, TimeUnit.SECONDS);
assertNotNull(msg);
lastunackedMsg = msg;
}
if (lastunackedMsg != null) {
consumer.acknowledgeCumulative(lastunackedMsg);
}
Thread.sleep(100);
assertEquals(topic.getSubscription(subscriptionName).getNumberOfEntriesInBacklog(), 0);
consumer.close();
producer.close();
}
use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class MessageIdTest method partitionedProducerSendAsync.
@Test(timeOut = 10000)
public void partitionedProducerSendAsync() throws PulsarClientException, PulsarAdminException {
// 1. Basic Config
String key = "partitionedProducerSendAsync";
final String topicName = "persistent://prop/cluster/namespace/topic-" + key;
final String subscriptionName = "my-subscription-" + key;
final String messagePredicate = "my-message-" + key + "-";
final int numberOfMessages = 30;
int numberOfPartitions = 3;
admin.persistentTopics().createPartitionedTopic(topicName, numberOfPartitions);
// 2. Create Producer
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
// 3. Create Consumer
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
// 4. Publish message and get message id
Set<MessageId> messageIds = new HashSet<>();
Set<Future<MessageId>> futures = new HashSet<>();
for (int i = 0; i < numberOfMessages; i++) {
String message = messagePredicate + i;
futures.add(producer.sendAsync(message.getBytes()));
}
futures.forEach(f -> {
try {
messageIds.add(f.get());
} catch (Exception e) {
Assert.fail("Failed to publish message, Exception: " + e.getMessage());
}
});
// 4. Check if message Ids are correct
log.info("Message IDs = " + messageIds);
Assert.assertEquals(messageIds.size(), numberOfMessages, "Not all messages published successfully");
for (int i = 0; i < numberOfMessages; i++) {
MessageId messageId = consumer.receive().getMessageId();
log.info("Message ID Received = " + messageId);
Assert.assertTrue(messageIds.remove(messageId), "Failed to receive Message");
}
log.info("Message IDs = " + messageIds);
Assert.assertEquals(messageIds.size(), 0, "Not all messages received successfully");
consumer.unsubscribe();
}
use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class RawReaderTest method testSeekToMiddle.
@Test
public void testSeekToMiddle() 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();
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());
}
use of org.apache.pulsar.client.api.MessageId 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));
}
Aggregations