MQTT - 订阅方法不起作用

时间:2015-11-01 20:33:36

标签: java mqtt subscribe

我有这个正在处理的Mqtt ckient应用程序,发布方法工作正常但是我很难使用subscribe方法。

这是订阅方法,我想点击按钮来订阅主题:

select q,r from a where s="whatever", q="whatever"
UNION
select q,r from b where t="whatever", q="whatever"
UNION
select q,r from c where u="whatever", q="whatever"
ORDER BY 1 -- If you want to order by specific colum and columns'name are different in the two select

这是按钮的actionPerformed方法

 public void subscribe( MqttClient MC) {

 String topic = jTextField3.getText();
 int qos = jComboBox1.getSelectedIndex() ;

 String[] topics = {topic};

 int[] QoS = {qos};

 if ( jLabel3.getText().equals("Connected") ) {

    try {

    MC.subscribe( topics, QoS );

          System.out.println(topics +"    "+QoS);
                 System.out.println(topic +"    "+qos);
            jButton2.setText("Subscribed");
            jTextField4.setText(topics.toString());

        } catch ( Exception ex ) {
   JOptionPane.showMessageDialog( this, ex.getMessage(), 
    "MQTT Subscription Exception", JOptionPane.ERROR_MESSAGE );
        }   
    } else {
    jTextArea1.setText("Not connected");
    }       
}   

}

这是我发布的获得方法:

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    subscribe(MC);

有人应该在这里帮助我。

1 个答案:

答案 0 :(得分:0)

您需要实现回调和消息到达功能。该函数将处理该消息。请参阅paho mqtt message client作为示例Subscribe and Read MQTT Message Using PAHO