Android App使用OSGi与服务器通信

时间:2013-10-16 23:05:02

标签: android tomcat osgi apache-felix

我是OSGi的新手。我的要求是让一个Android应用程序与OSGi框架上的服务器通信。

我将Apache Felix视为容器。根据我的理解,Felix上将安装捆绑包,它们可以从程序本身启动和停止。

我设法在android端创建捆绑并启动它。 但是我无法在服务器端清楚地了解OSGi。 Android应用程序使用Http请求和响应与服务器通信。 如果我在服务器上单独使用Apache Felix,我是否能够处理Http请求? 我也需要Tomcat吗?

我看到了这个链接 -

Trouble understanding the whole OSGi web eco system

他们在这里谈论Web容器是如何使用Tomcat / Jetty的。这告诉我在服务器上需要Tomcat和Felix。我对吗? 我还读到有关Jetty嵌入Felix的内容。但我无法连接点。

请引导我朝正确的方向前进。

1 个答案:

答案 0 :(得分:0)

OSGi的首选模型是在服务注册表中注册Servlet服务。该servlet应该由该框架上安装的Http服务器获取。该模型将在不久的将来成为标准模型,但Apache Felix已经支持该模型。这就是你在bnd(工具)中做到这一点的方法:

bnd.bnd
  -runfw: org.apache.felix.framework;version='[4,5)'
  -runbundles: \
    org.apache.felix.configadmin;           version=1.6.0, \
    org.apache.felix.log;                   version=1.0.1, \
    org.apache.felix.scr;                   version=1.6.0, \
    org.apache.felix.http.jetty;            version=2.2.0, \
    org.apache.felix.http.whiteboard;       version=2.2.0

如果正在运行,您可以使用Declarative Services编写一个servlet,如下所示:

  @Component(provide=Servlet.class,properties="alias=/hello") // makes it available on /hello 
  public class MyAndroidServer extends HttpServlet {
    public void doGet(HttpServletRequest rq, HttpSerletResponse rsp) throws IOException {
      rsp.getWriter().println("Hello World");
    }
  }

如果你从bndtools开始,应该很容易让它工作。据我所知,这是在OSGi环境中使用servlet的最简单方法。