Search in sources :

Example 16 with MessageId

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();
}
Also used : ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) Producer(com.yahoo.pulsar.client.api.Producer) Message(com.yahoo.pulsar.client.api.Message) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) MessageIdImpl(com.yahoo.pulsar.client.impl.MessageIdImpl) CountDownLatch(java.util.concurrent.CountDownLatch) MessageId(com.yahoo.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 17 with MessageId

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();
    ;
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) Consumer(com.yahoo.pulsar.client.api.Consumer) HashSet(java.util.HashSet) MessageId(com.yahoo.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 18 with MessageId

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();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Producer(com.yahoo.pulsar.client.api.Producer) ProducerConfiguration(com.yahoo.pulsar.client.api.ProducerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) MessageId(com.yahoo.pulsar.client.api.MessageId)

Aggregations

MessageId (com.yahoo.pulsar.client.api.MessageId)18 Test (org.testng.annotations.Test)14 Consumer (com.yahoo.pulsar.client.api.Consumer)12 Producer (com.yahoo.pulsar.client.api.Producer)11 Message (com.yahoo.pulsar.client.api.Message)10 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)7 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)6 HashSet (java.util.HashSet)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)4 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)3 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)3 MessageIdImpl (com.yahoo.pulsar.client.impl.MessageIdImpl)3 MessageMetadata (com.yahoo.pulsar.common.api.proto.PulsarApi.MessageMetadata)2 ByteBuf (io.netty.buffer.ByteBuf)2 IOException (java.io.IOException)2 Future (java.util.concurrent.Future)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Iterables (com.google.common.collect.Iterables)1