我对OSGI框架是完全陌生的,并且尝试通过以下两个捆绑包进行测试:
捆绑软件1:我使用工件组org.apache.karaf.archetypes通过maven创建了OSGI捆绑软件。在bundle 1激活器类中,我正在注册以下服务:
public void start(BundleContext context) {
System.out.println("-------------- Starting firstbundle ----------------");
context.registerService(EmployeeService.class, new EmployeeServiceImpl(), null);
}
软件包1的MANIFEST.MF文件的内容:
Manifest-Version: 1.0
Bnd-LastModified: 1594906982858
Build-Jdk: 1.8.0_251
Built-By: 212807091
Bundle-Activator: com.osgi.learn.firstbundle.Activator
Bundle-Description: firstbundle OSGi bundle project.
Bundle-ManifestVersion: 2
Bundle-Name: firstbundle Bundle
Bundle-SymbolicName: firstbundle
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: com.osgi.learn.firstbundle;version="0.0.1.SNAPSHOT";uses
:="org.osgi.framework"
Import-Package: org.osgi.framework;version="[1.7,2)"
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Tool: Bnd-2.3.0.201405100607
捆绑软件2:我以创建捆绑软件1的相同方式创建了另一个独立的捆绑软件。捆绑软件2激活器类如下:
public void start(BundleContext context) {
ServiceReference<?> ref = context.getServiceReference(EmployeeService.class);
EmployeeService empService = (EmployeeService) context.getService(ref);
boolean isRegister = empService.register("rohit", "rohit");
if(isRegister){
System.out.println("employee registered successfully");
} else{
System.out.println("employe is already registered");
}
System.out.println("after register employee list: "+empService.getEmployeeList().size());
}
软件包2的MANIFEST.MF文件的内容:
Manifest-Version: 1.0
Bnd-LastModified: 1594907276585
Build-Jdk: 1.8.0_251
Built-By: 212807091
Bundle-Activator: com.osgi.learn.secondbundle.Activator
Bundle-Description: secondbundle OSGi bundle project.
Bundle-ManifestVersion: 2
Bundle-Name: secondbundle Bundle
Bundle-SymbolicName: secondbundle
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: com.osgi.learn.secondbundle;version="0.0.1.SNAPSHOT";use
s:="org.osgi.framework"
Import-Package: com.osgi.learn.service,org.osgi.framework;version="[1.7,
2)"
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Tool: Bnd-2.3.0.201405100607
现在我已经在OSGI环境中安装了两个捆绑软件,并且当我启动两个捆绑软件时,捆绑软件1已正确启动,而捆绑软件2则抛出如下错误:
osgi> ss
"Framework is launched."
id State Bundle
0 ACTIVE org.eclipse.osgi_3.11.3.v20170209-1843
1 ACTIVE org.apache.felix.gogo.runtime_0.10.0.v201209301036
2 ACTIVE org.eclipse.equinox.console_1.1.200.v20150929-1405
3 ACTIVE org.apache.felix.gogo.shell_0.10.0.v201212101605
4 ACTIVE org.apache.felix.gogo.command_0.10.0.v201209301215
5 INSTALLED firstbundle_0.0.1.SNAPSHOT
6 INSTALLED secondbundle_0.0.1.SNAPSHOT
osgi> start 5
-------------- Starting firstbundle ----------------
osgi> start 6
gogo: BundleException: Could not resolve module: secondbundle [6]
Unresolved requirement: Import-Package: com.osgi.learn.service
我有以下疑问:
您能否让我知道为什么bundle 2无法部署以及如何解决它。
每个捆绑程序Activator类对象是否获得唯一的BundleContext对象。如果是这样,那么一旦捆绑包将类对象注册到上下文对象,如何使用上下文对象将其提供给另一个捆绑包。
OSGI环境的所有注册对象是否都存储在一个地方,可以由使用上下文对象部署的任何捆绑软件检索到,或者我必须使用其他逻辑。
您的设置似乎有点困惑?您使用的是bnd的较旧版本,并且生成的清单似乎与预期不符。bnd是从bnd文件或pom清单部分中提取的,通常最好显示这些输入。
发生错误是因为您没有正确导出/导入软件包。
Import-Package Export-Package
firstbundle org.osgi.framework com.osgi.learn.firstbundle
secondbundle com.osgi.learn.service com.osgi.learn.secondbundle
org.osgi.framework
当启动第二个捆绑软件(6)时,您将得到以下错误:该软件包com.osgi.learn.service
未由与清单信息匹配的任何捆绑软件导出。
因此,您需要有人来导出com.osgi.learn.service
软件包。
每个捆绑包都有其自己唯一的捆绑包上下文对象。该对象有一个getBundle()
返回其唯一Bundle对象的方法。但是,服务注册表由所有捆绑软件共享。由于OSGi知道注册或获取服务的捆绑包,因此通过特定于Bundle的BundleContext注册和获取服务,OSGi可以提供许多不错的功能,例如清理,安全性等等。
您可能想通过遵循Bndtools OSGi入门指南来获得一些OSGi经验,此外还有很多视频可以展示不同的阶段。这是基于Bndtools的,但是一旦您有了经验,就不难回到Maven。如果您仍然想要,那就是:-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句