如何使用Eclipse Paho在Java MQTT客户端上接收消息时发布消息 [英] How to publish a message while receiving on a Java MQTT client using Eclipse Paho

查看:1246
本文介绍了如何使用Eclipse Paho在Java MQTT客户端上接收消息时发布消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Eclipse Paho在Java的MQTT客户端上实现一些功能.目标是订阅主题,并且在收到消息后,客户端会在另一个主题上发送另一条消息.

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) {

    }

}

问题是,该程序只能运行一次.收到消息后,将发送对此消息的答案,但似乎消息消息已发布"从未显示在屏幕上,并且客户端没有收到任何其他消息. 我的印象是,行sampleClient.publish("topic/example/ofanswer", ans);从未完成其执行. 请问有人知道它是怎么来的以及如何解决我的问题吗?

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?

推荐答案

我今天遇到了类似的问题.当我阅读具有两个连接的另一个问题我明白了:您需要两个MqttClient实例.一种用于发布,另一种用于订阅.不幸的是,我没有找到有关该事实的文档.

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.

顺便说一句.在有两个客户端的第一个实现中,我给他们提供了相同的ID(从逻辑上讲,它应该是相同的连接).但是第二个连接断开了第一个连接.当我开始使用两个不同的ID时,它开始起作用.

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.

这篇关于如何使用Eclipse Paho在Java MQTT客户端上接收消息时发布消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆