A Microservice Connector Method is the place where the data hand-off occurs between the RabbitMQ Client and the Server, where the two modules connect. We're going to look at how to reduce some of the code required to pass the data from RabbitMQ Client to Server in this example taken from the NixMash Blog Microservices App.
NixMash Microservices consists of a Web Module and three Microservices, a REST Post Microservice and two other Microservices, Util and Solr which communicate through RabbitMQ Message Queuing. We're going to replace two Connector Methods in the Post Microservice with a generic method to handle all one-way data handoffs to our Util Microservice. RPC data exchanges occur elsewhere.
Here are the two separate Connector Methods, where Java POJOs are converted to Byte Arrays, a Queue Task is added to the RabbitMQ Message Envelope Header, and the data sent to the RabbitMQ Message Queue for processing by the Util Microservice. Notice the duplicate code. A task is assigned at (1) which is different between the two methods, but at (2) we see they both end with publishing a Byte Array to the Message Queue. One other point of difference is that they take a different POJO, a JsonPost object and in the second a ClientInfo.
Let's first address the POJO Parameter and consider its final state as a Byte Array. Our new generic Connector Method (which we'll see in a minute) is callBqUtilQueue(). Rather than create the Byte Array in the method we'll do it prior to calling the method. The only real difference in the logic of the two methods we saw above is the assignment of a task, so we'll do that outside of the method as well. (1)
Our Reusable RabbitMQ Connector Method
And what you've been waiting for, our reusable RabbitMQ Connector Method where we pass our Byte Array and Queue Task to callBqUtilQueue().
Working in a Microservices Environment requires more coding, much in the way of Connector Methods that exchange data between Microservices. Designing them as reusable makes life a bit easier.