问题
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.
回答1:
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).
来源:https://stackoverflow.com/questions/11445299/remove-read-topic-from-dds