[英]How to publish data to using MQTT
I used this docker image to install Mosquitto MQTT. 我使用此 docker映像安装Mosquitto MQTT。 Now it's running and showing the following message in the terminal: 现在它正在运行,并在终端中显示以下消息:
1515680808: mosquitto version 1.4.14 (build date Mon, 10 Jul 2017 23:48:43 +0100) starting
1515680808: Config loaded from /mqtt/config/mosquitto.conf.
1515680808: Opening websockets listen socket on port 9001.
1515680808: Opening ipv4 listen socket on port 1883.
1515680808: Opening ipv6 listen socket on port 1883.
Then I created a simple Maven project: 然后,我创建了一个简单的Maven项目:
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-mqtt_2.11</artifactId>
<version>1.6.3</version>
</dependency>
I tried to publish some data to a topic using the code shown below. 我尝试使用下面显示的代码将一些数据发布到主题。 I point to localhost:1883
as the MqttBrokerUrl
and a topic test
. 我指向localhost:1883
作为MqttBrokerUrl
和一个主题test
。 However, I get this error: 但是,我收到此错误:
Exception in thread "main" java.lang.NullPointerException at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:457) at org.eclipse.paho.client.mqttv3.MqttAsyncClient.(MqttAsyncClient.java:273) at org.eclipse.paho.client.mqttv3.MqttAsyncClient.(MqttAsyncClient.java:167) at org.eclipse.paho.client.mqttv3.MqttClient.(MqttClient.java:224) at org.test.MQTTPublisher$.main(MQTTPublisher.scala:37) at org.test.MQTTPublisher.main(MQTTPublisher.scala) org.eclipse.paho.client.mqttv3.MqttAsyncClient。(MqttAsyncClient.java:org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:457)处的线程“ main”中的异常。 273)在org.eclipse.paho.client.mqttv3.MqttAsyncClient。(MqttAsyncClient.java:167)在org.eclipse.paho.client.mqttv3.MqttClient。(MqttClient.java:224)在org.test.MQTTPublisher $。 main(MQTTPublisher.scala:37)在org.test.MQTTPublisher.main(MQTTPublisher.scala)
Code: 码:
package org.test
import org.apache.log4j.{Level, Logger}
import org.eclipse.paho.client.mqttv3._
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.streaming.mqtt._
import org.apache.spark.SparkConf
object MQTTPublisher {
def main(args: Array[String]) {
if (args.length < 2) {
System.err.println("Usage: MQTTPublisher <MqttBrokerUrl> <topic>")
System.exit(1)
}
// Set logging level if log4j not configured (override by adding log4j.properties to classpath)
if (!Logger.getRootLogger.getAllAppenders.hasMoreElements) {
Logger.getRootLogger.setLevel(Level.WARN)
}
val Seq(brokerUrl, topic) = args.toSeq
var client: MqttClient = null
try {
val persistence = new MemoryPersistence()
client = new MqttClient("localhost:1883", MqttClient.generateClientId(), persistence)
client.connect()
val msgtopic = client.getTopic(topic)
val msgContent = "test test test"
val message = new MqttMessage(msgContent.getBytes("utf-8"))
while (true) {
try {
msgtopic.publish(message)
println(s"Published data. topic: ${msgtopic.getName()}; Message: $message")
} catch {
case e: MqttException if e.getReasonCode == MqttException.REASON_CODE_MAX_INFLIGHT =>
Thread.sleep(10)
println("Queue is full, wait for to consume data from the message queue")
}
}
} catch {
case e: MqttException => println("Exception Caught: " + e)
} finally {
if (client != null) {
client.disconnect()
}
}
}
}
The MqttClient()
constructor takes a URI. MqttClient()
构造函数采用URI。
What you have provided is just a hostname and port number ( localhost:1883
), it's missing a protocol section which should be tcp://
(which is what the library is expecting and getting null back. This really should throw a better error.) 您提供的只是一个主机名和端口号( localhost:1883
),它缺少一个协议部分,该部分应该为tcp://
(这是库所期望的,并且会返回null。这确实应该抛出一个更好的错误。 )
You need to change the line to be 您需要将行更改为
client = new MqttClient("tcp://localhost:1883", MqttClient.generateClientId(), persistence);
I think you are giving the wrong Url ie you are not specifying the protocol over which it has to connect that is my hunch. 我认为您输入了错误的网址,即您没有指定必须连接的协议,这是我的直觉。
Try changing the url to : 尝试将网址更改为:
tcp://localhost:1883
I think it would work ! 我认为这行得通! Rest all seems fine to me. 休息对我来说似乎很好。
For a working example See this : https://github.com/shiv4nsh/scala-mqtt-client-rasberrypi-starter-kit/blob/master/src/main/scala/com/knoldus/MQTTPublisher.scala 有关工作示例,请参见: https : //github.com/shiv4nsh/scala-mqtt-client-rasberrypi-starter-kit/blob/master/src/main/scala/com/knoldus/MQTTPublisher.scala
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.