我从一篇文章中复制了兔子mqqt代码。
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
public class MqttPublishSample {
public static void main(String[] args) {
String topic = "MQTT Examples";
String content = "Message from MqttPublishSample";
int qos = 0;
String broker = "tcp://127.0.0.1:1883";
String clientId = "pahomqttpublish1";
try {
MqttClient sampleClient = new MqttClient(broker, clientId);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName("guest");
connOpts.setPassword("guest".toCharArray());
connOpts.setCleanSession(true);
System.out.println("Connecting to broker: " + broker);
sampleClient.connect(connOpts);
System.out.println("Connected");
System.out.println("Publishing message: " + content);
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos);
sampleClient.publish(topic, message);
System.out.println("Message published");
sampleClient.disconnect();
System.out.println("Disconnected");
System.exit(0);
} 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();
}
}
}
运行此代码时出错,这不是Qos问题 连接 sampleClient.connect(connOpts);
时出错**控制台错误**
Connecting to broker: tcp://127.0.0.1:1883
reason 32109
msg Connection lost
loc Connection lost
cause java.io.EOFException
excep Connection lost (32109) - java.io.EOFException
Connection lost (32109) - java.io.EOFException atrg.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146) at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException at java.io.DataInputStream.readByte(DataInputStream.java:267) at rg.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65) at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
Rabbitmq错误日志
=ERROR REPORT==== 19-Aug-2016::17:24:54 ===
** Generic server <0.469.0> terminating
** Last message in was
{inet_async,#Port<0.12379>,4714,{ok,[16,42,0,4,77,81,84,84,4,194,0,60,0,16,112,97,104,111,109,113,116,116,112,117,98,108,105,
115,104,49,0,5,103,117,101,115,116,0,5,103,117,101,115,116]}}`
** When Server state == {state,#Port<0.12379>,"127.0.0.1:34033 -> 127.0.0.1:1883",true,running,false,none,{proc_state,#Port<0.12379>,
{dict,0,16,16,8,80,48,{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},{{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]}}},
{undefined,undefined},{0,nil},{0,nil},undefined,1,undefined,undefined,undefined,{undefined,undefined},undefined,<<"amq.topic">>}}
Reason for termination ==
{{badfun,none},[{rabbit_mqtt_frame,parse,2,[{file,"rabbitmq-mqtt/src/rabbit_mqtt_frame.erl"},{line,39}]},
{rabbit_mqtt_reader,process_received_bytes,2,[{file,"rabbitmq-mqtt/src/rabbit_mqtt_reader.erl"},{line,136}]},{gen_server2,handle_msg,2,[{file,"src/gen_server2.erl"},{line,934}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,239}]}]}