Java Code Examples for org.apache.pulsar.client.api.Consumer#receiveAsync()

The following examples show how to use org.apache.pulsar.client.api.Consumer#receiveAsync() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: pulsar   File: V1_ProducerConsumerTest.java    License: Apache License 2.0 5 votes vote down vote up
private void receiveAsync(Consumer<byte[]> consumer, int totalMessage, int currentMessage, CountDownLatch latch,
        final Set<String> consumeMsg, ExecutorService executor) throws PulsarClientException {
    if (currentMessage < totalMessage) {
        CompletableFuture<Message<byte[]>> future = consumer.receiveAsync();
        future.handle((msg, exception) -> {
            if (exception == null) {
                // add message to consumer-queue to verify with produced messages
                consumeMsg.add(new String(msg.getData()));
                try {
                    consumer.acknowledge(msg);
                } catch (PulsarClientException e1) {
                    fail("message acknowledge failed", e1);
                }
                // consume next message
                executor.execute(() -> {
                    try {
                        receiveAsync(consumer, totalMessage, currentMessage + 1, latch, consumeMsg, executor);
                    } catch (PulsarClientException e) {
                        fail("message receive failed", e);
                    }
                });
                latch.countDown();
            }
            return null;
        });
    }
}
 
Example 2
Source Project: pulsar   File: NullValueTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void nullValueBytesSchemaTest() throws PulsarClientException {
    String topic = "persistent://prop/ns-abc/null-value-bytes-test";

    @Cleanup
    Producer producer = pulsarClient.newProducer()
            .topic(topic)
            .create();

    @Cleanup
    Consumer consumer = pulsarClient.newConsumer()
            .topic(topic)
            .subscriptionName("test")
            .subscribe();

    int numMessage = 10;
    for (int i = 0; i < numMessage; i++) {
        if (i % 2 == 0) {
            producer.newMessage().value("not null".getBytes()).send();
        } else {
            producer.newMessage().value(null).send();
        }
    }

    for (int i = 0; i < numMessage; i++) {
        Message message = consumer.receive();
        if (i % 2 == 0) {
            Assert.assertNotNull(message.getData());
            Assert.assertNotNull(message.getValue());
            Assert.assertEquals(new String(message.getData()), "not null");
        } else {
            Assert.assertNull(message.getData());
            Assert.assertNull(message.getValue());
        }
        consumer.acknowledge(message);
    }

    for (int i = 0; i < numMessage; i++) {
        if (i % 2 == 0) {
            producer.newMessage().value("not null".getBytes()).sendAsync();
        } else {
            producer.newMessage().value(null).sendAsync();
        }
    }

    for (int i = 0; i < numMessage; i++) {
        CompletableFuture<Message> completableFuture = consumer.receiveAsync();
        final int index = i;
        completableFuture.whenComplete((message, throwable) -> {
            Assert.assertNull(throwable);
            if (index % 2 == 0) {
                Assert.assertNotNull(message.getData());
                Assert.assertNotNull(message.getValue());
                Assert.assertEquals(new String(message.getData()), "not null");
            } else {
                Assert.assertNull(message.getData());
                Assert.assertNull(message.getValue());
            }
            try {
                consumer.acknowledge(message);
            } catch (PulsarClientException e) {
                Assert.assertNull(e);
            }
        });
    }

}