java - 如何在java中使用mqtt从云(iothub)检索数据

标签 java azure azure-eventhub azure-iot-hub

我是 IoTHub 新手。我已经使用python成功地将消息发送到IOT集线器(D2C)。我们使用的协议(protocol)是mqtt。我们正在尝试使用java从云(IOT集线器)检索数据,但无法找到接收消息的正确方法云......我的疑问是我们是否可以直接从 IOT 中心读取消息,或者我们需要将传入消息重定向到事件中心以检索消息。

我还尝试在将数据发送到云端的同时,同时从java中的iothub读取消息,但出现如下错误..(与服务器失去连接。重新连接0次。)

我使用此代码从iothub读取数据,

import com.microsoft.azure.sdk.iot.device.DeviceClient;
import com.microsoft.azure.sdk.iot.device.IotHubMessageResult;
import com.microsoft.azure.sdk.iot.device.Message;
import com.microsoft.azure.sdk.iot.device.MessageCallback;
import com.microsoft.azure.sdk.iot.device.IotHubClientProtocol;
import com.microsoft.azure.sdk.iot.service.sdk.IotHubServiceClientProtocol;
import java.io.IOException;
import java.net.URISyntaxException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Kafkareception {

    public static void main(String[] args) throws IOException {
        try {
            String connString = "HostName=";
            IotHubClientProtocol protocol = IotHubClientProtocol.MQTT;
            DeviceClient client = new DeviceClient(connString, protocol);

            MessageCallback callback = new AppMessageCallback();
            client.setMessageCallback(callback, null);
            client.open();
        } catch (URISyntaxException ex) {
            Logger.getLogger(Kafkareception.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static class AppMessageCallback implements MessageCallback {

        public IotHubMessageResult execute(Message msg, Object context) {
            System.out.println(new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET) + "Received message from hub: ");

            return IotHubMessageResult.COMPLETE;
        }
    }
}

最佳答案

根据您提供的信息,您可能尝试使用 DeviceClient 设置一台设备到 Azure IoT 中心的两个 Activity 连接:一个正在发送 D2C 消息,另一个正在“从 iothub 读取数据/em>”。您收到错误可能是因为:

IoT Hub only supports one active MQTT connection per device. Any new MQTT connection on behalf of the same device ID causes IoT Hub to drop the existing connection.

引用号:Communicate with your IoT hub using the MQTT protocol .

如果您想接收发送到 Azure IoT 中心的 D2C 消息,您可以 use Event Hub-compatible endpoint(Java) 。无需自行将传入消息重定向到事件中心。

IoT Hub exposes the messages/events built-in endpoint for your back-end services to read the device-to-cloud messages received by your hub. This endpoint is Event Hub-compatible, which enables you to use any of the mechanisms the Event Hubs service supports for reading messages.

引用号:Understand Azure IoT Hub messagingIoT Hub endpoints .

关于java - 如何在java中使用mqtt从云(iothub)检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44082018/

相关文章:

.net - 有什么方法可以在 azure 事件中心中使用 EventProcessorHost 检索跳过的事件吗?

azure - 是否可以在不使用实际事件中心的情况下在本地执行事件中心触发的 Azure Functions?

java - ConcurrentHashMap中Bucket级锁和Segment级锁的区别?

azure - 将对 Azure VM 的访问锁定到特定 IP

android - 从 Azure DevOps 部署到 Google Play 商店时出现错误 "APK specifies a version code that has already been used.."

azure - 使用 for 循环使用 terraform 从 csv 文件中获取值

go - 在 golang 中批量处理来自 ms azure eventhub 的事件

java - float 的快捷方式赋值和普通赋值的行为

java - Excel 中以 "ID"作为第一项的 CSV 文件已损坏

Java - 如何使用 XML Pull Parser 轻松解析 XML?