OSGI服务注册、引用、以及跟踪
2015-12-29 17:52
246 查看
下载地址<a target=_blank href="http://download.csdn.net/detail/qq_28051649/9382494">http://download.csdn.net/detail/qq_28051649/9382494</a>
1、新建一个bundle名为BundleBase
package example.service; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; public class Activator implements BundleActivator { private static BundleContext context; static BundleContext getContext() { return context; } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext bundleContext) throws Exception { Activator.context = null; } } package example.service; public interface IHelloService { String sayHello(String somebody); }
2、新建一个名为BundleEnservice
<p>package bundleenservice;</p><p>import java.util.Dictionary; import java.util.Hashtable;</p><p>import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext;</p><p>import example.service.IHelloService;</p><p>public class Activator implements BundleActivator {</p><p> private static BundleContext context;</p><p> static BundleContext getContext() { return context; }</p><p> /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; IHelloService hs = new EnHelloworld(); Dictionary<String, Object> properties = new Hashtable<String, Object>(); properties.put("service.language", "CN"); context.registerService(IHelloService.class.getName(), hs, properties); }</p><p> /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext bundleContext) throws Exception { Activator.context = null; }</p><p>}</p><p>package bundleenservice;</p><p>import example.service.IHelloService;</p><p>public class EnHelloworld implements IHelloService{</p><p> @Override public String sayHello(String somebody) { // TODO Auto-generated method stub return "how are you ?" + somebody; }</p><p>} </p><p> </p><p> </p><p> </p>
3、新建一个名为BundleCnService
package bundlecnservice; import java.util.Dictionary; import java.util.Hashtable; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import example.service.IHelloService; public class Activator implements BundleActivator { private static BundleContext context; static BundleContext getContext() { return context; } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; //通过服务工厂进行服务的获取 CnGreetingsServiceFactory cns = new CnGreetingsServiceFactory(); // IHelloService hs = new CnHelloworld(); Dictionary<String, Object> properties = new Hashtable<String, Object>(); // properties.put(Constants.SERVICE_RANKING,1 ); properties.put("service.language", "CN"); //服务的注册 context.registerService(IHelloService.class.getName(), cns, properties); } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext bundleContext) throws Exception { Activator.context = null; } }
<p>package bundlecnservice;</p><p>import org.osgi.framework.Bundle; import org.osgi.framework.ServiceFactory; import org.osgi.framework.ServiceRegistration;</p><p>public class CnGreetingsServiceFactory implements ServiceFactory{</p><p> @Override public CnHelloworld getService(Bundle arg0, ServiceRegistration arg1) { // TODO Auto-generated method stub System.out.println(arg0.getSymbolicName() + "获取了服务"); return new CnHelloworld(); }</p><p> @Override public void ungetService(Bundle arg0, ServiceRegistration arg1, Object arg2) { // TODO Auto-generated method stub System.out.println(arg0.getSymbolicName() + "释放了服务"); }</p><p>} </p>
package bundlecnservice;
import example.service.IHelloService;
public class CnHelloworld implements IHelloService{
@Override public String sayHello(String somebody) { // TODO Auto-generated method stub return "你好," + somebody; }
}
4、新建一个bundleserviceUser
package bundleserviceuser;
import java.util.Collection;
import java.util.Iterator;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import example.service.IHelloService;
public class Activator implements BundleActivator {
private static BundleContext context;
static BundleContext getContext() {
return context;
} /*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
ServiceReference reference = context.getServiceReference(IHelloService.class.getName());
String filter = "(service.language=CN)";
ServiceReference[] references = context.getServiceReferences(IHelloService.class.getName(), filter);
int iter = references.length;
for(int i = 0; i < iter; i++){
IHelloService service = (IHelloService) context.getService(references[i]);
System.out.println(service.sayHello("zhangfy"));
}
} /*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}}
相关文章推荐
- iOS 7 代码布局适配
- 配置tomcat数据库连接池及使用数据库连接池(hibernate数据源配置和使用)
- echo 的部分用法
- hibernate中获得session的方式
- 新一代的USB 3.0传输规格
- Unity-Animator深入系列---deltaPosition&deltaRotation
- Visual Studio 2010下编译osip及eXosip并搭建开发环境的方法
- grep
- 数据库连接池概念、种类、配置(三)(Spring\Hibernate\Tomcat配置数据库连接)
- svn使用学习
- 正则
- cocos2dx原生lua绑定工具的总结
- 出现java.lang.NoClassDefFoundError: com/google/common/base/Charsets异常错误
- sql存储过程等-版本控制
- WIndow Document
- 字符集和字符编码(Charset & Encoding)
- PDO execute 执行错误
- js私有方法、公有方法、特权方法总结
- Cobbler全自动批量安装部署Linux系统
- awk--awk