Em chào mọi người ạ, em đang viết cái mqtt chơi chơi mà ko hiểu sao nó không connect được, mọi người xem giúp em với ạ
package com.github.name.mqtt.Apps;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public final class Listener implements MqttCallback {
private final int qos = 1;
private String topic = "test";
private MqttClient client;
public Listener() throws MqttException {
try {
String host = String.format("tcp://localhost:1883");
String clientId = "example/temperature";
MqttConnectOptions conOpt = new MqttConnectOptions();
conOpt.setCleanSession(true);
this.client = new MqttClient(host, clientId, new MemoryPersistence());
this.client.setCallback(this);
this.client.connect(conOpt);
this.client.subscribe(this.topic, qos);
} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}
public void sendMessage(String payload) throws MqttException {
MqttMessage message = new MqttMessage(payload.getBytes());
message.setQos(qos);
this.client.publish(this.topic, message); // Blocking publish
}
/**
* @see MqttCallback#connectionLost(Throwable)
*/
public void connectionLost(Throwable cause) {
System.out.println("Connection lost because: " + cause);
System.exit(1);
}
/**
* @see MqttCallback#deliveryComplete(IMqttDeliveryToken)
*/
public void deliveryComplete(IMqttDeliveryToken token) {
}
/**
* @see MqttCallback#messageArrived(String, MqttMessage)
*/
public void messageArrived(String topic, MqttMessage message) throws MqttException {
System.out.println(String.format("[%s] %s", topic, new String(message.getPayload())));
}
public static void main(String[] args) throws MqttException {
new Listener();
}
}
docker
version: '2'
services:
mysql:
container_name: mosquitto_broker
restart: "no"
image: eclipse-mosquitto:latest
volumes:
- ./mosquitto.conf:/mosquitto/config/mosquitto.conf
- ./mosquitto/data:/mosquitto/data
- ./mosquitto/log:/mosquitto/log
ports:
- 1883:1883
- 9001:9001
mosquitto.conf
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
error trace:
cd /home/name/xddvm/web ; /usr/bin/env /usr/lib/jvm/java-11-openjdk-amd64/bin/java @/tmp/cp_7a0sfolri1d0yq2598sk5sj5u.argfile com.github.name.mqtt.Apps.Listener
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:197)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.io.EOFException
at java.base/java.io.DataInputStream.readByte(DataInputStream.java:272)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:92)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:137)
... 1 more