0

I've been looking for all over internet. I have tried setting cleansession "false" and qos 1 and 2 yet subscriber is not getting all the content when he comes online. Please help... my code is

Example.java(Producer)

public class Example extends PersonBean {
public  void hey(){
String clientId = MqttClient.generateClientId();
 MemoryPersistence persistence = new MemoryPersistence();

    PersonBean pb=new PersonBean();
    for(int i=1;i<=5;i++){
        Gson gson = new Gson();

        Date dt=new Date();
         DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
         String currentTime = df.format(dt);
    pb.setId(i);
    pb.setName("sai");
    pb.setEmail("s@g.com");
    pb.setAddress("hyderabad");
    pb.setCreatedOn(currentTime);


    String jsonInString = gson.toJson(pb);


        try {
            String broker = "tcp://localhost:1883";
            String topicName = "test/mqtt";
            int qos = 2;

    MqttClient mqttClient = new MqttClient(broker,clientId);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(false);
            mqttClient.connect(connOpts);

            MqttMessage message = new MqttMessage(jsonInString.getBytes());

            message.setQos(qos);
    message.setRetained(true);


            MqttTopic topic2 = mqttClient.getTopic(topicName);
topic2.publish(message);

        mqttClient.disconnect();
        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode() + " - msg "
                    + me.getMessage() + "- loc " + me.getLocalizedMessage()
                    + " - cause " + me.getCause() + "- exception " + me);

        }


    }}

    public static void main(String[] args) {
    Example ex=new Example();
    ex.hey();
}}

and my

Subscriber.java

public class SubcriberExample implements MqttCallback{

MqttClient client;
public void doDemo() {
    try {
        client = new MqttClient("tcp://192.168.4.189:1883", "Sending");
        client.connect();
        client.setCallback(this);
        client.subscribe("test/mqtt");



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

public static void main(String args[]){
    SubcriberExample se=new SubcriberExample();
    se.doDemo();
}

@Override
public void connectionLost(Throwable arg0) {
    // TODO Auto-generated method stub
    System.out.println("connection lost....");
}

@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
    // TODO Auto-generated method stub

}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
    // TODO Auto-generated method stub
    System.out.println("message is : "+message);
}}
Sai
  • 13
  • 4

1 Answers1

3

You're on the right track, the published messages are required to be QoS1/2 to be eligible for being held in an offline queue (for an offline subscriber).

However, from the code above it seems the problem is in the subscriber. For an MQTT subscriber to be able to receive offline messages, then it needs to have a persistent session. I.e. the subscriber needs to connect with clean session = false.

  • thanks for the reply.. but its not working. I've added client = new MqttClient("tcp://192.168.4.189:1883", "Sending"); MqttConnectOptions mqOptions=new MqttConnectOptions(); mqOptions.setCleanSession(false); client.connect(mqOptions); client.subscribe("test/mqtt"); client.setCallback(this); – Sai Dec 05 '16 at 08:48
  • I think the subscriber needs to subscribe with QoS 1/2 as well, otherwise the QoS is downgraded to QoS 0 (which I guess i default with the client) and the messages will not be stored. – Lars Hesel Christensen Dec 05 '16 at 09:18
  • do you have any small example in Java? please – Sai Dec 05 '16 at 09:32
  • I'm afraid not. Sorry. I can recommend the mosquitto command line tools (`mosquitto_sub` and `mosquitto_pub`) to test things out. They are quite easy to work with and can maybe help you with experiments and getting you confident with how MQTT (and the broker you are using) works. – Lars Hesel Christensen Dec 05 '16 at 09:36