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.
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 followingread()in a loop:The
read()method invoked like this will return all currently available samples in yourFooReader. After theread(), those samples still remain available in theFooReader, that is how theread()method behaves. Think of a read as a “peek”. The next time that you invoke theread()method in this way, you will see all samples that you saw before, unless they have been overwritten by a new update from aDataWriter.To resolve your issue, you could replace the
read()with atake(), like this:The
take()method is different from theread()method in that it does a destructive read; it not only reads the data but also removes it fromFooReader. That way, you will never receive the same sample twice. In fact, if you consistently usetake()as opposed toread(), you will never be able to see any sample twice.Another way to resolve your issue is to stick with
read(), but adjust the requestedSAMPLE_STATE, fromANYtoNOT_READ, like this: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 yourFooReader, which might be useful if you want to re-read it at a later stage (in which case you need to use theANYsample state as opposed toNOT_READto obtain previously read samples).