Skip to main content



      Home
Home » Eclipse Projects » Paho » Help with publishing
Help with publishing [message #1764155] Thu, 25 May 2017 22:46
Eclipse UserFriend
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!!

Previous Topic:multiple publish not working
Next Topic:Client not connecting
Goto Forum:
  


Current Time: Thu Jun 12 15:45:35 EDT 2025

Powered by FUDForum. Page generated in 0.05032 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top