The OSGi Service Platform states that a bundle may export multiple
instances of a service. While this is quite rare, the
BaseBundleActivator does support this need.
BaseBundleActivator provides APIs for handling multiple
exported services of the same type:
void addExportedService(String, Object, Dictionary)
void removeExportedService(String, Object)
void removeExportedServices(String)
void setExportedServiceProperties(String, Object, Dictionary)
Object[] getExportedServices(String)
Dictionary getExportedServiceProperties(String, Object)
Object parameter is always the
exported service.
addExportedService(String, Object, Dictionary) multiple
times. The following example shows how to export multiple
TimerService objects and register them with properties:
protected void activate() {
Timer timer;
Dictionary properties = new Hashtable();
properties.put("cn", "simona");
properties.put("o", "OTI");
properties.put("c", "US");
timer = new Timer(60000);
timer.startup();
setMinuteTimer(timer);
addExportedService(TimerService.SERVICE_NAME, timer, properties);
timer = new Timer(3600000);
timer.startup();
setHourTimer(timer);
addExportedService(TimerService.SERVICE_NAME, timer, properties);
}
getExportedServices(). For example:
protected void deactivate() {
Timer[] timers = (Timer[]) getExportedServices(TimerService.SERVICE_NAME);
Timer timer;
for (int i = 0; i < timers.length; i++) {
timer = timers [ i ];
timer.shutdown();
}
}
getExportedServiceProperties(String, Object) is
used to query the properties that were registered with an exported
service.
setExportedServiceProperties(String, Object, Dictionary) is
used to set the properties of an exported service.
Timer timer = getMinuteTimer();
Dictionary properties = getExportedServiceProperties(TimerService.SERVICE_NAME, timer);
properties.put("o", "IBM");
setExportedServiceProperties(TimerService.SERVICE_NAME, timer, properties);
Copyright © 2001, 2007 IBM Corporation and others. All Rights Reserved.