简体   繁体   English

OSGi Bundle不是以Felix Host应用程序启动

[英]OSGi Bundle not starting with Felix Host application

I am using Apache Felix in a host application to provide the ability to load extensions at runtime. 我在主机应用程序中使用Apache Felix以提供在运行时加载扩展的功能。 The mechanism works great but I have some very temperamental behaviour regarding the bundles starting if I include certain dependencies. 该机制很好用,但是如果我包括某些依赖项,我会发现一些关于捆绑包的气质行为。 If for example I use the following in my pom.xml: 例如,如果我在pom.xml中使用以下内容:

<packaging>bundle</packaging>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>2.5.0</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
                    <Bundle-Name>${project.artifactId}</Bundle-Name>
                    <Bundle-Version>1.0.0</Bundle-Version>
                    <Bundle-Activator>${pom.groupId}.activator.Activator</Bundle-Activator>
                    <Include-Resource>{maven-resources}, {maven-dependencies}</Include-Resource>
                    <Import-Package>*</Import-Package>
                    <Embed-Dependency>jackson-core</Embed-Dependency>
                    <Embed-Transitive>true</Embed-Transitive>
                </instructions>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.osgi.core</artifactId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>1.8.2</version>
    </dependency>
    <dependency>
        <groupId>co.ff36</groupId>
        <artifactId>halo.core</artifactId>
        <version>1.0.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

Everything works perfectly and the bundle registers and starts. 一切正常,捆绑包注册并开始。 However if I include async-http-client in the bundle it registers but does not start! 但是,如果我在捆绑包中包含async-http-client ,它将注册但无法启动! I have tried embedding the dependency in the bundle even though the parent exposes it by the parent host application. 我已经尝试将依赖项嵌入到包中,即使父项由父级宿主应用程序公开了。 If I look inside the compiled bundle the jar has been included but it still wont actually start. 如果我查看已编译的捆绑包,则该jar已包含在内,但实际上仍无法启动。

I tried adding: 我尝试添加:

    <dependency>
        <groupId>com.ning</groupId>
        <artifactId>async-http-client</artifactId>
        <version>1.9.31</version>
    </dependency>

and modified: 并修改:

<Embed-Dependency>jackson-core, async-http-client</Embed-Dependency>

Neither of these options work. 这些选项都不起作用。 I am not getting any error in the host application and I just can't work out why some libraries cause this but not others. 我没有在主机应用程序中遇到任何错误,而且我无法弄清为什么某些库导致了这种情况,而其他原因则没有。

The Jackson can be accessed as OSGi bundles you don't need to embed it. 您可以将Jackson嵌入为OSGi捆绑软件,而无需将其嵌入。 If you have felix webconsole or other type of console you can check what import (or capability) is not presented which causes that the bundle cannot came to ACTIVE state. 如果您有felix webconsole或其他类型的控制台,则可以检查未显示什么导入(或功能),这导致捆绑软件无法进入ACTIVE状态。 As the POM shows all dependecies included in transitive form. 正如POM所示,所有依存关系都包含在传递形式中。 It's not a good idea to use that, because it means no classes loaded from other bundles, instead of everything is embeded, which means you create a monolithic bundle which is not using anything from bundles. 使用它不是一个好主意,因为这意味着没有从其他包中加载任何类,而不是嵌入所有内容,这意味着您创建了一个不使用任何包的整体包。

Another possible reason to not start is that the activator calls some method which throws an exception which will invalidate your activator, as the OSGi spec defines. 无法启动的另一个可能原因是,激活程序调用某些方法,该方法会引发异常,这将使您的激活程序无效,如OSGi规范所定义。 Recommended to check your log, maybe there is some reflection instantiate classes in the code which cannot be resolved, because its not manifested by bundle plugin - only that packages can be imported which are presented in classes import's. 建议检查您的日志,也许在代码中有一些反射实例化类无法解析,因为它没有通过捆绑插件来体现-只有可以导入的包才在类import的目录中显示。

After further investigation it turns out that the problem is related to versioning. 经过进一步调查,结果表明该问题与版本控制有关。 The bundle MANIFEST.MF that is created explicitly states the versions for some import packages: 创建的捆绑软件MANIFEST.MF明确指出了某些导入软件包的版本:

Import-Package: ...,com.fasterxml.jackson.core.type;version="[2.4,3)",com.ning.http.client;version="[1.9,2)",
 org.osgi.framework;version="[1.5,2)"

However, the host application does not specify a version: 但是,主机应用程序未指定版本:

Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA -> ... com.ning.http.client ...

It appears that the version must be explicitly stated in the host and it must match the bundles import otherwise the bundle won't activate. 似乎必须在主机中明确声明该版本,并且该版本必须与导入的捆绑软件匹配,否则捆绑软件将不会激活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM