Python Manual Benutzerhandbuch

Seite von 124
Service Registry
41
Any service agent may make queries against its local service registry and get back an immediate result
which reflects the current state of the service registry. A service agent may also subscribe to the service
registry or aspects of it and be notified in real time of changes made to the service registry. When sub-
scribing to the service registry itself, a service agent would be notified of all available services, when
those services join or leave groups and when those services are withdrawn.
Subscribing to the service registry as a whole is a useful debugging tool as it can produce an audit trail
relating to the creation and deletion of services as well as group memberships. When used as a debug-
ging tool as well as in other cases, it may not be appropriate that a service agent be created merely that
the service registry can be queried. To this end, the member functions of the
Service
class relating
to the service registry are also available through the
Monitor
class. In fact, the
Service
class de-
rives from the
Monitor
class.
To setup a subscription against the service registry as a whole, the member function "
subscrib-
eRegistry()
" is used. A subscription to the service registry can later be removed using the member
function "
unsubscribeRegistry()
".
class RegistryMonitor(netsvc.Monitor):
def __init__(self):
netsvc.Monitor.__init__(self)
self.subscribeRegistry(self.announce)
def announce(self,binding,group,status):
if group == None:
# global group
action = "WITHDRAWN"
if status == netsvc.SERVICE_AVAILABLE:
action = "AVAILABLE"
name = binding.serviceName()
identity = binding.agentIdentity()
print "SERVICE-%s: %s (%s)" % (action,`name`,identity)
else:
# specific group
action = "LEAVE"
if status == netsvc.SERVICE_AVAILABLE:
action = "JOIN"
name = binding.serviceName()
identity = binding.agentIdentity()
print "%s-GROUP[%s]: %s (%s)" % \
(action,`group`,`name`,identity)
When making queries or subscriptions against the service registry, details of a specific service are re-
turned in the form of a service binding object. This is the same type of object returned by the "
serv-
iceBinding()
" member function of a specific service agent. Where an operation needs to refer to
a particular service it will be usually done in terms of this service binding object rather than the infor-
mation it carries.