Archived

This forum has been archived. Please start a new discussion on GitHub.

TopicManagerPrx: Strange Java code or am I missing something?

Hi

My apologies if I'm way out of line.

DISCLAIMER: I' new to Ice and not a Java expert, but experimenting with both as part of investigating strategies for some new projects.

I'm reading Chapter 45 (IceStorm). The java examples uses the following code:
try {
topic = topicManager.retrieve("Weather");
}
catch (IceStorm.NoSuchTopic ex) {
topic = topicManager.create("Weather");
}

The Java compiler complains about the unhandled exception thrown by topicManager.create (TopicExists). Which means I have to add handler code to handle an exception which should not be thrown because I tried to retrieve the topic first and it did not exist. Adding the handler results in code which just seems strange...

try {
topic = topicManager.retrieve("Weather");
}
catch (IceStorm.NoSuchTopic ex) {
try {
topic = topicManager.create("Weather");
} catch (TopicExists e) {
e.printStackTrace();
}
}

Thanks.

Comments

  • matthew
    matthew NL, Canada
    Yes, the code in the manual seems to be broken. If you look at java/demo/IceStorm/clock/Publisher.java you'll see:
    IceStorm.TopicPrx topic;
    try
    {
        topic = manager.retrieve(topicName);
    }
    catch(IceStorm.NoSuchTopic e)
    {
        try
        {
            topic = manager.create(topicName);
         }
         catch(IceStorm.TopicExists ex)
         {
             System.err.println(appName() + ": temporary failure, try again.");
             return 1;
          }
    }
    

    It is possible to hit this error condition if you run two clients at the same time.
  • I was wondering if it is possible to enter that condition. Shouldn't "production" code cater for that situation?

    The solution does seem ugly. Al that I can think of is to have a loop that retries to first get the topic and then to create it until the topic var is not null...:confused:
  • bernard
    bernard Jupiter, FL
    Hi Kobus,

    For production code, I think a more sensible setup is to:
    - create your topic or topics when you deploy the application (and not "on-demand")
    - in your publishers and subscribers, fail if the desired topics don't exist

    Best regards,
    Bernard