MQTT实现服务端和客户端互相推送订阅消息

注意: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(
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值