3

I have a problem with subscribing the data (using the java platform). When a subscriber subscribes to a topic, that subscribed data must be removed from the DDS. But in my case whenever I subscribe to the data the same data is subscribed many times. The data is not removed from the DDS. I tried with QoS but I don't know how to use it.

Please suggest how I can remove the read data from the DDS.

Reinier Torenbeek
  • 16,669
  • 7
  • 46
  • 69
Bathakarai
  • 1,517
  • 6
  • 23
  • 39

1 Answers1

4

This behavior is not caused by your QoS settings, but by your method of accessing the DataReader. When you retrieve your data, you are probably calling something like the following read() in a loop:

FooReader.read(
    dataSeq, infoSeq, 10,
    ANY_SAMPLE_STATE.value,
    ANY_VIEW_STATE.value,
    ANY_INSTANCE_STATE.value);

The read() method invoked like this will return all currently available samples in your FooReader. After the read(), those samples still remain available in the FooReader, that is how the read() method behaves. Think of a read as a "peek". The next time that you invoke the read() method in this way, you will see all samples that you saw before, unless they have been overwritten by a new update from a DataWriter.

To resolve your issue, you could replace the read() with a take(), like this:

FooReader.take(
    dataSeq, infoSeq, 10,
    ANY_SAMPLE_STATE.value,
    ANY_VIEW_STATE.value,
    ANY_INSTANCE_STATE.value);

The take() method is different from the read() method in that it does a destructive read; it not only reads the data but also removes it from FooReader. That way, you will never receive the same sample twice. In fact, if you consistently use take() as opposed to read(), you will never be able to see any sample twice.

Another way to resolve your issue is to stick with read(), but adjust the requested SAMPLE_STATE, from ANY to NOT_READ, like this:

FooReader.read(
    dataSeq, infoSeq, 10,
    NOT_READ_SAMPLE_STATE.value,
    ANY_VIEW_STATE.value,
    ANY_INSTANCE_STATE.value);

That way, you will only read samples that you have not read previously. The difference with take() in this case is that the data does remain available in your FooReader, which might be useful if you want to re-read it at a later stage (in which case you need to use the ANY sample state as opposed to NOT_READ to obtain previously read samples).

Reinier Torenbeek
  • 16,669
  • 7
  • 46
  • 69
  • Thank you Reinier. This is really userful and solve my issue. – Bathakarai Jul 13 '12 at 04:06
  • 1
    You are welcome. Please make sure to accept any responses that answer your questions by clicking on the check-mark -- you currently have accepted zero answers for any of your six questions. See some instructions [here](http://stackoverflow.com/faq#howtoask) – Reinier Torenbeek Jul 13 '12 at 04:11
  • Hi Rinier, Now, what my requirement is I have many subscribers but the publisher is single. My subscriber reads the data from the DDS and checks the message is for that particular subscriber. If the checking success then only it takes the data and remove from DDS. The message must maintain in DDS until the authenticated subscriber takes it's data. How can i achieve this using DDS (in java environment)?? – Bathakarai Jul 16 '12 at 08:38
  • Since this is a separate kind of issue that probably requires some of discussion, please pose this as a new question. – Reinier Torenbeek Jul 16 '12 at 10:57