JAVA開發MQTT總結


JAVA開發MQTT總結

MQTT 介紹

  • 它是一種 機器之間通訊 machine-to-machine (M2M)、物聯網 Internet of Things (IoT)常用的一種輕量級消息傳輸協議
  • 適用於網絡帶寬較低的場合
  • 包含發布、訂閱模式,通過一個代理服務器(broker),任何一個客戶端(client)都可以訂閱或者發布某個主題的消息,然后訂閱了該主題的客戶端則會收到該消息

mqtt還是之前公司有需求所以寫的一個demo,在這里記錄下來,方便有人使用的時候查閱,不涉及mqtt的具體講解,只是貼代碼和運行過程。

MQTT的入門,以及特性,協議,結構的講解,請看下面這篇文章

https://www.runoob.com/w3cnote/mqtt-intro.html

什么是MQTT,它能干什么,它的應用場景在哪里?請參考下面這篇文章

https://www.ibm.com/developerworks/cn/iot/iot-mqtt-why-good-for-iot/index.html

本文中采用的MQTT服務器Apache-Apollo的下載配置搭建過程,請參考下面這篇文章

https://blog.csdn.net/qq_29350001/article/details/76680646

下面就開始創建broker,

RaindeMacBook-Pro:bin rain$ ./apollo create mybroker
Creating apollo instance at: mybroker
Generating ssl keystore...

Warning:
JKS 密鑰庫使用專用格式。建議使用 "keytool -importkeystore -srckeystore keystore -destkeystore keystore -deststoretype pkcs12" 遷移到行業標准格式 PKCS12。

You can now start the broker by executing:

   "/Users/rain/Documents/Soft/apache-apollo-1.7.1/bin/mybroker/bin/apollo-broker" run

Or you can run the broker in the background using:

   "/Users/rain/Documents/Soft/apache-apollo-1.7.1/bin/mybroker/bin/apollo-broker-service" start


進入新生成的broker中

RaindeMacBook-Pro:bin rain$ ls
apollo		apollo.cmd	mybroker	testbroker
RaindeMacBook-Pro:bin rain$ cd mybroker/
RaindeMacBook-Pro:mybroker rain$ ls
bin	data	etc	log	tmp
RaindeMacBook-Pro:mybroker rain$ cd bin
RaindeMacBook-Pro:bin rain$ ls
apollo-broker		apollo-broker-service

可以看到有兩個文件,啟動apollo-broker

啟動成功以后,就可以在瀏覽器中訪問了,默認用戶名和密碼是admin,password

剛進去是,Topics選項卡是空的,我是在運行程序后截圖的,所以有一個topic列表

配置Maven

在pom.xml中添加以下配置

<dependency>
            <groupId>org.eclipse.paho</groupId>
            <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
            <version>1.2.0</version>
        </dependency>

再創建下面的類

MqttServer

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MqttServer2 {
    /**
     * 代理服務器ip地址
     */
    public static final String MQTT_BROKER_HOST = "tcp://127.0.0.1:61613";

    /**
     * 訂閱標識
     */
    public static final String MQTT_TOPIC = "test2";

    private static String userName = "admin";
    private static String password = "password";

    /**
     * 客戶端唯一標識
     */
    public static final String MQTT_CLIENT_ID = "android_server_xiasuhuei32";
    private static MqttTopic topic;
    private static MqttClient client;

    public static void main(String... args) {
        // 推送消息
        MqttMessage message = new MqttMessage();
        try {
            client = new MqttClient(MQTT_BROKER_HOST, MQTT_CLIENT_ID, new MemoryPersistence());
            MqttConnectOptions options = new MqttConnectOptions();
            options.setCleanSession(true);
            options.setUserName(userName);
            options.setPassword(password.toCharArray());
            options.setConnectionTimeout(10);
            options.setKeepAliveInterval(20);

            topic = client.getTopic(MQTT_TOPIC);

            message.setQos(1);
            message.setRetained(false);
            message.setPayload("message from server222222".getBytes());
            client.connect(options);

            while (true) {
                MqttDeliveryToken token = topic.publish(message);
                token.waitForCompletion();
                System.out.println("已經發送222");
                Thread.sleep(10000);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

MqttClient

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MyMqttClient {
    /**
     * 代理服務器ip地址
     */
    public static final String MQTT_BROKER_HOST = "tcp://127.0.0.1:61613";

    /**
     * 客戶端唯一標識
     */
    public static final String MQTT_CLIENT_ID = "android_xiasuhuei321";

    /**
     * 訂閱標識
     */
//    public static final String MQTT_TOPIC = "xiasuhuei321";

    /**
     *
     */
    public static final String USERNAME = "admin";
    /**
     * 密碼
     */
    public static final String PASSWORD = "password";
    public static final String TOPIC_FILTER = "test2";

    private volatile static MqttClient mqttClient;
    private static MqttConnectOptions options;

    public static void main(String... args) {
        try {
            // host為主機名,clientid即連接MQTT的客戶端ID,一般以客戶端唯一標識符表示,
            // MemoryPersistence設置clientid的保存形式,默認為以內存保存

            mqttClient = new MqttClient(MQTT_BROKER_HOST, MQTT_CLIENT_ID, new MemoryPersistence());
            // 配置參數信息
            options = new MqttConnectOptions();
            // 設置是否清空session,這里如果設置為false表示服務器會保留客戶端的連接記錄,
            // 這里設置為true表示每次連接到服務器都以新的身份連接
            options.setCleanSession(true);
            // 設置用戶名
            options.setUserName(USERNAME);
            // 設置密碼
            options.setPassword(PASSWORD.toCharArray());
            // 設置超時時間 單位為秒
            options.setConnectionTimeout(10);
            // 設置會話心跳時間 單位為秒 服務器會每隔1.5*20秒的時間向客戶端發送個消息判斷客戶端是否在線,但這個方法並沒有重連的機制
            options.setKeepAliveInterval(20);
            // 連接
            mqttClient.connect(options);
            // 訂閱
            mqttClient.subscribe(TOPIC_FILTER);
            // 設置回調
            mqttClient.setCallback(new MqttCallback() {
                @Override
                public void connectionLost(Throwable throwable) {
                    System.out.println("connectionLost");
                }

                @Override
                public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
                    System.out.println("Topic: " + s + " Message: " + mqttMessage.toString());
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

PublishSample

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 class PublishSample {
    public static void main(String[] args) {

        String topic = "test2";
        String content = "hello 哈哈";
        int qos = 1;
        String broker = "tcp://127.0.0.1:61613";
        String userName = "admin";
        String password = "password";
        String clientId = "pubClient";
        // 內存存儲
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            // 創建客戶端
            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            // 創建鏈接參數
            MqttConnectOptions connOpts = new MqttConnectOptions();
            // 在重新啟動和重新連接時記住狀態
            connOpts.setCleanSession(false);
            // 設置連接的用戶名
            connOpts.setUserName(userName);
            connOpts.setPassword(password.toCharArray());
            // 建立連接
            sampleClient.connect(connOpts);
            // 創建消息
            MqttMessage message = new MqttMessage(content.getBytes());
            // 設置消息的服務質量
            message.setQos(qos);
            // 發布消息
            sampleClient.publish(topic, message);
            // 斷開連接
            sampleClient.disconnect();
            // 關閉客戶端
            sampleClient.close();
        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode());
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
            me.printStackTrace();
        }
    }
}

SubscribeSample

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

/**
 *訂閱端
 */
public class SubscribeSample {

    public static void main(String[] args) throws MqttException {
        String HOST = "tcp://127.0.0.1:61613";
        String TOPIC = "test2";
        int qos = 1;
        String clientid = "subClient";
        String userName = "admin";
        String passWord = "password";
        try {
            // host為主機名,test為clientid即連接MQTT的客戶端ID,一般以客戶端唯一標識符表示,MemoryPersistence設置clientid的保存形式,默認為以內存保存
            MqttClient client = new MqttClient(HOST, clientid, new MemoryPersistence());
            // MQTT的連接設置
            MqttConnectOptions options = new MqttConnectOptions();
            // 設置是否清空session,這里如果設置為false表示服務器會保留客戶端的連接記錄,這里設置為true表示每次連接到服務器都以新的身份連接
            options.setCleanSession(true);
            // 設置連接的用戶名
            options.setUserName(userName);
            // 設置連接的密碼
            options.setPassword(passWord.toCharArray());
            // 設置超時時間 單位為秒
            options.setConnectionTimeout(10);
            // 設置會話心跳時間 單位為秒 服務器會每隔1.5*20秒的時間向客戶端發送個消息判斷客戶端是否在線,但這個方法並沒有重連的機制
            options.setKeepAliveInterval(20);
            // 設置回調函數
            client.setCallback(new MqttCallback() {

                public void connectionLost(Throwable cause) {
                    System.out.println("connectionLost");
                }

                public void messageArrived(String topic, MqttMessage message) throws Exception {
                    System.out.println("topic:"+topic);
                    System.out.println("Qos:"+message.getQos());
                    System.out.println("message content:"+new String(message.getPayload()));

                }

                public void deliveryComplete(IMqttDeliveryToken token) {
                    System.out.println("deliveryComplete---------"+ token.isComplete());
                }

            });
            client.connect(options);
            //訂閱消息
            client.subscribe(TOPIC, qos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

啟動程序

1.啟動MqttServer2以后,開始循環發送消息。

2.啟動MyMqttClient開始接收消息。

到這里,整個程序基本可以運行。

3.啟動PublishSample,發布一條消息,在啟動SubscribeSample來訂閱發布的消息。

4.發布的消息在MyMqttClient中也會顯示出來


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM