Using mqtt protocol in java

MQTT: MQTT (Message Queuing Telemetry Transport) is a lightweight communication protocol based on publish/subscribe mode, which is built on TCP/IP protocol and published by IBM in 1999. The biggest advantage of MQTT is that it can provide real-time and reliable messaging services for connecting remote devices with very little code and limited bandwidth. As a low-cost, low-bandwidth instant messaging protocol, it has a wide range of applications in the Internet of Things, small devices, mobile applications and so on.

MQTT is a client-server based message publish/subscribe transfer protocol. MQTT protocol is lightweight, simple, open and easy to implement. These characteristics make it widely applicable. In many cases, including limited environments, such as machine-to-machine (M2M) communications and the Internet of Things (IoT). It has been widely used in satellite link communication sensors, occasional dial-up medical equipment, smart home, and some miniaturized equipment.

Using mqtt through java

1. First, download the jar package related to org.eclipse.paho.client.mqttv3-1.1.1.jar, or add dependencies to the maven project.


2. Sending Information

String HOST = "tcp://ip:1883" Server address
String userName = ""  # User name
String password = ""  # Password
String clientId = ""  # 
int qos = 1|2|0	# Level of message
String topic = ""  # Published topics
String count = ""  # Contents published  


 MemoryPersistence persistence = new MemoryPersistence()ï¼›Memory storage
   // Create Client
    MqttClient  client = new MqttClient(String url, String clientId, MemoryPersistence);
    //Create connection parameters
    MqttConnectOptions connOpts = nwe MqttConnectOptions();
    // Keep state in mind when restarting and reconnecting
     // Set the username of the connection
     connOptis.setPassword(password.toCharArray());  # Parameters are character array types
     // Create connection
     client.connect(connOpts);  # If you do not pass parameters, follow the default
     // Create message
      MqttMessage message = new MqttMessage(content.getBytes());   #  The parameter is the byte array type
    //  Setting the quality of service of messages
    //  Publish news
     client.publish(topic, message);
    //  Disconnect
    // Close the client

receiving end

MemoryPersistence memoryPersistence = new MemoryPersistence();
        try {
            MqttClient client = new MqttClient("","",memoryPersistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            // Set the callback function to invoke this method when subscription arrives
            client.setCallback(new MqttCallback() {
                public void connectionLost(Throwable throwable) {
                    // Called when connection fails

                public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
                    // Subscribe successfully and accept information calls
                    mqttMessage.getPayload(); // Get message content

                public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

        } catch (Exception e) {

Keywords: Eclipse Mobile Java Maven

Added by netzverwalter on Tue, 01 Oct 2019 23:26:00 +0300