문제 설명
임베디드 osgi 프레임워크, 서비스 기능을 호출하는 방법은 무엇입니까? (Embedded osgi framework, how to call service functions?)
서버라는 간단한 자바 프로젝트가 있습니다. 또한 인터페이스 BlockProvider를 정의하는 osgi API 프로젝트가 있습니다. 다음으로 BlockProvider를 구현하는 서비스가 포함된 osgi 선언적 서비스 프로젝트가 있습니다.
내 자바 프로젝트 '서버'에는 임베디드 osgi 프레임워크(내 경우에는 felix)가 있습니다.
저는 gogo osgi 번들, scr 번들, 내 API 프로젝트 번들 및 서비스 프로젝트 번들을 설치하는 bundleactivator를 만들었습니다.
문제 없이 작동하고 내 서비스 번들을 검사할 수 있습니다.
내 서버에서 프로젝트 BlockProvider 인터페이스를 구현하는 서비스를 보고 싶습니다. 그래서 두 번째 매개변수(필터 1)로 "BlockProvider.class.getName()"을 사용하여 ServiceTracker를 구현합니다.
또한 문제 없이 작동합니다.
참조 솔루션
방법 1:
You can only access services from outside the OSGi framework if the API package is provided by the system bundle / the framework. So you should not install the API bundle and instead use the framework property org.osgi.framework.system.packages.extra to export your API package.
방법 2:
There are two important rules that apply here.
When two bundles communicate via services, they MUST both import the API package from the same bundle. Normally you have a choice of which bundle actually exports the package. The provider of the service could export it, in which case the consumer must import it from the provider. Alternatively, you can have a separate "pure API" bundle exporting the API package, and both the provider and consumer import it from there. (Rarely, the consumer bundle exports the package, which is legal in OSGi but kind of weird design.)
The system bundle — that is, the special bundle that represents the OSGi Framework itself from inside OSGi — cannot import packages from any ordinary bundle. It can only export.
When you embed OSGi, your application code outside of OSGi is seen as part of the system bundle. If you want the system bundle to talk to another bundle via services, then combining the two rules above implies that the API package must be exported by the system bundle. This remains true whether the system bundle is the provider or the consumer of the service.
Hence, the API package must be included on the system classpath of the embedding application, and exported from the system bundle when you setup the OSGi Framework using the org.osgi.framework.system.packages.extra
property.
(by Tinus Tate、Christian Schneider、Neil Bartlett)