I am trying to setup an asynchronous consumer for Apache Pulsar, but my problem is only 1 message is received and no other messages come through unless I restart my Spring Boot application. Unfortunately, the documentation around using the CompletableFuture with Pulsar is not great and I am new to using them.
My code below:
@EventListener(ApplicationReadyEvent.class)
public void subscribe() throws PulsarClientException {
Consumer consumer = this.pulsarClient.newConsumer(JSONSchema.of(TenantMicroserviceProvisioningRequestSchema.class))
.subscriptionName(this.pulsarSubscriptionName)
.topic(this.pulsarTopicName)
.ackTimeout(240, TimeUnit.SECONDS)
.subscriptionType(SubscriptionType.Exclusive)
.subscribe();
while(true) {
CompletableFuture<Message> asyncMessage = consumer.receiveAsync();
asyncMessage.thenAcceptAsync(incomingMessage -> {
TenantMicroserviceProvisioningRequestSchema schema = (TenantMicroserviceProvisioningRequestSchema) incomingMessage.getValue();
LOGGER.info(String.format("***New provisioning request recieved for tenant database [%s] with instance code [%s]", schema.getDatabaseName(), schema.getInstanceCode()));
consumer.acknowledgeAsync(incomingMessage.getMessageId());
});
}
}
In the Java Docs it does say "receiveAsync() should be called subsequently once returned CompletableFuture gets complete with received message. Else it creates backlog of receive requests in the application.", but I'm unsure of how to do this. I think this is causing the issue.
Docs: Pulsar Docs: https://pulsar.apache.org/docs/en/client-libraries-java/#async-receive Pulsar Java Doc for receieveAsync(): https://pulsar.apache.org/api/client/org/apache/pulsar/client/api/Consumer.html
*** UPDATE *** I've added the while loop, but when I do this my Spring Boot memory consumption floats up to 10 GB. Not sure, but wondering if it is how the future is setup.