diff options
author | Dwayne Boone <dwayne@zeroc.com> | 2005-10-21 15:04:24 +0000 |
---|---|---|
committer | Dwayne Boone <dwayne@zeroc.com> | 2005-10-21 15:04:24 +0000 |
commit | 72b06332d8ed2676d662436e42462f3a5bad3204 (patch) | |
tree | 6cb79cda0874cbe8fe0b1bbf3f94f4b60385e267 /java/demo/IceStorm/clock/Subscriber.java | |
parent | Fix (diff) | |
download | ice-72b06332d8ed2676d662436e42462f3a5bad3204.tar.bz2 ice-72b06332d8ed2676d662436e42462f3a5bad3204.tar.xz ice-72b06332d8ed2676d662436e42462f3a5bad3204.zip |
Ported IceStorm demo
Diffstat (limited to 'java/demo/IceStorm/clock/Subscriber.java')
-rw-r--r-- | java/demo/IceStorm/clock/Subscriber.java | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/java/demo/IceStorm/clock/Subscriber.java b/java/demo/IceStorm/clock/Subscriber.java new file mode 100644 index 00000000000..a6c5c476e18 --- /dev/null +++ b/java/demo/IceStorm/clock/Subscriber.java @@ -0,0 +1,144 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +import Demo.*; + +public class Subscriber extends Ice.Application +{ + public int + run(String[] args) + { + Ice.Properties properties = communicator().getProperties(); + + final String proxyProperty = "IceStorm.TopicManager.Proxy"; + String proxy = properties.getProperty(proxyProperty); + if(proxy == null) + { + System.err.println("property `" + proxyProperty + "' not set"); + return 1; + } + + Ice.ObjectPrx base = communicator().stringToProxy(proxy); + IceStorm.TopicManagerPrx manager = IceStorm.TopicManagerPrxHelper.checkedCast(base); + if(manager == null) + { + System.err.println("invalid proxy"); + return 1; + } + + // + // Gather the set of topics to which to subscribe. It is either + // the set provided on the command line, or the topic "time". + // + java.util.List topics = new java.util.ArrayList();; + if(args.length > 1) + { + for(int i = 0; i < args.length; ++i) + { + topics.add(args[i]); + } + } + else + { + topics.add("time"); + } + + // + // 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. + // + java.util.Map subscribers = new java.util.HashMap();; + + // + // Add the servant to the adapter for each topic. A ServantLocator + // could have been used for the same purpose. + // + 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; + } + + // + // 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); + } + + // + // 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(); + } + + // + // 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); + } + } + + return 0; + } + + public static void + main(String[] args) + { + Subscriber app = new Subscriber(); + int status = app.main("Subscriber", args, "config"); + System.exit(status); + } +} |