【问题标题】:OSGI bundle activated, but component never created when using DS and Apache KarafOSGI 包已激活,但在使用 DS 和 Apache Karaf 时从未创建组件
【发布时间】:2017-06-27 23:25:28
【问题描述】:

我正在开发一个 Hello World 应用程序。我的应用程序是用 Maven (3.5.0) 构建的,使用 Apache Felix 注释,并在 Apache Karaf (4.1.1) 中运行。我的应用程序由一个名为 App 的组件组成,应该立即启动。捆绑包构建成功。我可以从我的 mvn 存储库成功地将它安装到 Karaf 中。 Karaf 将捆绑包显示为“活动”。问题是我的组件(App)的构造函数和激活方法从未被调用。我需要第二双眼睛来帮助我弄清楚为什么会发生这种情况。我的 pom.xml 中缺少什么?

为了完整起见,我在我的项目中创建了一个实现 BundleActivator 的 Activator 类。然后我指示 Maven 将我的 Bundle-Activator 设置为这个新类。现在,当我在 Karaf 中安装我的包时,我可以看到我的 Activator 的日志输出。 start 方法被击中。所以我知道我的捆绑包实际上已经开始了。我只是不明白为什么我的 App 组件从未创建和激活。

这里是相关文件。

App.java

package myCompany;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;

@Component(immediate=true)
public class App 
{
    public App()
    {
        System.out.println( "App constructed" );
    }

    @Activate
    public void activate()
    {
        System.out.println( "App activated" );
    }
}

Activator.java

package myCompany;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator  implements BundleActivator{

    public void start(BundleContext context) throws Exception {
        System.out.println("Activator started");
    }

    public void stop(BundleContext context) throws Exception {
        System.out.println("Activator stopped");
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>myCompany</groupId>
    <artifactId>myProject</artifactId>
    <packaging>bundle</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>myProject</name>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>6.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.scr.annotations</artifactId>
            <version>1.9.6</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-scr-plugin</artifactId>
                <version>1.24.0</version>
                <executions>
                    <execution>
                        <id>generate-scr-scrdescriptor</id>
                        <goals>
                            <goal>scr</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <version>3.3.0</version>
                <configuration>
                    <instructions>
                        <_dsannotations>*</_dsannotations>
                        <_metatypeannotations>*</_metatypeannotations>
                        <Bundle-Activator>myCompany.Activator</Bundle-Activator>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

【问题讨论】:

    标签: java maven osgi apache-karaf


    【解决方案1】:

    Karaf 不支持开箱即用的声明式服务。您需要安装 scr 功能才能激活 DS 支持:

    feature:install scr
    

    【讨论】:

    • 那行得通。我的包已经加载到 Karaf 中(但没有做任何事情),我运行了 feature:install scr,瞧,我终于看到我的“应用程序构建”和“应用程序激活”消息弹出。谢谢。
    【解决方案2】:

    您正在使用来自 org.apache.felix.scr.annotations 包的过时注释,bnd 无法识别或处理它们 - 它们需要额外的 Maven 插件才能工作。

    最好迁移您的代码以使用来自org.osgi.service.component.annotations 包的OSGi 标准注释。请参阅 OSGi 纲要第 6 版规范,第 112.8 节。

    【讨论】:

    • Christian Schneider 在下面提供了我的即时解决方案。实际上,我确实从标准的 OSGI 注释开始,但无法正确生成我的包,所以我切换到我所熟知的 Felix SCR 注释。现在我已经有了 Felix SCR 注释,我将重新审视标准的 OSGI 注释。
    猜你喜欢
    • 2015-05-22
    • 2019-08-31
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多