2014-01-13 71 views
0

我做了一個maven-osgi項目,激活者應發送一個osgi事件,但由於某種原因,EventAdmin始終爲空。maven-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的原因是空?

+0

是變量ref null還是變量eventAdmin? –

回答

2

您已經在OSGi中實現了一個經典的反模式:假設您的軟件包啓動時EventAdmin服務已經可用。這是一個固有的不安全假設,因爲您的包實際上可能在提供EventAdmin服務的包之前啓動。

有一個錯誤的方法和一個正確的方法來解決這個問題。錯誤的方法是堅持必須在EventAdmin之後啓動您的包。這種方式導致啓動順序依賴性和極端脆弱性。請參閱:http://wiki.osgi.org/wiki/Avoid_Start_Order_Dependencies

正確的方法是使用聲明式服務(DS)等框架在引用EventAdmin服務的捆綁包中聲明組件。然後,DS會激活您的組件,並在EventAdmin實例可用時儘快注入它。參見:http://wiki.osgi.org/wiki/Declarative_Services

+0

再次感謝,我使用DS,它很好地工作:) – user1985273

相關問題