- Introduction to the Service Location Protocol
- Deployment scenarios
- Examples
- Why LiveTribe :: SLP does not implement RFC 2614
- Frequently Asked Questions
How To Leverage LiveTribe SLP in OSGi
LiveTribe SLP can be leveraged in an OSGi environment with very little programming by using instances of OSGi ServiceTracker and configuration managers. You can directly use these trackers or read their code to learn how to roll your own instances. Some examples are shown below.
Readers who are new to SLP should begin reading the Wikipedia page Service Location Protocol first. The Wikipedia page OSGi is a great place to start becoming familiar with OSGi.
Advertising Using OSGi Service Properties
There are times where you may want to automatically, with little programming, advertise a service URL when your OSGi service is registered and automatically remove it when the OSGi service is unregistered. You can do this by registering the OSGi service tracker ByServicePropertiesServiceTracker and adding the OSGi service property slp.url to your OSGi service.
The OSGi service tracker ByServicePropertiesServiceTracker uses the OSGi filter (slp.url=*) to listen to OSGi services that wish to have SLP service URLs registered with the SLP service agent. Such OSGi services that get unregistered will automatically have their SLP service URLs unregistered by the OSGi service tracker.
A single instance of the OSGi service tracker ByServicePropertiesServiceTracker can be shared amongst all the services in the OSGi container.
A typical setup might include a single "shared" bundle which would be in charge of creating and managing the SLP service agent that would be in charge of advertising the SLP service URLs and the OSGi service tracker that would listen for OSGi service registrations.
All of this would be done in a BundleActivator.
public class AcmeActivator implements BundleActivator
{
private ServiceAgent serviceAgent;
private ByServicePropertiesServiceTracker tracker;
public void start(BundleContext bundleContext) throws Exception
{
serviceAgent = SLP.newServiceAgent(null);
serviceAgent.start();
tracker = new ByServicePropertiesServiceTracker(bundleContext, serviceAgent);
tracker.open();
}
public void stop(BundleContext bundleContext) throws Exception
{
tracker.close();
serviceAgent.stop();
}
}
Advertising By Registering ServiceInfo In The OSGi Service Registry
One can obtain a finer degree of control of the SLP service URL registration. This can be done by registering the OSGi service tracker ByServiceInfoServiceTracker and registering an instance of ServiceInfo with the OSGi service registry. Such OSGi services that get unregistered will automatically have their SLP service URLs unregistered by the OSGi service tracker.
A single instance of the OSGi service tracker ByServiceInfoServiceTracker can be shared amongst all the services in the OSGi container.
A typical setup might include a single "shared" bundle which would be in charge of creating and managing the SLP service agent that would be in charge of advertising the SLP service URLs and the OSGi service tracker that would listen for OSGi service registrations.
All of this would be done in a BundleActivator.
public class AcmeActivator implements BundleActivator
{
private ServiceAgent serviceAgent;
private ByServiceInfoServiceTracker tracker;
public void start(BundleContext bundleContext) throws Exception
{
serviceAgent = SLP.newServiceAgent(null);
serviceAgent.start();
tracker = new ByServiceInfoServiceTracker(bundleContext, serviceAgent);
tracker.open();
}
public void stop(BundleContext bundleContext) throws Exception
{
tracker.close();
serviceAgent.stop();
}
}
Monitoring SLP Service URL Registrations Using An OSGi Service Tracker
The ServiceNotificationListenerServiceTracker simplifies the management of SLP service notification listeners by leveraging the OSGi service registry mechanism. This has the added benefit of automatic deregistration of the listeners should the bundle become unresolved.
Bundles wishing to register an SLP service notification listeners merely need to register an instance of ServiceNotificationListener in the OSGi service registry.
A typical setup might include a single "shared" bundle which would be in charge of creating and managing the SLP service agent that would be in charge of advertising the SLP service URLs and the OSGi service tracker that would listen for OSGi service registrations.
All of this would be done in a BundleActivator.
public class AcmeActivator implements BundleActivator
{
private UserAgent userAgent;
private ByServiceInfoServiceTracker tracker;
public void start(BundleContext bundleContext) throws Exception
{
userAgent = SLP.newUserAgent(null);
userAgent.start();
tracker = new ServiceNotificationListenerServiceTracker(bundleContext, userAgent);
tracker.open();
}
public void stop(BundleContext bundleContext) throws Exception
{
tracker.close();
userAgent.stop();
}
}
A bundle wishing to be notified of SLP service URL registrations would register the listener.
public class AcmeListenerActivator implements BundleActivator, ServiceNotificationListener
{
private final AtomicInteger counter = new AtomicInteger();
public void start(BundleContext bundleContext) throws Exception
{
bundleContext.registerService(ServiceNotificationListener.class.getName(),
this,
null);
}
public void stop(BundleContext bundleContext) throws Exception
{
}
public void serviceRegistered(ServiceNotificationEvent event)
{
if ("service:jmx:rmi:///jndi/jmxrmi".equals(event.getService().getServiceURL().getURL()))
{
counter.incrementAndGet();
}
}
public void serviceDeregistered(ServiceNotificationEvent event)
{
if ("service:jmx:rmi:///jndi/jmxrmi".equals(event.getService().getServiceURL().getURL()))
{
counter.decrementAndGet();
}
}
}
Using OSGi's Configuration Admin Service
You can use OSGi's configuration admin service to create instances of SLP service and user agents.
public class AcmeActivator implements BundleActivator
{
private UserAgentManagedService service;
private ServiceRegistration registration;
public void start(BundleContext bundleContext) throws Exception
{
service = new UserAgentManagedService(bundleContext, false);
registration = bundleContext.registerService(UserAgentManagedService.class.getName(),
this,
null);
}
public void stop(BundleContext bundleContext) throws Exception
{
registration.unregister();
service.close();
}
}
The OSGi configuration admin service can then be used to instantiate and configure an instance of an SLP user agent. To configure multiple SLP user agents use UserAgentManagedServiceFactory.
