0

I'm trying to implement a fake broker (actually it is an mqtt publisher client in an mqtt subscriber's callback). There are 3 separated publisher clients which are publishing random numbers between 0 and 1. This fake broker just summarizes this random numbers, and publishes away to an other topic. (Maybe not in the right way, but for now it is ok) This solution is working but after a few incoming messages this broker stops to work. I Tried to debug it, but I found only ClassNotFound Exceptions... Here is my FakeBroker and it's Callback implementation.

public class FakeBroker implements Runnable{

public static final String BROKER_URL = "";
public static final String TOPIC_FAKE_A = "";
public static final String TOPIC_FAKE_B = "";
public static final String TOPIC_FAKE_C = "";
public static final String USER_NAME = "";
public static final char[] USER_PSW  = "".toCharArray();

private MqttClient client;
private MqttConnectOptions options;
private SubscriberCallback callback;

public FakeBroker() {
    options = new MqttConnectOptions();
    options.setUserName(USER_NAME);
    options.setPassword(USER_PSW);
    options.setCleanSession(false);
    callback = new SubscriberCallback();
    try {
        client = new MqttClient(BROKER_URL, MqttClient.generateClientId()+"-sub");
        client.setCallback(callback);


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


public void start() {
    try {
        client.connect(options);
        System.out.println("Fake Broker are connected to the cloud.");
        client.subscribe(TOPIC_FAKE_A);
        client.subscribe(TOPIC_FAKE_B);
        client.subscribe(TOPIC_FAKE_C);


    } catch (MqttException e) {
        e.printStackTrace();
    }
}

@Override
public void run() {
    start();
}
}

And here is it's Callback

public class SubscriberCallback implements MqttCallback {

public static final String BROKER_URL = "";
public static final String TOPIC_FAKE_BROKER = "";
public static final String USER_NAME = "";
public static final char[] USER_PSW = "".toCharArray();
private MqttClient client;
private MqttConnectOptions options;
private int counter = 1;
private int result = 0;
public SubscriberCallback() {
    try {
        client = new MqttClient(BROKER_URL, "4-pub");
        options = new MqttConnectOptions();

        options.setPassword(USER_PSW);
        options.setUserName(USER_NAME);
    } catch (MqttException e) {
        e.printStackTrace();
    }

}

@Override
public void connectionLost(Throwable throwable) {


}

@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
    System.out.println("Message Arrived. Topic   " + topic + "    message:     " +mqttMessage + " ---- Message Counter: " + counter);

    int number = Integer.parseInt(mqttMessage.toString());
    result += number;
    if (counter%3 == 0) {
        publishAway(new MqttMessage(Integer.toString(result).getBytes()));
        result = 0;
    }
    incrementCounter();

}

private void publishAway(MqttMessage mqttMessage) throws MqttException {
    client.connect(options);
    final MqttTopic topicFakeBroker = client.getTopic(TOPIC_FAKE_BROKER);
    topicFakeBroker.publish(mqttMessage);
    client.disconnect();
    System.out.println("Fake broker got the message " + mqttMessage + " and published away to" + topicFakeBroker.getName());
}

@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

}

private void incrementCounter() {
    counter++;
}
}

Of course I use valid BROKER_URL and TOPICS but these informations are confidential. Thanks for Your answers! :)

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
pthben
  • 11
  • 2
  • 1
    Edit the question and include the actual Exception information, just posting code and saying it doesn't work is not helpful – hardillb Mar 11 '18 at 19:30
  • As I mentioned, I have found only ClassNotFoundException: on FakeBroker... – pthben Mar 11 '18 at 19:41
  • 1
    Yes but if you show the exception information it will show the Class it's looking for and the stacktrace which will help tell us what the real problem – hardillb Mar 11 '18 at 19:42

0 Answers0