Java MQTT with RabbitMQ and LavinMQ
We recommend the Eclipse Paho Java client,
the most widely used MQTT client for the JVM. Add it to your pom.xml:
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.5</version>
</dependency>
Publish and subscribe
Set MQTT_HOST, MQTT_PORT, MQTT_USERNAME and MQTT_PASSWORD to the MQTT
details on your instance’s details page in the CloudAMQP console. Port 8883
uses TLS. Without them, the examples connect to a local broker.
Save the publisher as src/main/java/Publisher.java:
import static java.nio.charset.StandardCharsets.UTF_8;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class Publisher {
public static void main(String[] args) throws Exception {
String port = System.getenv().getOrDefault("MQTT_PORT", "1883");
String scheme = port.equals("8883") ? "ssl" : "tcp";
String host = System.getenv().getOrDefault("MQTT_HOST", "localhost");
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName(System.getenv().getOrDefault("MQTT_USERNAME", "guest"));
options.setPassword(System.getenv().getOrDefault("MQTT_PASSWORD", "guest").toCharArray());
try (MqttClient client = new MqttClient(scheme + "://" + host + ":" + port, MqttClient.generateClientId(), new MemoryPersistence())) {
client.connect(options);
// QoS 1 blocks until the broker has sent its PUBACK
client.publish("hello", "Hello world!".getBytes(UTF_8), 1, false);
System.out.println(" [x] Sent 'Hello world!'");
client.disconnect();
}
}
}
Save the subscriber as src/main/java/Worker.java:
import static java.nio.charset.StandardCharsets.UTF_8;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class Worker {
public static void main(String[] args) throws Exception {
String port = System.getenv().getOrDefault("MQTT_PORT", "1883");
String scheme = port.equals("8883") ? "ssl" : "tcp";
String host = System.getenv().getOrDefault("MQTT_HOST", "localhost");
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName(System.getenv().getOrDefault("MQTT_USERNAME", "guest"));
options.setPassword(System.getenv().getOrDefault("MQTT_PASSWORD", "guest").toCharArray());
MqttClient client = new MqttClient(scheme + "://" + host + ":" + port, MqttClient.generateClientId(), new MemoryPersistence());
client.connect(options);
// Each message is acked when the listener returns
client.subscribe("hello", 1, (topic, message) ->
System.out.println(" [x] Received '" + new String(message.getPayload(), UTF_8) + "'"));
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
}
}
Run mvn compile exec:java -Dexec.mainClass=Worker in one terminal and
mvn compile exec:java -Dexec.mainClass=Publisher in another.
A runnable example is in java/paho-mqtt in cloudamqp/examples.
Good to know
- Put the vhost in the username, as
vhost:user. MQTT has no vhost of its own, and the console shows the username that way. - Use
ssl://for TLS, as the examples do on port 8883. The server certificate and hostname are verified. - Use QoS 1, so
publishblocks until the broker has sent its PUBACK, and each message is acked when the listener returns. - Paho doesn’t reconnect by default. Call
setAutomaticReconnect(true)on theMqttConnectOptions, and subscribe again in aMqttCallbackExtended’sconnectComplete, as a clean session drops the subscriptions.
Was this page helpful?
Thanks for your feedback!