Connect via Java SDK

Eclipse Paho Java ClientConnect via Java SDK - 图1 (opens new window) is an MQTT client library written in Java(MQTT Java Client), which can be used in JVM or other Java compatible platforms (such as Android ).

Eclipse Paho Java Client provides asynchronous and synchronous API of MqttAsyncClient and MqttClient .

Install Paho Java via Maven

The Paho Java client library can be easily installed through the package management tool Maven. Until now, the latest version is installed as follows:

  1. <dependency>
  2. <groupId>org.eclipse.paho</groupId>
  3. <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
  4. <version>1.2.2</version>
  5. </dependency>

Paho Java usage example

In the Java system, Paho Java is a relatively stable and widely used MQTT client library. This example contains the complete code of Paho Java in the Java language connecting to EMQX, sending and receiving messages:

App.java

  1. package io.emqx;
  2. import org.eclipse.paho.client.mqttv3.MqttClient;
  3. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  4. import org.eclipse.paho.client.mqttv3.MqttException;
  5. import org.eclipse.paho.client.mqttv3.MqttMessage;
  6. import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
  7. public class App {
  8. public static void main(String[] args) {
  9. String subTopic = "testtopic/#";
  10. String pubTopic = "testtopic/1";
  11. String content = "Hello World";
  12. int qos = 2;
  13. String broker = "tcp://broker.emqx.io:1883";
  14. String clientId = "emqx_test";
  15. MemoryPersistence persistence = new MemoryPersistence();
  16. try {
  17. MqttClient client = new MqttClient(broker, clientId, persistence);
  18. // MQTT connection option
  19. MqttConnectOptions connOpts = new MqttConnectOptions();
  20. connOpts.setUserName("emqx_test");
  21. connOpts.setPassword("emqx_test_password".toCharArray());
  22. // retain session
  23. connOpts.setCleanSession(true);
  24. // set callback
  25. client.setCallback(new PushCallback());
  26. // establish a connection
  27. System.out.println("Connecting to broker: " + broker);
  28. client.connect(connOpts);
  29. System.out.println("Connected");
  30. System.out.println("Publishing message: " + content);
  31. // Subscribe
  32. client.subscribe(subTopic);
  33. // Required parameters for message publishing
  34. MqttMessage message = new MqttMessage(content.getBytes());
  35. message.setQos(qos);
  36. client.publish(pubTopic, message);
  37. System.out.println("Message published");
  38. client.disconnect();
  39. System.out.println("Disconnected");
  40. client.close();
  41. System.exit(0);
  42. } catch (MqttException me) {
  43. System.out.println("reason " + me.getReasonCode());
  44. System.out.println("msg " + me.getMessage());
  45. System.out.println("loc " + me.getLocalizedMessage());
  46. System.out.println("cause " + me.getCause());
  47. System.out.println("excep " + me);
  48. me.printStackTrace();
  49. }
  50. }
  51. }

Callback message processing class OnMessageCallback.java

  1. package io.emqx;
  2. import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
  3. import org.eclipse.paho.client.mqttv3.MqttCallback;
  4. import org.eclipse.paho.client.mqttv3.MqttMessage;
  5. public class OnMessageCallback implements MqttCallback {
  6. public void connectionLost(Throwable cause) {
  7. // After the connection is lost, it usually reconnects here
  8. System.out.println("disconnect, you can reconnect");
  9. }
  10. public void messageArrived(String topic, MqttMessage message) throws Exception {
  11. // The messages obtained after subscribe will be executed here
  12. System.out.println("Received message topic:" + topic);
  13. System.out.println("Received message QoS:" + message.getQos());
  14. System.out.println("Received message content:" + new String(message.getPayload()));
  15. }
  16. public void deliveryComplete(IMqttDeliveryToken token) {
  17. System.out.println("deliveryComplete---------" + token.isComplete());
  18. }
  19. }

Paho Java MQTT 5.0 Support

Currently, Paho Java is still adapting to MQTT 5.0 and has not yet fully supported it .