声明性OSGi服务 [英] Declarative OSGi Services
问题描述
我对声明性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屋!