summaryrefslogtreecommitdiff
path: root/java/demo/IceStorm/clock/Subscriber.java
diff options
context:
space:
mode:
authorDwayne Boone <dwayne@zeroc.com>2006-11-15 15:15:48 +0000
committerDwayne Boone <dwayne@zeroc.com>2006-11-15 15:15:48 +0000
commit2f55717289d3c6820317434ef61669aba5763913 (patch)
treea02f62b4df6ec4f88ddadfe82ac391b001492f88 /java/demo/IceStorm/clock/Subscriber.java
parentBug 1547 (diff)
downloadice-2f55717289d3c6820317434ef61669aba5763913.tar.bz2
ice-2f55717289d3c6820317434ef61669aba5763913.tar.xz
ice-2f55717289d3c6820317434ef61669aba5763913.zip
Bug 1547
Diffstat (limited to 'java/demo/IceStorm/clock/Subscriber.java')
-rw-r--r--java/demo/IceStorm/clock/Subscriber.java121
1 files changed, 41 insertions, 80 deletions
diff --git a/java/demo/IceStorm/clock/Subscriber.java b/java/demo/IceStorm/clock/Subscriber.java
index 4c451ee2ed6..374c5d183ba 100644
--- a/java/demo/IceStorm/clock/Subscriber.java
+++ b/java/demo/IceStorm/clock/Subscriber.java
@@ -11,6 +11,15 @@ import Demo.*;
public class Subscriber extends Ice.Application
{
+ public class ClockI extends _ClockDisp
+ {
+ public void
+ tick(String date, Ice.Current current)
+ {
+ System.out.println(date);
+ }
+ }
+
public int
run(String[] args)
{
@@ -24,112 +33,64 @@ public class Subscriber extends Ice.Application
return 1;
}
- Ice.ObjectPrx base = communicator().stringToProxy(proxy);
- IceStorm.TopicManagerPrx manager = IceStorm.TopicManagerPrxHelper.checkedCast(base);
+ IceStorm.TopicManagerPrx manager = IceStorm.TopicManagerPrxHelper.checkedCast(
+ communicator().stringToProxy(proxy));
if(manager == null)
{
System.err.println("invalid proxy");
return 1;
}
+ String topicName = "time";
+ if(args.length != 0)
+ {
+ topicName = args[0];
+ }
+
//
- // Gather the set of topics to which to subscribe. It is either
- // the set provided on the command line, or the topic "time".
+ // Retrieve the topic named "time".
//
- java.util.List topics = new java.util.ArrayList();;
- if(args.length > 1)
+ IceStorm.TopicPrx topic;
+ try
{
- for(int i = 0; i < args.length; ++i)
- {
- topics.add(args[i]);
- }
+ topic = manager.retrieve(topicName);
}
- else
+ catch(IceStorm.NoSuchTopic e)
{
- topics.add("time");
+ try
+ {
+ topic = manager.create(topicName);
+ }
+ catch(IceStorm.TopicExists ex)
+ {
+ System.err.println("temporary failure, try again.");
+ return 1;
+ }
}
- //
- // Set the requested quality of service "reliability" =
- // "batch". This tells IceStorm to send events to the subscriber
- // in batches at regular intervals.
- //
- java.util.Map qos = new java.util.HashMap();
- qos.put("reliability", "batch");
-
- //
- // Create the servant to receive the events.
- //
Ice.ObjectAdapter adapter = communicator().createObjectAdapter("Clock.Subscriber");
- Ice.Object clock = new ClockI();
//
- // List of all subscribers.
+ // Add a Servant for the Ice Object.
//
- java.util.Map subscribers = new java.util.HashMap();;
+ Ice.ObjectPrx subscriber = adapter.addWithUUID(new ClockI());
//
- // Add the servant to the adapter for each topic. A ServantLocator
- // could have been used for the same purpose.
+ // This demo requires no quality of service, so it will use
+ // the defaults.
//
- java.util.Iterator p = topics.iterator();
- while(p.hasNext())
- {
- String name = (String)p.next();
-
- //
- // Add a Servant for the Ice Object.
- //
- Ice.ObjectPrx object = adapter.addWithUUID(clock);
- try
- {
- IceStorm.TopicPrx topic = manager.retrieve(name);
- topic.subscribe(qos, object);
- }
- catch(IceStorm.NoSuchTopic e)
- {
- System.err.println(e + " name: " + e.name);
- break;
- }
+ java.util.Map qos = new java.util.HashMap();
- //
- // Add to the set of subscribers _after_ subscribing. This
- // ensures that only subscribed subscribers are unsubscribed
- // in the case of an error.
- //
- subscribers.put(name, object);
- }
+ topic.subscribe(qos, subscriber);
+ adapter.activate();
- //
- // Unless there is a subscriber per topic then there was some
- // problem. If there was an error the application should terminate
- // without accepting any events.
- //
- if(subscribers.size() == topics.size())
- {
- adapter.activate();
- shutdownOnInterrupt();
- communicator().waitForShutdown();
- }
+ shutdownOnInterrupt();
+ communicator().waitForShutdown();
//
// Unsubscribe all subscribed objects.
//
- p = subscribers.entrySet().iterator();
- while(p.hasNext())
- {
- java.util.Map.Entry entry = (java.util.Map.Entry)p.next();
-
- try
- {
- IceStorm.TopicPrx topic = manager.retrieve((String)entry.getKey());
- topic.unsubscribe((Ice.ObjectPrx)entry.getValue());
- }
- catch(IceStorm.NoSuchTopic e)
- {
- System.err.println(e + " name: " + e.name);
- }
- }
+ topic.unsubscribe(subscriber);
return 0;
}