eclipse-paho / eclipse-paho/paho.mqtt.java

MqttException: Unable to connect to server

Open
#761 3 comments 0 reactions 0 assignees View on GitHub
triage
Dominant language
Java
Stars
2.3k
Forks
919
PR merge metrics
No merged PRs in 30d

Description

- [X] Bug exists Release Version 1.2.2 ( Master Branch)
- [ ] Bug exists in MQTTv3 Client on Snapshot Version 1.2.3-SNAPSHOT (Develop Branch)
- [ ] Bug exists in MQTTv5 Client on Snapshot Version 1.2.3-SNAPSHOT (Develop Branch)

Hello, I am trying to use Paho with TomCat server. While I am running the application locally, everything is fine, but once I deploy the app on the server (apache tomcat 8.0.48, jdk1.8.0_162) its not working.

Here are the logs:
`11:47:52.406 [WEBSITE-startStop-7] INFO com.APP.mqtt.MQTTConfig - Try to connect to tcp://m21.cloudmqtt.com:16952
11:47:52.529 [WEBSITE-startStop-7] INFO com.APP.mqtt.MQTTConfig - CONNECTING TO MQTT
11:47:52.551 [WEBSITE-startStop-7] INFO com.APP.mqtt.MQTTConfig - CONNECTED
11:47:52.551 [WEBSITE-startStop-7] INFO com.APP.mqtt.MQTTConfig - wait for completion reached
11:47:54.617 [WEBSITE-startStop-7] ERROR com.APP.mqtt.MQTTConfig - ERROR(Connection)
org.eclipse.paho.client.mqttv3.MqttException: Unable to connect to server
at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:80)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:722)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400)
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243)
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402)
at java.base/java.net.Socket.connect(Socket.java:591)
at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:74)
... 2 common frames omitted`

Here is the code:

void config(){
String brokerUrl = "tcp://" + this.broker + ":" + port;
logger.info("Try to connect to " + brokerUrl);
MqttConnectOptions connectionOptions = new MqttConnectOptions();
try {
this.mqttClient = new MqttAsyncClient(brokerUrl, clientId, new MemoryPersistence());
connectionOptions.setCleanSession(true);
connectionOptions.setAutomaticReconnect(true);
if (password != null) {
connectionOptions.setPassword(this.password.toCharArray());
}
if (userName != null) {
connectionOptions.setUserName(this.userName);
}
logger.info("CONNECTING TO MQTT");
IMqttToken token = this.mqttClient.connect(connectionOptions);
logger.info("CONNECTED");
logger.info("wait for completion reached");
token.waitForCompletion();
logger.info("CONNECTION DONE");
this.mqttClient.setCallback(this);
}
catch (Exception me) {
logger.error("ERROR(Connection)", me);
}
}

Please help :(

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.