mqtt paho java,如何使用Eclipse Paho在Java MQTT客户端上接收消息时发布消息

本文档描述了使用Eclipse Paho在Java中实现MQTT客户端时遇到的问题:当收到消息后,客户端无法正确发送回复消息。问题在于同一个MqttClient实例同时用于订阅和发布。解决方案是使用两个不同的MqttClient实例,一个用于订阅,另一个用于发布,以避免连接问题。
摘要由CSDN通过智能技术生成

I'm trying to implement some features on an MQTT client in Java with Eclipse Paho. The target is to subscribe to a topic and when a message is received, the client send another message on another topic.

This looks very easy, but I have a weird problem I can't solve. Here is my code :

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;

import org.eclipse.paho.client.mqttv3.IMqttToken;

import org.eclipse.paho.client.mqttv3.MqttCallback;

import org.eclipse.paho.client.mqttv3.MqttConnectOptions;

import org.eclipse.paho.client.mqttv3.MqttException;

import org.eclipse.paho.client.mqttv3.MqttMessage;

import org.eclipse.paho.client.mqttv3.MqttClient;

import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MqttOperations implements MqttCallback {

MqttClient sampleClient;

MqttConnectOptions connOpts;

public MqttOperations() {

}

public static void main(String[] args) throws InterruptedException {

new MqttOperations().launchMqttClient();

}

public void launchMqttClient() throws InterruptedException {

try {

MemoryPersistence persistence = new MemoryPersistence();

sampleClient = new MqttClient("tcp://broker.mqttdashboard.com:1883", "iamaclient", persistence);

connOpts = new MqttConnectOptions();

connOpts.setCleanSession(true);

sampleClient.connect(connOpts);

sampleClient.subscribe("topic/example/ofmessage");

sampleClient.setCallback(this);

} catch(MqttException me) {

System.out.println("reason "+me.getReasonCode());

System.out.println("msg "+me.getMessage());

System.out.println("loc "+me.getLocalizedMessage());

System.out.println("cause "+me.getCause());

System.out.println("excep "+me);

me.printStackTrace();

}

}

@Override

public void connectionLost(Throwable cause) {

// TODO Auto-generated method stub

}

@Override

public void messageArrived(String topic, MqttMessage message) throws MqttException

{

System.out.println("Received: " + message.toString());

try{

System.out.println("Publishing message: i am the answer");

MqttMessage ans = new MqttMessage("i am the answer".getBytes());

ans.setQos(2);

sampleClient.publish("topic/example/ofanswer", ans);

System.out.println("Message published");

}catch(MqttException me){

System.out.println("reason "+me.getReasonCode());

System.out.println("msg "+me.getMessage());

System.out.println("loc "+me.getLocalizedMessage());

System.out.println("cause "+me.getCause());

System.out.println("excep "+me);

me.printStackTrace();

}

}

@Override

public void deliveryComplete(IMqttDeliveryToken token) {

}

}

The thing is, this program works only once. When the message is received, the answer to this message is sent, but it appears that the message "message published" is never displayed on the screen, and the client doesn't receive any other messages.

I have this impression that the line sampleClient.publish("topic/example/ofanswer", ans); never finishes its execution.

Does anyone know how it comes and how to solve my problem please?

解决方案

I had a similar problem today. When I read an other question with two connections I got it: You need two MqttClient instances. One for publishing and one for subscribing. Unfortunately I found no documentation for that fact.

By the way. In my first implementation with two clients, I gave them the same ids (logically it should be the same connection). But the second connection disconnects the first one. When I started to use two different ids, it starts to work.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值