如何使用MQTT Paho订阅RabbitMQ队列 [英] How subscribe in an RabbitMQ queue with MQTT Paho

查看:982
本文介绍了如何使用MQTT Paho订阅RabbitMQ队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的Android应用连接到一个称为消息"的队列.

生产者(一种基于AMQP协议的Web服务)已经连接,可以通过RabbitMQ管理面板进行检查.

要从我的Android设备进行连接,我正在这样编码.

private void connect() throws Exception {

    this.sampleClient = new MqttClient(this.broker, this.clientId);

    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setUserName("user");
    connOpts.setPassword("user".toCharArray());
    /*connOpts.setConnectionTimeout(60 * 10);
    connOpts.setKeepAliveInterval(60 * 5);*/
    connOpts.setCleanSession(true);

    this.sampleClient.connect(connOpts);

    this.sampleClient.setCallback(this);

    this.sampleClient.subscribe("messages");

    if(!this.sampleClient.isConnected()){

        System.out.println("Not Connected");
        return;
    }

    System.out.println("Connected");
}

我尝试使用"amq.topic","amq.topic.*","amq.topic.messages"等.但是当我在RabbitMQ队列部分中查看时,"messages"的消费者为0,并已自动设置一个名为"mqtt-subscription-Sampleqos1"的新队列.

发生了什么事?我如何怀疑消息"队列?

解决方案

此问题有两个重要要点.

根据RabbitMQ MQTT文档: http://www.rabbitmq.com/mqtt.html

首先,每个队列都由mqtt-plugin自动绑定到 amq.topic 交换.

第二,每个订阅者都有自己的队列,如下所示: mqtt-subscription- {cliend_id} {qosX} (其中X是订阅的qos级别)

因此,生产者必须将消息发布到"amq.topic"交换和"amq.topic .."路由密钥,而接收者必须订阅"amq.topic .."路由密钥.

I am trying to connect from my Android app to one queue called "messages".

The producer (one webservices under AMQP protocol) is already connected, it can be check through RabbitMQ admin panel.

To connect from my Android device I am coding like this.

private void connect() throws Exception {

    this.sampleClient = new MqttClient(this.broker, this.clientId);

    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setUserName("user");
    connOpts.setPassword("user".toCharArray());
    /*connOpts.setConnectionTimeout(60 * 10);
    connOpts.setKeepAliveInterval(60 * 5);*/
    connOpts.setCleanSession(true);

    this.sampleClient.connect(connOpts);

    this.sampleClient.setCallback(this);

    this.sampleClient.subscribe("messages");

    if(!this.sampleClient.isConnected()){

        System.out.println("Not Connected");
        return;
    }

    System.out.println("Connected");
}

I have tried with "amq.topic", "amq.topic.*", "amq.topic.messages", etc... But when I look in the RabbitMQ queue section "messages" is with 0 consumers, and have been set one new queue called "mqtt-subscription-Sampleqos1" automatically.

What's happening? How can I susbscribe to "messages" queue?

解决方案

There are two important points about this question.

According with the RabbitMQ MQTT documentation: http://www.rabbitmq.com/mqtt.html

Firstly, every queues are bound automatically to amq.topic exchange by the mqtt-plugin.

Secondly, every subscriber has his own queue which look like this, mqtt-subscription-{cliend_id}{qosX} (where X is the qos level of the subscription)

Therefore, producer must to publish the message to "amq.topic" exchange, and "amq.topic.." routing-key, and receiver must to subscribe to "amq.topic.." routing-key.

这篇关于如何使用MQTT Paho订阅RabbitMQ队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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