Multiple datareaderlistener in different classes

2 posts / 0 new
Last post
Last seen: 1 month 1 week ago
Joined: 02/25/2019
Posts: 13
Multiple datareaderlistener in different classes

I have a topic and there is a reader for this topic, reader is shared with two different classes , can i attach two datareaderlistener for this shared reader in respective classes and implement on_data_available in respective classes, so that logic and implementation according to that particular class is get executed. Code is developed in java. Can anyone suggest what should i do , when i need to read same topic data in two different class on_data_available. There is  a need for creating two different classes they are independently executed.

Howard's picture
Last seen: 2 days 7 hours ago
Joined: 11/29/2012
Posts: 383

Well, fundamentally, you want to process the data being received by a DataReader twice, with invocations to 2 diffferent processing functions.

Unfortunately, there's nothing in the DDS API that supports that directly.  You have to write code to take the data from a DataReader and pass it to 2 different methods on 2 different objects to execute.  There is no magic available to do that. 

Fundamentally, the listener of the DataReader is executed by an internal DDS thread.  That thread is doing lots of work...and by default, dealing with the data being received for ALL datareaders in a participant. 

Which is why the best practice is not to use the on_data_available() listener to process data except in the most performant and trivial way.  Fundamentally, the internal DDS thread can't do its own work (taking data out of socket buffers, deserializing and storing in DataReaders) until your code in on_data_available() finishes.

And of course, this situation will be exacerbated when the same data needs to be processed multiple times.  Which is why the best practice is to use your own thread to process the data.  You can do this by blocking your thread on a DDSWaitSet, which will unblock when configured to wake up when data arrives for a DataReader.

Then your thread can get the data from the DataReader and pass the data to as many objects that want to process the data received.