当前位置: 首页 > 面试题库 >

在maven-osgi项目中,EventAdmin为null

淳于知
2023-03-14
问题内容

我做了一个maven-osgi项目,其中激活程序应该发送一个osgi事件,但是由于某种原因EventAdmin始终为null。

这是我的java课

package com.example.eventhandler;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

public class App implements BundleActivator {
    public void start(BundleContext context) throws Exception {
        ServiceReference ref = context.getServiceReference(EventAdmin.class.getName());
        if (ref != null) {
            EventAdmin eventAdmin = (EventAdmin) context.getService(ref);
            Dictionary properties = new Hashtable();

            eventAdmin.sendEvent(new Event("com/acme/reportgenerator/GENERATED", properties));
        } else {
            System.out.println("Ref is null!!");
        }
        System.out.println("Hello World!!");
    }

    public void stop(BundleContext context) throws Exception {
        System.out.println("Goodbye World!!");
    }
}

这是我的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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>eventhandler</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eventhandler</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <bundle.symbolicName>com.example</bundle.symbolicName>
        <bundle.namespace>com.example</bundle.namespace>
    </properties>

    <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>4.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.osgi</groupId>
            <artifactId>org.eclipse.osgi</artifactId>
            <version>3.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.osgi.compendium</artifactId>
            <version>1.4.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <id>bundle-manifest</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>manifest</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <manifestLocation>META-INF</manifestLocation>
                    <instructions>
                        <Bundle-SymbolicName>${bundle.symbolicName}</Bundle-SymbolicName>
                        <Bundle-Version>${pom.version}</Bundle-Version>
                        <Bundle-Activator>${bundle.namespace}.eventhandler.App</Bundle-Activator>
                        <Import-Package>
                            org.osgi.framework,
                            org.osgi.service.event
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

EventAdmin为空的原因可能是什么?


问题答案:

您已经在OSGi中实现了经典的反模式:您假设在捆绑包启动时EventAdmin服务将已经可用。这本质上是不安全的假设,因为您的捆绑包实际上可能在提供EventAdmin服务的捆绑包之前启动。

有一个错误的方法和正确的方法来解决此问题。错误的方法是坚持认为必须在EventAdmin之后启动捆绑软件。这样会导致开始顺序依赖性和极端的脆弱性。请参阅:http
:
//wiki.osgi.org/wiki/Avoid_Start_Order_Dependencies

正确的方法是使用诸如声明式服务(DS)之类的框架在捆绑包中声明一个引用EventAdmin服务的组件。然后,DS将激活您的组件,并在EventAdmin实例可用时将其注入。请参阅:http://wiki.osgi.org/wiki/Declarative_Services



 类似资料:
  • 我们面临的最大问题是,我们无法找出如何构造项目C,以使其在项目A和B中都能正常工作。在项目A中,我们希望maven能像往常一样工作,并根据需要降低依赖项。但我们也希望在项目B(也许结合Nexus+Tycho??)中实现该功能。 我们应该如何解决这个问题。我还没有找到一个好的解决办法如何做这件事。我已经在maven中尝试了apache felix捆绑插件,但不能让它像我想要的那样工作。这是正确的解决

  • 我是OSGi的新手,很难将包包含到我的Maven项目中。 我使用mave-bundle-plugin创建了一个API包和一个实现包。在我的主要项目(一个Maven项目)中,我试图使用Felix框架从ServiceTracker获得已实现包的服务。当我最终尝试将获得的服务强制转换为正确的类型时,我会收到一个ClassCastException。 Maven使用以下清单文件创建了两个jar文件: 在我

  • 我有一个使用Gradle作为构建工具的项目和第二个使用Maven的POM的子项目。我没有在子项目上更改构建工具的自由。 我想要实现的是将我的项目添加到Maven POM中,作为我的Gradle项目的依赖项。 其中root(当前目录)是我的Gradle项目,并包含,Maven项目位于下,POM文件就在该目录下。 我在我的< code>build.gradle文件中尝试了这些变化: 第一次尝试: 第二

  • 我试图使用Eclipse的从maven开始。但如果我尝试将现有项目更改为Maven项目(),结果是: null 问题可能出在哪里? 更新(生成的pom.xml):

  • 我正在创建一个新的Karaf特性,它将包含几个包(jclouds)。我还有一个maven“superproject”(jclouds-all),它包含我需要的所有模块/包。 在定义特性时,是否可以只指定一个SuperProject的bundle,并自动解析所有具体的bundle,或者我需要在特性中列出所有的bundle?