声明性OSGi服务 [英] Declarative OSGi Services

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

问题描述

我对声明性OSGi服务有疑问.我有以下界面:

  public interface PrintService {
    public void print();
  }

及其实现:

    public class PrintServiceImpl implements PrintService {

      @Override
      public void print() {
        System.out.println("Hello from PrintServiceImpl!");
      }
    }

OSGI-INF/component.xml:

<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="service">
   <implementation class="service.PrintServiceImpl"/>
   <service>
      <provide interface="print.PrintService"/>
   </service>
</scr:component>

MANIFEST.MF:

Service-Component: OSGI-INF/component.xml

安装服务并启动后,什么都没有发生.如何激活它并打印"来自PrintServiceImpl的Hello!"到控制台.

解决方案

为什么期望调用print方法?它是服务接口的一部分,因此只有在绑定到客户端并调用它的客户端之前,它才会被调用.

如果在控制台中键入services命令,您应该看到捆绑软件正在发布print.PrintService服务.这意味着您的组件正在工作.如果您没有看到此内容,则可能是汤姆·塞德尔(Tom Seidel)在上面的评论中建议的SCR捆绑件之类的东西.

I have a question about declarative OSGi Services. I have the following interface :

  public interface PrintService {
    public void print();
  }

and its implementation:

    public class PrintServiceImpl implements PrintService {

      @Override
      public void print() {
        System.out.println("Hello from PrintServiceImpl!");
      }
    }

OSGI-INF/component.xml :

<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="service">
   <implementation class="service.PrintServiceImpl"/>
   <service>
      <provide interface="print.PrintService"/>
   </service>
</scr:component>

MANIFEST.MF :

Service-Component: OSGI-INF/component.xml

After I install the service and start it nothing happend. How can I activate it and print "Hello from PrintServiceImpl!" to console.

解决方案

Why do you expect your print method to be invoked? It is part of the interface of the service, so it will not be invoked until you can a client that binds to it and calls it.

If you type the services command in the console you should see that your bundle is publishing the print.PrintService service; this means that your component is working. If you don't see this then you may be missing something like the SCR bundle as suggested by Tom Seidel in the comments above.

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

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