EventAdmin is null in maven-osgi project

独自空忆成欢 提交于 2019-12-02 17:53:25

问题


I made an maven-osgi project where an activator should send an osgi event but for some reason EventAdmin is always null.

Here is my java class

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!!");
    }
}

And here is my 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>

What could be the reason EventAdmin is null?


回答1:


You have implemented a classic anti-pattern in OSGi: you assume that the EventAdmin service will already be available when your bundle starts. This is an inherently unsafe assumption because your bundle might in fact be started before the bundle that provides the EventAdmin service.

There is a wrong way and a right way to fix this. The wrong way is to insist that your bundle must be started after EventAdmin. That way leads to start-order dependencies and extreme fragility. See: http://wiki.osgi.org/wiki/Avoid_Start_Order_Dependencies

The right way is to use a framework such as Declarative Services (DS) to declare a component inside your bundle that references the EventAdmin service. Then DS will activate your component and inject it with the EventAdmin instance as soon as it becomes available. See: http://wiki.osgi.org/wiki/Declarative_Services



来源:https://stackoverflow.com/questions/21100900/eventadmin-is-null-in-maven-osgi-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!