MQTT專案中的實踐

2021-08-26 05:14:07 字數 4098 閱讀 8795

1.org.eclipse.paho

org.eclipse.paho.client.mqttv3

1.2.0

2.配置檔案

#mqtt-使用者名稱

spring.mqtt.username=admin

#mqtt-密碼

spring.mqtt.password=password

spring.mqtt.url=tcp:

#mqtt-連線伺服器預設客戶端id,注意乙個必須唯一,訂閱端的clientid如果重複,訂閱端只能啟動乙個

spring.mqtt.client.id=publishclient

#mqtt-預設的訊息推送主題,實際可在呼叫介面時指定

spring.mqtt.default.topic=topic

1.建立乙個配置檔案實現bean方法

@configurationproperties("spring.mqtt")

@configuration

public class mqttconfig ")

private string username;

@value("$")

private string password;

@value("$")

private string hosturl;

@value("$")

private string clientid;

@value("$")

private string defaulttopic;

public string getusername()

public void setusername(string username)

public string getpassword()

public void setpassword(string password)

public string gethosturl()

public void sethosturl(string hosturl)

public string getclientid()

public void setclientid(string clientid)

public string getdefaulttopic()

public void setdefaulttopic(string defaulttopic)

@bean

mqttclient mqttclient() catch (exception me)

}@bean

mqttconnectoptions mqttconnectoptions()

}

2.實現service

@service

public class mqttservice

}mqttmessage message = new mqttmessage(content.getbytes("utf-8"));

// 設定訊息的服務質量

if(qos == null)

message.setqos(qos);

mqttclient.publish(topic, message);

}catch (exception e)

}}

1.config檔案

@configuration

public class mqttconfig catch (exception me)

}@bean

mqttconnectoptions mqttconnectoptions()catch (exception e)

}}

2.service

下面是我寫的乙個傳輸資料的方法的一部分,主要是在messagearrived()接收資料,注意設定編碼格式,不然在idea裡可以正確執行,但是打成jar包執行時將亂碼。

@component

public class mqttservice

public void messagearrived(string topic, mqttmessage message) catch (unsupportedencodingexception e)

if((systemid+"_"+clientid+"/datajoinupdate").equals(topic))else

timertasklist.clear();

}for (timertask t:timerlist2)

//設定10s的定時器

timertask task = new timertask()

};timer timer = new timer();

timer.schedule(task,1000*10);

timertasklist.add(task);

}}else

keylist.add(entry.getkey());

valuelist.add("'"+entry.getvalue().tostring()+"'");}}

string sql_count = "select count("+primarykey+") from "+tablename+" where "+primarykey+" = "+primaryvalue;

stmt = conn.preparestatement(sql_count);

resultset rs = stmt.executequery();

int count = 0;

while (rs.next())

rs.close();

if(count == 0)

}else if((systemid+"_"+clientid+"/update").equals(topic))

if(primarykey.equals(entry.getkey()))

}string str = string.join(",",list);

string updatesql = "update "+tablename+" set "+str+" where "+primarykey+" = "+primaryvalue;

stmt = conn.preparestatement(updatesql);

stmt.execute();

string sql_update = "select * from "+tablename+" where "+primarykey+" = "+primaryvalue;

stmt = conn.preparestatement(sql_update);

resultset r = stmt.executequery();

resultsetmetadata data = r.getmetadata();

list updatelist = new arraylist();

while (r.next())}}

r.close();

string delstr = string.join(" and ",updatelist);

string deleteupdatesql = "delete from "+tablename+"copy where "+delstr+" and action = 'update'";

stmt = conn.preparestatement(deleteupdatesql);

stmt.execute();

}else if((systemid+"_"+clientid+"/delete").equals(topic))

}catch (exception e)finally catch (exception e)}}

}public void deliverycomplete(imqttdeliverytoken token)

});client.connect(options);

//訂閱訊息

client.subscribe(topic, qos);

} catch (exception e)

return json;

}

MQTT協議實踐

補充 第一次搭建好mqtt伺服器,沒有設定使用者密碼,在登入時,username admin,password password 登入成功後的介面是這樣的 開啟客戶端paho,建立連線,輸入tcp埠號 輸入使用者名稱和密碼 連線成功後,伺服器顯示如下內容 用lu客戶端訂閱test主題 用luy客戶端...

大資料分析專案中的「最差」實踐

如果它是那麼簡單就好了。當bi 商樂呵呵地告訴你他們的客戶已經成功部署大資料分析專案時,他們不會告訴你還有那麼多失敗的案例。大資料分析專案令 人失望是有一些潛在原因的。你可以找到大量關於大資料分析最佳實踐的建議。但下面是一些大資料分析專案的最差實踐,你需要了解如何避免它們。盲目跟風 這種觀點重複犯了...

Redis在Web專案中的應用與實踐

redis作為乙個開源的 bsd 基於記憶體的高效能儲存系統,已經被各大網際網路公司廣泛使用,並且有著諸多的應用場景。本篇文章將基於php來詳細講解redis在web專案中的主要應用與實踐。這裡所介紹的快取是指可以丟失或過期的資料。常用的命令有set,hset,get,hget,使用redis作為快...