use of com.yahoo.pulsar.client.api.MessageId in project pulsar by yahoo.
the class PersistentTopicE2ETest method testProducerReturnedMessageId.
@Test
public void testProducerReturnedMessageId() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic-xyz";
// 1. producer connect
Producer producer = pulsarClient.createProducer(topicName);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
assertEquals(topicRef.getProducers().size(), 1);
ManagedLedgerImpl managedLedger = (ManagedLedgerImpl) topicRef.getManagedLedger();
long ledgerId = managedLedger.getLedgersInfoAsList().get(0).getLedgerId();
// 2. producer publish messages
final int SyncMessages = 10;
for (int i = 0; i < SyncMessages; i++) {
String message = "my-message-" + i;
MessageId receivedMessageId = producer.send(message.getBytes());
assertEquals(receivedMessageId, new MessageIdImpl(ledgerId, i, -1));
}
// 3. producer publish messages async
final int AsyncMessages = 10;
final CountDownLatch counter = new CountDownLatch(AsyncMessages);
for (int i = SyncMessages; i < (SyncMessages + AsyncMessages); i++) {
String content = "my-message-" + i;
Message msg = MessageBuilder.create().setContent(content.getBytes()).build();
final int index = i;
producer.sendAsync(msg).thenRun(() -> {
assertEquals(msg.getMessageId(), new MessageIdImpl(ledgerId, index, -1));
counter.countDown();
}).exceptionally((ex) -> {
return null;
});
}
counter.await();
// 4. producer disconnect
producer.close();
}
use of com.yahoo.pulsar.client.api.MessageId in project pulsar by yahoo.
the class MessageIdTest method producerSend.
@Test(timeOut = 10000)
public void producerSend() throws PulsarClientException {
// 1. Basic Config
String key = "producerSend";
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;
// 2. Create Producer
Producer producer = pulsarClient.createProducer(topicName);
// 3. Create Consumer
Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName);
// 4. Publish message and get message id
Set<MessageId> messageIds = new HashSet();
for (int i = 0; i < numberOfMessages; i++) {
String message = messagePredicate + i;
messageIds.add(producer.send(message.getBytes()));
}
// 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++) {
Assert.assertTrue(messageIds.remove(consumer.receive().getMessageId()), "Failed to receive Message");
}
log.info("Message IDs = " + messageIds);
Assert.assertEquals(messageIds.size(), 0, "Not all messages received successfully");
consumer.unsubscribe();
;
}
use of com.yahoo.pulsar.client.api.MessageId in project pulsar by yahoo.
the class SampleAsyncProducer method main.
public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
PulsarClient pulsarClient = PulsarClient.create("http://127.0.0.1:8080");
ProducerConfiguration conf = new ProducerConfiguration();
conf.setSendTimeout(3, TimeUnit.SECONDS);
Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic", conf);
List<CompletableFuture<MessageId>> futures = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
final String content = "my-message-" + i;
CompletableFuture<MessageId> future = producer.sendAsync(content.getBytes());
future.handle((v, ex) -> {
if (ex == null) {
log.info("Message persisted: {}", content);
} else {
log.error("Error persisting message: {}", content, ex);
}
return null;
});
futures.add(future);
}
log.info("Waiting for async ops to complete");
for (CompletableFuture<MessageId> future : futures) {
future.join();
}
log.info("All operations completed");
pulsarClient.close();
}
Aggregations