I have a class with 2 methods, each publishing and subscribing to a common MQTT broker separately.
public class operations{
public void fetch(){
// do something
mqttConn.mqttSubscriberFetch(MQTTtopic);
// do something
}
public void store(){
// do something
mqttConn.mqttSubscriberStore(MQTTtopic);
// do something
}
}
And the MQTT method for method fetch is as follows:
public class mqttConnectionFetch implements MqttCallback{
MqttClient client;
String topic;
public mqttConnectionFetch() {
}
public void mqttSubscriberFetch(String topic) {
final String MQTT_BROKER = "tcp://localhost:1883" ;
final String CLIENT_ID = "fetch";
try {
client = new MqttClient(MQTT_BROKER, CLIENT_ID);
client.connect();
client.setCallback(this);
client.subscribe(topic);
MqttMessage message = new MqttMessage();
} catch (MqttException e) {
e.printStackTrace();
}
}
@Override
public void connectionLost(Throwable cause) {
// TODO Auto-generated method stub
}
@Override
public void messageArrived(String topic, MqttMessage message)
throws Exception {
System.out.println("the message received is "+message);
if(message.toString().equals("Processed")) {
MqttPublisherFetch("Processed", "operations/fetch");
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
// TODO Auto-generated method stub
}
public void MqttPublisherFetch(String message, String topic) {
final String MQTT_BROKER = "tcp://localhost:1883" ;
final String CLIENT_ID = "store";
try {
client = new MqttClient(MQTT_BROKER, CLIENT_ID);
client.connect();
createMqttMessage(message,topic);
client.disconnect();
} catch (MqttException e) {
e.printStackTrace();
}
}
private void createMqttMessage(String message, String topic) throws MqttException {
MqttMessage publishMessage = new MqttMessage();
publishMessage.setPayload(message.getBytes());
client.publish(topic, publishMessage);
}
}
Now i am trying to have such a functionality that whenever my fetch method is subscribing to a topic, if the message from the broker is "Processing" , it should go on wait state. While the store method should be working normally. And when the message received is "Processed", the fetch should again start working.
I tried this with normal java wait() and start(), But I am not getting the desired output. Can someone help me to demystify this ?