如何从OSGi HTTP服务使用OSGi服务 [英] How to consume OSGi service from OSGi HTTP Service

查看:87
本文介绍了如何从OSGi HTTP服务使用OSGi服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个捆绑包A,它提供以下服务:

I have a bundle A which exposes the following service:

在OSGI-INF/config.xml

In OSGI-INF/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" 
    name="com.example.MyService" modified="updated" immediate="true">
 <implementation class="com.example.impl.MyServiceImpl"/>
 <service>
  <provide interface="com.example.MyService"/>
 </service>
</scr:component>

下一步,我想从捆绑软件B中的servlet使用此服务.

Next step, I want to consume this service from a servlet in bundle B.

我要做的是以下事情:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
 BundleContext bundleContext = (BundleContext) getServletContext().getAttribute("osgi-bundlecontext");
    if (bundleContext != null) {
     // Here MyService is the service exposed as declarative service
     MyService myService = getService(bundleContext, MyService.class);
     if(myService != null) {
      // I want to invoke some method declared in MyService interface
      myService.invokeMyServiceMethod();
     }
    }
}// end of doPost

protected <T> T getService(BundleContext bundleContext, Class<T> type) {
    ServiceReference<T> serviceRef = bundleContext.getServiceReference(type);
    if (serviceRef == null) {
        return null;
    }
    T service = bundleContext.getService(serviceRef);
    return service;
}// end of getService method

随着OSGi中服务的不断出现,假设即使通过doPost方法中的非null引用检查通过,下一条语句 myService.invokeMyServiceMethod()不会抛出NPE是否正确?

As services in OSGi come and go, is it correct to assume that even if the check for non null reference in doPost method passes, the next statement myService.invokeMyServiceMethod() will not throw NPE?

如何保证我将始终从服务注册表中获得对MyService的有效引用?

How can I guarantee that I will always get a valid reference to MyService from service registry?

如果这不是从Http Service获取服务引用的正确方法,那么正确的方法是什么?

If this is not the correct way of getting service reference from Http Service, what is the correct one?

我正在使用Equinox作为OSGi实现.

I am using Equinox as OSGi implementation.

干杯, 鲍里斯

推荐答案

我认为您已经错过了一些声明式服务(DS):-) DS的整个思想是在XML中指定依赖项(或使用注释要好得多). servlet应该是这样的:

I think you've missed a few bits of declarative services (DS) :-) The whole idea of DS that you specify your dependencies in the XML (or MUCH MUCH better with the annotations). This is how the servlet should look like:

@Component(provide=Servlet.class)
public class MyServlet extends HttpServlet {
  T myService;

  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
    myService.invokeMyServiceMethod();
  }

  @Reference
  T setT(T t) {
    myService =t;
  }
}

唯一需要做的就是确保安装了Apache Felix的Http Whiteboard捆绑包(是的,它在Equinox(标准之美)上可以正常工作).该捆绑软件监视所有正在注册的Servlet服务,并将它们添加到Http Service中.由于DS确保在拥有myService之前不会注册您的组件,因此保证myService为非null.这称为DS静态模式:在调用之前,您的所有依赖项都已满足.

The only thing required is to ensure you have Apache Felix's Http Whiteboard bundle installed (yes, it works fine on Equinox, the beauty of standards). This bundle watches any Servlet services being registered and adds them to the Http Service. Since DS ensures that your component is not registered until it has the myService, you myService is guaranteed to be non-null. This is called the DS static mode: all your dependencies are satisfied before you get called.

如果您很勇敢,则可以将setT方法声明为动态方法.这样,即使没有T服务,您的组件也将被注册.例如.允许您告诉呼叫者没有服务.这称为动态模式.

If you're brave, you could declare the setT method to be dynamic. Then your component would be registered even if there is no T service. E.g. allows you to tell the caller there is no service. This is called the dynamic mode.

使用的注释是标准DS.它们由bnd处理并转换为XML.这可以在maven,gradle等环境中使用,但是最好在所有bndtools中使用.

The used annotations are standard DS. They are processed by bnd and turned into the XML. This works in maven, gradle, etc. but best of all of course in bndtools.

这篇关于如何从OSGi HTTP服务使用OSGi服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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