- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我创建了一个类型为 MqttClient
的客户端
,如下面的代码所示,我创建了一个客户端并设置了它的异步回调
。问题是,
1-当我运行程序时,System.out.println("Client is Connected");
出现,但我没有收到来自 onSuccess
的响应或来自 oonFailure
,为什么?我在代码中做错了什么。
2-i 实现了 static IMqttAsyncClient asynchClientCB = new IMqttAsyncClient()
接口(interface),但是因为我有一个 MqttClient
类型的客户端,所以我不能使用这个 IMqttAsyncClient
接口(interface)。我尝试使用 mqttAsynchClien
但因为我为 java 而不是为 Android
编程,所以我无法使用它。如何使用IMqttAsyncClient
接口(interface)?
Update_1
在下面的代码“Updated_code_1”中,我稍微修改了代码,但我希望每次成功连接到 broker
时,onSuccess
同步回调中的消息是打印,并在连接终止的情况下打印 onFailure
同步回调中的消息,例如当我故意断开网络时。但是在我连接到 broker
时,onSuccess
和 onFailur
都没有显示任何内容。那么,它们的设计用途是什么?
*Update_2_17_Dec_2014
我有一个问题可能会引导我们找到解决方案,也就是说,我是否通过有线/无线网络连接到代理有关系吗?这会改变同步和异步监听器的行为吗?
Updated_1_code:
MqttConnectOptions opts = getClientOptions();
client = MQTTClientFactory.newClient(broker, port, clientID);
if (client != null) {
System.out.println("Client is not Null");
client.setCallback(AsynchCallBack);
if (opts != null) {
iMQTTToken = client.connectWithResult(opts);
publishMSG(client, TOPIC,"010101".getBytes(), QoS, pub_isRetained);
iMQTTToken.setActionCallback(synchCallBack);
if (client.isConnected()) {
System.out.println("Client CONNECTED.");
publishMSG(client, TOPIC,"010101".getBytes(), QoS, pub_isRetained);
}
}
}
....
....
....
....
IMqttToken iMQTTToken = new IMqttToken() {
@Override
public void waitForCompletion(long arg0) throws MqttException {
// TODO Auto-generated method stub
System.out.println("@waitForCompletion(): waiting " + (arg0 * 1000) + " seconds for connection to be established.");
}
@Override
public void waitForCompletion() throws MqttException {
// TODO Auto-generated method stub
System.out.println("@waitForCompletion(): waiting for connection to be established.");
}
@Override
public void setUserContext(Object arg0) {
// TODO Auto-generated method stub
}
@Override
public void setActionCallback(IMqttActionListener arg0) {
// TODO Auto-generated method stub
arg0.onSuccess(iMQTTToken);
//System.out.println(" " + arg0.onSuccess());
//System.out.println(" " + arg0.onSuccess(iMQTTToken));
iMQTTToken.setActionCallback(synchCallBack);
}
@Override
public boolean isComplete() {
// TODO Auto-generated method stub
return false;
}
@Override
public Object getUserContext() {
// TODO Auto-generated method stub
return null;
}
@Override
public String[] getTopics() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean getSessionPresent() {
// TODO Auto-generated method stub
return false;
}
@Override
public MqttWireMessage getResponse() {
// TODO Auto-generated method stub
return null;
}
@Override
public int getMessageId() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int[] getGrantedQos() {
// TODO Auto-generated method stub
return null;
}
@Override
public MqttException getException() {
// TODO Auto-generated method stub
return null;
}
@Override
public IMqttAsyncClient getClient() {
// TODO Auto-generated method stub
return null;
}
@Override
public IMqttActionListener getActionCallback() {
// TODO Auto-generated method stub
return null;
}
};
IMqttActionListener synchCallBack = new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken arg0) {
// TODO Auto-generated method stub
System.out.println("@onSuccess: Connection Successful.");
}
@Override
public void onFailure(IMqttToken arg0, Throwable arg1) {
// TODO Auto-generated method stub
System.out.println("@onFailure: Connection Failed.");
setViewEnableState(Bconnect, true);
}
};
MqttCallback AsynchCallBack = new MqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage msg) throws Exception {
// TODO Auto-generated method stub
System.out.println("@messageArrived: Message Delivered.");
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
// TODO Auto-generated method stub
System.out.println("@deliveryComplete: Delivery Completed.");
}
@Override
public void connectionLost(Throwable thrw) {
// TODO Auto-generated method stub
System.out.println("@Connection Lost: Connection Lost.");
setViewEnableState(Bconnect, true);
}
};
新客户:
MqttConnectOptions opts = new MqttConnectOptions();
opts.setCleanSession(CS);
opts.setKeepAliveInterval(KATimer);
HashMap<Integer, WILL> LWTData = WILLFactory.newWILL("LWT", "LWT MS".getBytes(), 1, false);
opts.setWill(LWTData.get(0).getWILLTopic(),
LWTData.get(0).getWILLPayLoad(),
LWTData.get(0).getWILLQoS(),
LWTData.get(0).isWILLRetained());
client = MQTTClientFactory.newClient(IP, PORT, clientID);
if (client != null) {
System.out.println("client is not null");
client.setCallback(AsynchCB);
IMqttToken token = client.connectWithResult(opts);
if (client.isConnected()) {
System.out.println("Client is Connected");
token.setActionCallback(new IMqttActionListener() {
public void onSuccess(IMqttToken arg0) {
// TODO Auto-generated method stub
System.out.println("synchCB->@onSuccess(): Connection Successful");
try {
client.subscribe(TOPIC, QoS);
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
client.disconnect();
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onFailure(IMqttToken arg0, Throwable arg1) {
// TODO Auto-generated method stub
System.out.println("synchCB->@onFailure(): Connection Failed");
}
});
}else {
System.out.println("client is not connected");
}
}else {
System.out.println("client = null");
}
异步回调:
/**
* Asynchronous Callback to inform the user about events that might happens Asynchronously. If it is not used, any pending
* messages destined to the client would not be received.
*/
private static MqttCallback AsynchCB = new MqttCallback() {
public void messageArrived(String topic, MqttMessage msg) throws Exception {
// TODO Auto-generated method stub
System.out.println("AsynchCB->@messageArrived(): ");
System.out.println("Topic: " + topic);
System.out.println("MSG: " + msg.toString());
}
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
System.out.println("AsynchCB->@deliveryComplete(): ");
}
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
System.out.println("AsynchCB->@connectionLost(): ");
}
};
最佳答案
您的计算机上驻留的客户端,它处理您的回调,传出端口可能被计算机的防火墙阻止。
关于java - MqttClient 对象的同步和异步接口(interface)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27185591/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!