Server closed connection without DISCONNECT

I am trying to write an integration test using hivemq test containers.But at start itself the hivemq server is getting disconnected .
and getting the below error:
Failed connecting MQTT client: test-mqtt-client12 to broker. Exception is: {} Retrying connecting…
com.hivemq.client.mqtt.exceptions.ConnectionClosedException: Server closed connection without DISCONNECT.

My code is as follows:

public class TestDMAPLauncher {

private HiveMQContainer hivemq = new HiveMQContainer(DockerImageName.parse("hivemq/hivemq-ce:latest"));

@Autowired
private DefaultMqttClientConfigImpl mqttClientConfig;

@PostConstruct
public void init() {
    hivemq.withExposedPorts(8080, 8081);
    hivemq.start();
    String address = hivemq.getHost() + ":" + hivemq.getMappedPort(8080);
    log.debug("HiveMQ container address: {}", address);
    mqttClientConfig.getMqtt().setServerPort(hivemq.getMappedPort(8080));
    mqttClientConfig.getMqtt().setBrokerURL(hivemq.getHost());
}

public static void main(String[] args) {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(TestLauncher.class);
}

}

Hi @poorvi.ijantkar

Thank you for reaching out. I understand your concern. By default, when you initiate a HiveMQ broker without custom port configurations in the /opt/hivemq/conf/config.xml file, MQTT client connections are exposed on port 1883.

I recommend trying to connect using port 1883 rather than 8080. This should address your issue. If you encounter any further difficulties or have additional questions, please don’t hesitate to ask. We are here to assist you.

Warm regards,
Dasha from HiveMQ Team

1 Like

Thanks for the reply.This issue was occuring during my local integration testing.
I did the below to get the exact port from the running instance of hive mq broker
mqttClientConfig.getMqtt().setServerPort(hivemq.getMqttPort());
mqttClientConfig.getMqtt().setBrokerURL(hivemq.getHost());