Search code examples
javaspringspring-bootmqttpaho

Adding MQTT connection with different client id in spring boot


My program gets real-time data for multiple devices over MQTT. I'm using spring-integration-mqtt version 5.5.13. The MQTT broker uses different client ids for each device. Everything's fine with a static and predefined client id. The problem arises when I try to add new adapter of class MqttPahoMessageDrivenChannelAdapter. The problem is that I can't.

I have defined a bean for MqttPahoClientFactory and MessageChannel. I also have a bean for the MessageHandler class that is annotated with the following:

@ServiceActivator(inputChannel = "mqttChannel") // mqttChannel is the adapter output channel

When I need a new connection that has its own client id, I call the method below.

public MessageProducer inbound() {
        MqttPahoMessageDrivenChannelAdapter adapter =
                new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "testClient",
                                                 "topic1", "topic2");
        adapter.setCompletionTimeout(5000);
        adapter.setConverter(new DefaultPahoMessageConverter());
        adapter.setQos(1);
        adapter.setOutputChannel(mqttInputChannel());
        return adapter;
    }

I'm guessing it doesn't submit as a listener because it's not a bean. I don't know if I can have multiple bean of one type added at runtime.

I didn't have any luck defining two beans of the MessageProducer type with different qualifiers. only one of them works.

Maybe simply calling the inbound method won't connect and subscribe? or the output channel isn't being called?

I'm new to MQTT and this is my first time trying this. also my first ever question so I hope it's a good one:)


Solution

  • So, I ended up using Eclipse Paho Client Mqttv3. It hasn't been updated in 2 years but it lets me manage my mqtt connections more direct and gets the job done.