Help with publishing [message #1764155] |
Fri, 26 May 2017 02:46 |
Felipe Braz Messages: 1 Registered: May 2017 |
Junior Member |
|
|
I'm new (and newbie) on development so I'm trying to learn and understand.
I'm using Eclipse Paho Java and I'm having some troubles with MqttClient and Publish.
I've created this class to be called by another methods to send messages:
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 mqttSend {
public void enviaMensagem(String content) {
String topic = "msgTratada";
String broker = "tcp://192.168.1.150:1883";
String clientId = "Servidor";
try{
MqttClient client = new MqttClient(broker, clientId);
MqttConnectOptions conOpt = new MqttConnectOptions();
System.out.println("Oi");
if(client.isConnected() == false){
client.connect(conOpt);
}
MqttMessage message = new MqttMessage(content.getBytes());
client.publish(topic, message);
client.disconnect();
} 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();
}
}
}
My whole program works just fine, when I call this function the message is delivered and the program just closes. If I don't call it any time the program keeps working for hours, but in the moment I need to send a message it sends and close the all program.
What I did wrong?
My intention is having a method for calling every time I have to send a message, but the program needs to keep running and I should be able to send the amount of messages I want, anytime I want.
Could anyone help me?
Thanks!!
|
|
|
Powered by
FUDForum. Page generated in 0.82021 seconds