简体   繁体   English

如何在数据库中存储MQTT订阅的数据

[英]How to store mqtt subscribed data in database

I am using MQTT Paho MqttCallback I am successfully able to subscribe and publish on my topic without any issues but, my task is to store the data in database which is getting in messageArrived(String topic, MqttMessage message) so the data I can use it in future. 我正在使用MQTT Paho MqttCallback我可以成功地在主题上进行订阅和发布,没有任何问题,但是,我的任务是将数据存储在messageArrived(String topic, MqttMessage message)中的数据库中,以便可以使用它的数据在未来。

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
    // code to store the message in database
     LOGGER.info("messageArrived>> " + message);
}

messageArrived() is not executing the code which is storing to database its simply executing the LOGGER that's. messageArrived()未执行仅将LOGGER执行的代码存储到数据库中。 Code to store the message in database is correct then also it is skipping the code. 将消息存储在数据库中的代码是正确的,那么它也在跳过代码。 What strategy do I need to apply so first it will store the message in database and prints the log. 我需要采用什么策略,因此首先将消息存储在数据库中并打印日志。

update 更新

I am trying something similar to this 我想类似的东西这个

This will be because your logger/database code it throwing an exception that is getting swallowed by the paho framework. 这是因为您的记录器/数据库代码引发了paho框架吞噬的异常。

Add a try/catch block round your Logger call. 在Logger调用周围添加一个try / catch块。

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
     try {
         // code to store the message in database
         LOGGER.info("messageArrived>> " + message);
     } catch (Exception ex) {
         System.err.println(ex);
     }
}

Now it should print the reason for the failure to stderr 现在应该打印出stderr失败的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM