1. 程式人生 > >MQTT協議探究之消息集成

MQTT協議探究之消息集成

session align args exec star timeout window trac persist

一、 配置MQTT服務


1、下載apache-apollo-1.7.1-windows-distro.zip並解壓,到bin目錄下,雙擊apollo.cmd,出現瞬間閃現,則需要先生成本地用戶。

技術分享圖片

2、進入本地運行CMD命令窗口,執行如下三步命令,生成本地用戶。

技術分享圖片

3、在mybroker/etc/users.properties下面,可以修改用戶名和密碼。

技術分享圖片

4、進入本地生成用戶目錄mybroker文件的bin目錄下,運行apollo-broker.cmd run,出現如下信息。

技術分享圖片

5、訪問鏈接http://127.0.0.1:61680/console/index.html,輸入用戶名和密碼。出現如下界面,則說明服務已經啟動。

技術分享圖片

技術分享圖片

二、代碼


package hang.com.client;
import hang.com.test.PushCallbackTest;
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.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.util.concurrent.ScheduledExecutorService; public class ClientTest { public static final String HOST = "tcp://192.168.1.23:61613"; public static final String TOPIC = "toclient/124"; private static final String clientid = "client124"; private MqttClient client; private MqttConnectOptions options;
private String userName = "admin"; private String passWord = "password"; private ScheduledExecutorService scheduler; private void start() { try { // host為主機名,clientid即連接MQTT的客戶端ID,一般以唯一標識符表示,MemoryPersistence設置clientid的保存形式,默認為以內存保存 client = new MqttClient(HOST, clientid, new MemoryPersistence()); // MQTT的連接設置 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 PushCallbackTest()); MqttTopic topic = client.getTopic(TOPIC); //setWill方法,如果項目中需要知道客戶端是否掉線可以調用該方法。設置最終端口的通知消息 options.setWill(topic, "close".getBytes(), 2, true); client.connect(options); //訂閱消息 int[] Qos = {1}; String[] topic1 = {TOPIC}; client.subscribe(topic1, Qos); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws MqttException { ClientTest client = new ClientTest(); client.start(); } }

package hang.com.server;

import hang.com.test.PushCallbackTest;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class ServerTest {

    public static final String HOST = "tcp://192.168.1.23:61613";
    public static final String TOPIC = "toclient/124";
    public static final String TOPIC125 = "toclient/125";
    private static final String clientid = "server";

    private MqttClient client;
    private MqttTopic topic;
    private MqttTopic topic125;
    private String userName = "admin";
    private String passWord = "password";

    private MqttMessage message;

    public ServerTest() throws MqttException {
        // MemoryPersistence設置clientid的保存形式,默認為以內存保存
        client = new MqttClient(HOST, clientid, new MemoryPersistence());
        connect();
    }

    private void connect() {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(false);
        options.setUserName(userName);
        options.setPassword(passWord.toCharArray());
        // 設置超時時間
        options.setConnectionTimeout(10);
        // 設置會話心跳時間
        options.setKeepAliveInterval(20);
        try {
            client.setCallback(new PushCallbackTest());
            client.connect(options);
            topic = client.getTopic(TOPIC);
            topic125 = client.getTopic(TOPIC125);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void publish(MqttTopic topic , MqttMessage message) throws MqttPersistenceException,
            MqttException {
        MqttDeliveryToken token = topic.publish(message);
        token.waitForCompletion();
        System.out.println("message is published completely! "
                + token.isComplete());
    }

    public static void main(String[] args) throws MqttException {
        ServerTest server = new ServerTest();

        server.message = new MqttMessage();
        server.message.setQos(2);
        server.message.setRetained(true);
        server.message.setPayload("給客戶端124推送的信息".getBytes());
        server.publish(server.topic , server.message);

        server.message = new MqttMessage();
        server.message.setQos(2);
        server.message.setRetained(true);
        server.message.setPayload("給客戶端125推送的信息".getBytes());
        server.publish(server.topic125 , server.message);

        System.out.println(server.message.isRetained() + "------ratained狀態");
    }
}

MQTT協議探究之消息集成