注意:MQTT需要搭建服务器,不是服务端,是服务器!主题格式可以约定,约定格式后,格式不符合的主题不会推送成功,比如预定主题///*,推送主题为sss1/sss就会失败!
引入依赖:
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.22.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
创建mqtt连接实例:
import org.springframework.stereotype.Component;
@Component
public class MqttConfigBean {
private String host = "tcp://127.0.0.1:80801";
// private String topic;
private String username = "name";
private String password = "pass";
private int timeout = 300;
private int keepalive = 300;
/**
* @return the host
*/
public String getHost() {
return host;
}
/**
* @param host the host to set
*/
public void setHost(String host) {
this.host = host;
}
/**
* @return the topic
*/
// public String getTopic() {
// return topic;
// }
/**
* @param topic the topic to set
*/
// public void setTopic(String topic) {
// this.topic = topic;
// }
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the timeout
*/
public int getTimeout(