【问题标题】:Error running a Maven OSGi project运行 Maven OSGi 项目时出错
【发布时间】:2015-12-31 06:17:01
【问题描述】:

我正在尝试运行一个 maven OSGi 捆绑项目,但我得到:

Exception in thread "main" java.lang.NullPointerException: Specified service reference cannot be null.

System.out.println(framework.getBundleContext().getService(reference)); 下方initialize()。

我似乎无法正确理解。我是 OSGi 的新手。请帮助我让项目运行。提前谢谢大家。

这是我嵌入 OSGi 框架的方式,然后尝试启动服务:

private void initialize() throws BundleException, URISyntaxException {
    Map<String, String> map = new HashMap<String, String>();

        // make sure the cache is cleaned
    map.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);

    map.put("ds.showtrace", "true");
    map.put("ds.showerrors", "true");

    FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
    Framework framework = frameworkFactory.newFramework(map);

    System.out.println("Starting OSGi Framework");
    framework.init();
    framework.start();

    loadScrBundle(framework);

    framework.getBundleContext().installBundle("file:" + "D:/core-1.0.jar").start();

    ServiceReference<?> reference = framework.getBundleContext()
    .getServiceReference(HelloWorldService.class.getName());
    System.out.println(framework.getBundleContext().getService(reference));

    for (Bundle bundle : framework.getBundleContext().getBundles()) {
        System.out.println("Bundle: " + bundle.getSymbolicName());
        if (bundle.getRegisteredServices() != null) {
            for (ServiceReference<?> serviceReference : bundle.getRegisteredServices())
                System.out.println("\tRegistered service: " + serviceReference);
        }
    }
}

这就是我导出rev.core.api 服务包的方式:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
                    <Export-Package>rev.core.api</Export-Package>
                    <Bundle-Activator>rev.core.impl.Activator</Bundle-Activator>
                    <Bundle-Vendor>Rev CAIT</Bundle-Vendor>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

【问题讨论】:

    标签: java osgi apache-felix maven-compiler-plugin maven-bundle-plugin


    【解决方案1】:

    我可以想象两个原因:

    服务未注册

    您确定该服务已注册?您没有发布激活器的代码。你的服务注册在Activator的start方法中了吗?

    服务实例实现另一个 HelloworldService 类型

    容器中的任何捆绑包都可能包含 HelloWorldService 类型吗?参见BundleContext.getServiceReference(...)的Javadoc:

    ... 并且注册服务的类名的包与 ServiceReference.isAssignableTo(Bundle, String) 中定义的上下文包的包相匹配。

    如果框架包发现 HelloWorldService 类型的实例不是注册服务的包,则框架包上下文的 BundleContext.getServiceReference 方法将不会返回服务的引用。

    尝试调用 BundleContext.getAllServiceReferences(...)。如果返回一个服务引用但 BundleContext.getServiceReference(...) 没有,这是你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      • 2016-07-15
      • 2017-09-05
      • 2018-10-09
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多