Perform Action in MQTT Subscriber

Viewed 267

This mqtt subscriber code works fine. I can easily subscribe to messages which are published at broker.hivemq.com with respective topic.

public class AccelerometerSubscriber  implements MqttCallback,  
IMqttActionListener {

public static void main(String[] args)  throws MqttException  {
int QUALITY_OF_SERVICE = 2;
MqttClient client=new MqttClient("tcp://broker.hivemq.com:1883", 
MqttClient.generateClientId());
client.setCallback( new SimpleMqttCallBack() );
client.connect();
System.out.println("Subscribing ....");
client.subscribe("MQTT Examples");    }

System.out.println("some action"); //------------right here--------------

public void connectionLost(Throwable throwable) {

System.out.println("Connection to MQTT broker lost!");  }


public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {

  System.out.println("Message received:\n\t"+ new String(mqttMessage.getPayload()) );

}   

 public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

  // not used in this example

}}

Now I want to perform action only when a message is received. I'm unable to do that.

1 Answers
Related