注册 OSGi 服务
可以创建对象,并将其注册为 OSGi 服务以供第三方功能部件使用。
关于此任务
通过使用无格式的旧 Java 代码,可以创建对象,然后将其注册为使用 BundleContext 类的服务。因为代码必须运行,所以您通常在 BundleActivator 接口中注册对象。注册对象时,可以指定它所提供的接口并提供属性映射。会返回 ServiceRegistration 对象;如有必要,可以随时使用 ServiceRegistration 对象来更改属性。完成服务后,使用 ServiceRegistration 对象来注销服务。
要获取服务,可以查询 BundleContext 以查找实现必需接口的服务,而且可以选择性地提供 LDAP 语法过滤器来匹配服务属性。根据所调用的方法,可以检索最佳匹配项或所有匹配项。然后,可以使用返回的 ServiceReference 来提供属性以在代码中执行进一步匹配。可以使用 ServiceReference 来获取实际的服务对象。完成使用服务时,使用 BundleContext 来释放服务。
过程
- 通过在捆绑软件中添加以下代码来声明服务接口。
package com.ibm.foo.simple; /** * Our multifunctional sample interface */ public interface Foo { }
- 指定接口的实现代码。
package com.ibm.foo.simple; /** * The implementation of the Foo interface */ public class FooImpl implements Foo { public FooImpl() { } public FooImpl(String vendor) { } /** * used by the ServiceFactory implementation. */ public void destroy() { } }
- 使用 BundleContext 直接在代码中注册服务、修改服务属性以及注销服务。
import java.util.Dictionary; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; /** * Registers and unregsiters a Foo service directly, * and shows how to modify the service properties in code. */ public class FooController { private final BundleContext bundleContext; private ServiceRegistration<Foo> sr; public FooController( BundleContext bundleContext ) { this.bundleContext = bundleContext; } public void register(Dictionary<String, Object> serviceProperties) { Foo foo = new FooImpl(); //typed service registration with one interface sr = bundleContext.registerService( Foo.class, foo, serviceProperties ); //or //untyped service registration with one interface sr = (ServiceRegistration<Foo>)bundleContext.registerService( Foo.class.getName(), foo, serviceProperties ); //or //untyped service registration with more than one interface (or class) sr = (ServiceRegistration<Foo>)bundleContext.registerService(new String[] { Foo.class.getName(), FooImpl.class.getName()}, foo, serviceProperties ); } public void modifyFoo(Dictionary<String, Object> serviceProperties) { //with the service registration you can modify the service properties at any time sr.setProperties( serviceProperties ); } public void unregisterFoo() { //when you are done unregister the service using the service registration sr.unregister(); } }
- 从另一个类获取服务和返回服务:
package com.ibm.foo.simple; import java.util.Collection; import org.osgi.framework.BundleContext; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; /** * A simple Foo client that directly obtains the Foo service and returns it when done. */ public class FooUser { private final BundleContext bundleContext; public FooUser( BundleContext bundleContext ) { this.bundleContext = bundleContext; } /** * assume there's only one Foo */ public void useFooSimple() { ServiceReference<Foo> sr = bundleContext.getServiceReference( Foo.class ); String[] propertyKeys = sr.getPropertyKeys(); for (String key: propertyKeys) { Object prop = sr.getProperty( key ); //think about whether this is the Foo we want.... } Foo foo = bundleContext.getService( sr ); try { //use foo } finally { //we're done bundleContext.ungetService( sr ); } } /** * Use a filter to select a particular Foo. Note we get a collection back and have to pick one. * @throws InvalidSyntaxException */ public void useFooFilter() throws InvalidSyntaxException { Collection<ServiceReference<Foo>> srs = bundleContext.getServiceReferences( Foo.class, "(&(service.vendor=IBM)(id='myFoo')" ); ServiceReference<Foo> sr = srs.iterator().next(); String[] propertyKeys = sr.getPropertyKeys(); for (String key: propertyKeys) { Object prop = sr.getProperty( key ); //think about whether this is the Foo we want.... } Foo foo = bundleContext.getService( sr ); try { //use foo } finally { //we're done bundleContext.ungetService( sr ); } } }
父主题: 使用 OSGi 服务注册表


http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-libcore-mp&topic=twlp_feat_service_pojo
文件名:twlp_feat_service_pojo.html