Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OSGI框架中部署的每个bundle是否都有';s自己的文本对象?_Java_Osgi_Maven 2_Osgi Bundle - Fatal编程技术网

Java OSGI框架中部署的每个bundle是否都有';s自己的文本对象?

Java OSGI框架中部署的每个bundle是否都有';s自己的文本对象?,java,osgi,maven-2,osgi-bundle,Java,Osgi,Maven 2,Osgi Bundle,我对OSGI框架完全陌生,我正尝试用两个捆绑包测试以下场景: 捆绑包1: 我已经通过maven使用artifact grouporg.apache.karaf.archetypes创建了一个OSGI包。 在bundle 1 activator类中,我注册的服务如下: public void start(BundleContext context) { System.out.println("-------------- Starting firstbundle ---

我对OSGI框架完全陌生,我正尝试用两个捆绑包测试以下场景:

捆绑包1: 我已经通过maven使用artifact grouporg.apache.karaf.archetypes创建了一个OSGI包。 在bundle 1 activator类中,我注册的服务如下:

 public void start(BundleContext context) {
        System.out.println("-------------- Starting firstbundle ----------------");
        context.registerService(EmployeeService.class, new EmployeeServiceImpl(), null);
    }
 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());
    
}
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 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
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
捆绑包2: 我以创建bundle 1的相同方式创建了另一个独立bundle。 捆绑包2激活器类别如下:

 public void start(BundleContext context) {
        System.out.println("-------------- Starting firstbundle ----------------");
        context.registerService(EmployeeService.class, new EmployeeServiceImpl(), null);
    }
 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());
    
}
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
现在我已经在OSGI环境中安装了这两个捆绑包,当我启动这两个捆绑包时,捆绑包1正确启动,而捆绑包2抛出错误,如下所示:

 public void start(BundleContext context) {
        System.out.println("-------------- Starting firstbundle ----------------");
        context.registerService(EmployeeService.class, new EmployeeServiceImpl(), null);
    }
 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());
    
}
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
我有以下疑问:

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
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
  • 你能告诉我为什么bundle 2没有得到部署以及我如何解决它吗

  • 每个bundle Activator类对象是否获得唯一的BundleContext对象。如果是这样的话,那么一旦bundle向context对象注册了类对象,那么使用context对象的另一个bundle将如何使用该类对象

  • 是否所有向OSGI环境注册的对象都存储在一个位置,使用上下文对象部署的任何包都可以检索到该位置,或者我必须使用其他逻辑


  • 您的设置似乎有点混乱?您使用的是古老版本的bnd,生成的清单似乎与预期不符。bnd从bnd文件或pom清单部分工作,通常最好显示这些输入

    1未解决 发生此错误的原因是您未正确导出/导入包

                     Import-Package            Export-Package
    
    firstbundle      org.osgi.framework        com.osgi.learn.firstbundle
    secondbundle     com.osgi.learn.service    com.osgi.learn.secondbundle
                     org.osgi.framework
    
    当您启动第二个bundle(6)时,您会得到一个错误,即包
    com.osgi.learn.service
    未由任何与清单信息匹配的bundle导出

    因此,您需要有人导出
    com.osgi.learn.service

    2/3捆绑上下文和服务注册表 每个bundle都有自己独特的bundle上下文对象。此对象有一个
    getBundle()
    方法返回其唯一的Bundle对象。但是,服务注册表由所有捆绑包共享。通过一个特定于Bundle的Bundle注册和获取服务,OSGi可以做很多很好的功能,比如清理、安全等,因为OSGi知道注册或获取服务的Bundle


    您可能希望通过遵循Bndtools获得一些OSGi体验,这里还提供了一些演示不同阶段的工具。这是基于Bndtools的,但一旦你获得了经验,就不难回到maven。如果您仍然需要,即:-)

    您的设置似乎有点混乱?您使用的是古老版本的bnd,生成的清单似乎与预期不符。bnd从bnd文件或pom清单部分工作,通常最好显示这些输入

    1未解决 发生此错误的原因是您未正确导出/导入包

                     Import-Package            Export-Package
    
    firstbundle      org.osgi.framework        com.osgi.learn.firstbundle
    secondbundle     com.osgi.learn.service    com.osgi.learn.secondbundle
                     org.osgi.framework
    
    当您启动第二个bundle(6)时,您会得到一个错误,即包
    com.osgi.learn.service
    未由任何与清单信息匹配的bundle导出

    因此,您需要有人导出
    com.osgi.learn.service

    2/3捆绑上下文和服务注册表 每个bundle都有自己独特的bundle上下文对象。此对象有一个
    getBundle()
    方法返回其唯一的Bundle对象。但是,服务注册表由所有捆绑包共享。通过一个特定于Bundle的Bundle注册和获取服务,OSGi可以做很多很好的功能,比如清理、安全等,因为OSGi知道注册或获取服务的Bundle

    您可能希望通过遵循Bndtools获得一些OSGi体验,这里还提供了一些演示不同阶段的工具。这是基于Bndtools的,但一旦你获得了经验,就不难回到maven。如果你还想,那就是:-)