mqtt的ClientSub

该博客展示了如何使用IBM的MqttClient类创建一个MQTT客户端并订阅主题。代码中设置了CLEAN_START为true,KEEP_ALIVE时间为30秒,并订阅了"user/2"主题。当消息到达或连接丢失时,都会触发相应的回调处理。
package com.demo.mqtt;


import com.ibm.mqtt.MqttClient;
import com.ibm.mqtt.MqttException;
import com.ibm.mqtt.MqttSimpleCallback;


public class ClientSub {
private final static boolean CLEAN_START = true;
private final static short KEEP_ALIVE = 30;// 低耗网络,但是又需要及时获取数据,心跳30s
private final static String CLIENT_ID = "clientSub";
/**
* @param args
* @throws MqttException 
*/
public static void main(String[] args) throws MqttException {
MqttClient mqttClient1 = new MqttClient("tcp://123.57.81.91:1883");
mqttClient1.connect(CLIENT_ID, CLEAN_START, KEEP_ALIVE);

mqttClient1.subscribe(new String[]{"user/2"}, new int[]{2});
mqttClient1.registerSimpleHandler(new MqttSimpleCallback() {

@Override
public void publishArrived(String arg0, byte[] arg1, int arg2, boolean arg3)
throws Exception {
System.out.println(arg0);
System.out.println(new String(arg1));
System.out.println(arg2);
System.out.println(arg3);
}

@Override
public void connectionLost() throws Exception {
System.out.println("4444444444444444444444444444444444444444");
}
});
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值