【问题标题】:EventAdmin is null in maven-osgi projectEventAdmin 在 maven-osgi 项目中为空
【发布时间】:2014-02-01 18:23:18
【问题描述】:

我创建了一个 maven-osgi 项目,其中激活器应该发送一个 osgi 事件,但由于某种原因 EventAdmin 始终为空。

这是我的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 为空的原因可能是什么?

【问题讨论】:

  • 变量ref是null还是变量eventAdmin?

标签: java maven osgi


【解决方案1】:

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

有一个错误的方法和一个正确的方法来解决这个问题。错误的方法是坚持必须在 EventAdmin 之后启动你的包。这种方式会导致启动顺序依赖性和极端脆弱性。见:http://wiki.osgi.org/wiki/Avoid_Start_Order_Dependencies

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

【讨论】:

    猜你喜欢
    • 2012-09-26
    • 2012-06-30
    • 2019-12-31
    • 1970-01-01
    • 2011-02-22
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    相关资源
    最近更新 更多