Suppose that I have a single Spring Boot application that needs to instantiate several MQTT clients (# ~10^4) to virtualize physical devices into a broker (Mosquitto). Each of them has a unique client ID, in order to get/send messages from/to dedicated topics.
I'm trying to achieve this using MQTT inbound/outbound Integration Flows in Spring Boot (spring-integration-mqtt-5.1.6.RELEASE), but each flow (MqttPahoMessageDrivenChannelAdapter/MqttPahoMessageHandler
) instantiates its own client to manage the incoming/outgoing messages.
My concerns are:
- By definition, I cannot use just one client for the in/out flow due to the Spring Boot MQTT integration flows' structure.
- It might not be efficient, because I need two different clients, thus double resources.
- I cannot use the same Client ID for each "logical" channel because I'm using two different MQTT Clients and as you know if you try to connect to the same broker with the same Client Id, the oldest one gets disconnected. Thus I need different configurations (and server-side authorizations) for each logical in/out flow in order to send and receive messages.
- The Spring Boot Application generates 2 times connections towards the server, so the server must be sized to receive 2 times the incoming connections and this might be a problem.
The general idea I have is to reimplement MqttPahoMessageDrivenChannelAdapter
and MqttPahoMessageHandler
in order to let them share the same MqttClient instance for the same Client ID. To do this, I have to reimplement the connection/disconnection/subscription methods within the Adapter and the Handler in order to make them robust. Also, I have to use a dedicated Client Factory in order to return the same client for the same Client ID (this is the easiest part).
Now the question is: is this approach correct? Am I raping Spring Boot trying to do so?
The only alternative I found is to implement raw MqttClients directly, managing their entire lifecycle. This is working, but I'm reinventing the wheel and I'm not leveraging the messaging design pattern of Spring Boot.