Micronaut Pulsar
Tags: java, micronaut, pulsar, apachepulsar
Published: 2021-12-11 20:32:36

A while ago I worked with Apache Pulsar and had interest in Micronaut. At the time there was no Micronaut Pulsar integration so I decided to give it a try. It was fun to go through certain Micronaut things and understand how and what to do but more than that I finally could contribute to open source project. Graeme Rocher is quite a nice guy I have to say. I expected more problems regarding coding style or something but it turns out if the tools pass the test you're quite OK with the contribution.

So to try to make it a bit more public I decided to publish a simple blog post about it. You can find the repository [here](https://github.com/micronaut-projects/micronaut-pulsar).

## What does it do?
It just makes integration between Micronaut framework and Apache Pulsar library. In the background it still uses Apache Pulsar client library for Java. Yes, that means it does still have some reflection usage in the background but I figured it's better to use existing piece of code than to rewrite whole protocol implementation in Java. I did test it against native image using Graal tools so I did some effort into making Pulsar client Java library work with it. It's not the best case scenario but it works so I guess it's good enough until either their library is rewritten to not use reflections or help is provided with rewriting this implementation.

## What's supported?
Just the "basics" so to speak. Configuring Apache Pulsar client with TLS, OAuth2, or just setting the local URL. Consumers and producers are the main focus so one could easily annotate classes/interfaces and their methods to consume/send messages. I did replace Pulsars object mapper with Micronaut one so you can rely on the same processing when using JSON, and thanks to [Johnas Conrad](https://github.com/yawkat) it's now replaced with new JsonMapper instead of ObjectMapper.

Annotations are made for consumers and producers but readers can have just annotation for injections - they will be automatically created for each injection point.

Protobuf mapping is not tested so the whole module is treated as snapshot - beta. If someone would like to give it a go feel free to fork and play with it. Other than that String, byte, JSON are well tested and working even Kotlin should be fine with suspend marked methods.

I assume anyone interested in this module understand Pulsars approach so I will not detail types of consumers or other things. I will just mention that most of the things are configurable through annotation properties like type of subscription, single topic, regex topic, acknowledgment timeout, and such. 

## Plans for the future
I wanted to build multi-tenant module which would allow reader, consumer, and producer injection point to detect tenant in context and instantiate or reuse existing one. This however would assume that producers and consumers that are made with annotations require a list in application properties or someplace to boot those consumers and readers during runtime. Otherwise it's not possible given that tenant headers are passed in requests. This would mean that consumers (or producers) would not be instantiated until request is made that would make that tenant known to the application. I think this is not usable so didn't implement it but let me know if you think otherwise.

Because module is in testing phase and I have no idea would be it be used enough I'd rather wait for requests and see where should effort be put it than to make code for something that is not used at all.

So, I'm hoping that this post would make it a bit more public and check interest in this module. If there's any I can get some feedback or maybe even couple of pull requests :D.

Given that I don't use Pulsar or Micronaut in my daily job anymore I don't have too much insights on what could be useful or what is bad so please do feel free to make suggestions or any kind of input.