嵌入式OSGi框架中的消费服务 [英] Consuming Services from Embedded OSGi Framework

查看:68
本文介绍了嵌入式OSGi框架中的消费服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有可嵌入的Felix.我有一些API捆绑包和Impl. API导出接口C .Impl导入该接口并在激活器中注册impl.现在我要获取C隐含的OSGi

I have embeddable Felix. I have some API bundle and Impl. API exports interface C.Impl imports that interface and register impl in activator. Now I want get C impl otside OSGi

  FrameworkFactory ff = new FrameworkFactory();
  ...
  BundleContext bc = fwk.getBundleContext();
  ...
  final ServiceReference[] serviceReferences = bc.getServiceReferences(C.class.getName(), "(objectclass=" + C.class.getName() + ")");
  for(ServiceReference serviceReference : serviceReferences){
     final Object service = bc.getService(serviceReference);
     ...
  }

现在我想与它互动.我可以用反射来做

Now I want to interact with it. I can do it with reflection

     System.out.println(service.getClass().getMethod("some").invoke(service)); //using 

但我无法投射

     System.out.println(service instanceof C); //prints false

我猜这来自不同的ClassLoader.但是我该如何解决呢?我们如何从外部与OSGi上下文进行交互?还是我们可以将其全部放入OSGi容器中?

I guess that comes from different ClassLoaders. But how I can solve it? How we can interract with OSGi context from outside? Or we can obly put it all into OSGi container?

推荐答案

如果要嵌入OSGi,则该服务的API(即接口"C")必须对外部应用程序可见,并通过系统导出到OSGi捆绑出口.外部应用程序无法从OSGi框架内包含的包中导入软件包.

If you are embedding OSGi, the API for the service (i.e. interface "C") has be to visible to the outer application and exported into OSGi via the system bundle exports. The outer application cannot import packages from the bundles contained inside the OSGi framework.

这篇关于嵌入式OSGi框架中的消费服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆