How to determine if a contentfilteredtopic already exists?

8 posts / 0 new
Last post
Offline
Last seen: 4 years 10 months ago
Joined: 08/03/2017
Posts: 13
How to determine if a contentfilteredtopic already exists?

Hi everyone, 

I have an application that starts/stops within the same JVM (with several other applications that use DDS and the same sets of topics). Each app creates its own readers/writers and cleans them up when they shut down. Though as to not interrupt other applications, it doesn't remove the topics. These applications share participants and a subscriber/publisher. 

So I am able to check to see if a topic has already been created via

topic_descr = participant.lookup_topicdescription(key); 

If that is found, I can use the following to find the particular Topic object: 

topic = participant.find_topic(key, new Duration_t(5, 0));

How can I do the same for a content filtered topic? 

Simply, I'm trying to find a way to see if a contentfiltered topic exists instead of trying to create a second version of it and it erroring out. If it exists, get the ContentFilteredTopic object back.

I am aware that calling create_contentfilteredtopic() on an existing topic will return null, but that also returns and outputs an ugly D0000: create content filtered topic error line.

Thanks!

Offline
Last seen: 3 years 4 months ago
Joined: 08/09/2017
Posts: 25

You can use participant.lookup_topicdescription() and then cast the returned object as a ContentFilteredTopic (and check for null).  Something like:

ContentFilteredTopic cft = participant.create_contentfilteredtopic("MyCFT", topic, "val=0", new StringSeq());
TopicDescription td = participant.lookup_topicdescription("MyCFT");
if (td != null) {
  ContentFilteredTopic othercft = (ContentFilteredTopic) td;
  System.out.println("Successfully looked up " + othercft);
}

Would this work for you?

 

 

Offline
Last seen: 4 years 10 months ago
Joined: 08/03/2017
Posts: 13

Mike. Thanks for the reply.

There are three issues/questions that I have with this solution:

1) if you just cast the regular topic to a contentfilteredtopic and create a reader from it, wouldn't it just give you a non-content filtered reader? Because aren't you just in essence getting the topic description of the non-content filtered topic? Because for each topic we create the regular topic and then create the contentfilteredtopic if it has filter options. Then we create the reader from either regular topic or if it has filters, the CFT instead. So what prevents this code from returning the non-content filtered version?

2) I don't think you can straight cast a TopicDescription to a ContentFilteredTopic because I just fixed a runtime exception in this code that attempted to cast a TopicDescription to a regular Topic and got a ClassCastException. 

3) As I mentioned above, calling create_contentfilteredtopic() and having it fail (return null) also outputs an error in the log window from somewhere in the RTI code D0000 !create content filtered topic. 

Regards.

Offline
Last seen: 1 month 6 days ago
Joined: 09/07/2018
Posts: 20

Hi Tacitus86, it looks like you're a supported user at your organization. I'll send you an email and we can continue working on this question via a formal support case.

Best regards,

Grayson Honan
RTI | Software Engineer

Offline
Last seen: 3 years 4 months ago
Joined: 08/09/2017
Posts: 25

Hi. I'll take a shot at answering your questions.

1) TopicDescription is the base class for Topic and ContentFilteredTopic.  The object returned by lookup_topicdescription() is one or the other.  If it is a Topic, then creating a new reader using it would result in a regular reader.  If a ContentFilteredTopic, then a content filtered reader.  You can determine which using the instanceof Java operator.  Yes, instanceof is frowned on in some situations, but this is one case where I think it is a reasonable thing to do.

2) Doing this cast will work.  I ran the code excerpt I provided before making the post.  This is a perfectly valid thing to do as long as it IS a ContentFilteredTopic object, which you can determine using instanceof.

3) I agree that calling create_contentfilteredtopic() is not a great way of checking whether the CFT already exists.  But I think using lookup_topicdescription() is a good way to do it.

Does this help?

Thanks!

Offline
Last seen: 4 years 10 months ago
Joined: 08/03/2017
Posts: 13

Hey Mike. 

I got the classCastException when I tried it (I'm using DDS 5.3.1 if that matters) but let me give it a shot with instanceof. Though what happens using lookup when you know there is a regular topic AND a contentFilteredTopic of the same topic? 

Offline
Last seen: 3 years 4 months ago
Joined: 08/09/2017
Posts: 25

Hi.  I am also using 5.3.1.

I'm not sure which one you would get if you created a topic and content filtered topic using the same name string.  I suggest avoiding that as a matter of policy.  Keep in mind that the name of a content filtered topic is arbitrary.  The CFT is not a DDS entity, but rather the application of filtering logic on top of a real DDS Topic.

Offline
Last seen: 4 years 10 months ago
Joined: 08/03/2017
Posts: 13

Mike, that worked perfectly. The key missing information there for me was that the name was arbitrary. After making all the regular topics just topicName and making all the CFTs topicName + CFT, the casting worked fine. Thank you so much for your help!