Mqtt-client JAVA + user interface

1

I'm trying to implement a code that uses the MQTT protocol in Java, now I come from C ++ and a long time ago I did not program (Reasons for university), but I need to take up with a small project in JAVA and better than once to remember my knowledge in POO and finish in JAVA in a good way. My code using MQTT with PAHO libraries, subscribe to a theme and it works fine but, I need the result not to show it to me in the console, but in a panel with JFrame or good, so I thought the idea is to show in graphical interface and not in console, and the vdd I do not know how to do it. I hope you can help me

package com.mycompany.mavenproject1;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
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 Ventana2 extends JFrame implements MqttCallback{

    MqttClient clienteJava;
    MqttConnectOptions connectOptions;

    static final String topic = "Temperatura";    
    static final String Broker = "tcp://iotmosquitto.cloudapp.net:1883";
    static final String ClientID = MqttClient.generateClientId();
    static final Boolean Pub = true;
    static final Boolean Subs = true;

    JPanel Panel;
    JLabel Texto;

    // connectionLost se invoca apenas la conexión se pierda
    @Override
    public void connectionLost(Throwable thrwbl) {
        //throw new UnsupportedOperationException("Not supported yet."); 
        //To change body of generated methods, choose Tools | Templates.
        System.out.println("Se ha perdido la conexíón");

        //Código para reconectar podría ir aquí.
    }

    // messageArrived se invoca cuando un suscriptor del tópico ha recibido el mensaje
    @Override    
    public void messageArrived(String Topico, MqttMessage Message) throws Exception {
        //throw new UnsupportedOperationException("Not supported yet."); 
        //To change body of generated methods, choose Tools | Templates.
        String mensaje = new String(Message.getPayload());
        System.out.println(mensaje);        

    }

    // deliveryComplete se invoca cuando el broker ha recibido exitosamente un mensaje       publicado por este cliente
    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        //throw new UnsupportedOperationException("Not supported yet."); 
        //To change body of generated methods, choose Tools | Templates.

    }

    public void ventana(String mensaje){
        Panel = new JPanel();
        Texto = new JLabel();

        this.add(Panel);
        Panel.add(Texto);
        Texto.setText(mensaje);
    }

    public void ejecutaCliente(){
        // preparando un cliente MQTT

        connectOptions = new MqttConnectOptions();        
        connectOptions.setCleanSession(true);
        connectOptions.setKeepAliveInterval(50);        
        //MqttTopic Topico = clienteJava.getTopic(topic);

        //conexion al broker
        try{
            clienteJava = new MqttClient(Broker, ClientID);
            clienteJava.setCallback(this);
            clienteJava.connect(connectOptions);

        }catch(MqttException e){
            e.printStackTrace();
            System.exit(-1);
        }

        System.out.println("Ha sido conectado al broker " + Broker);

        // Preparando un tópico
        //MqttTopic Topico = clienteJava.getTopic(Topico);

        try{
                int subQoS = 1;
                clienteJava.subscribe(topic,subQoS);
        }catch(Exception e) {
                e.printStackTrace();
        }

    }


}
    
asked by Cristhian Plaza 16.04.2016 в 17:11
source

1 answer

1

The problem was to use threads, since several processes are executed, and without using them it works but the program is not running properly, for them I have simply adapted the program in a better way, to those who are interested I can give the code.

    
answered by 04.05.2016 в 00:51