【问题标题】:OSGI Bundle Installed and Started but no visible outputOSGI Bundle 已安装并启动,但没有可见的输出
【发布时间】:2013-07-20 04:17:05
【问题描述】:

我正在努力学习 OSGI。 (主要是bundle的动态加载和卸载)。

按照 Neil Bartlett 的 How To Embed OSGi 教程,我将 Equinox OSGi 框架实现添加到类路径并开始游戏。

代码如下:

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;


public class BundleManager {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        FrameworkFactory frameworkFactory = ServiceLoader.load(
                FrameworkFactory.class).iterator().next();
        Map<String, String> config = new HashMap<String, String>();
        //TODO: add some config properties
        Framework framework = frameworkFactory.newFramework(config);
        framework.start();



        BundleContext context = framework.getBundleContext();
        List<Bundle> installedBundles = new LinkedList<Bundle>();

        installedBundles.add(context.installBundle(
                              "file:C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar"));


        for (Bundle bundle : installedBundles) {

       if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) == null)
                 bundle.start();

         }


        System.out.println("done!!");



    }

}

是的,它有效。完全没有错误。但是,我安装的包是路径中的 jar 文件:C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar 在其启动方法中包含一个“HelloWorld”。我在 Eclipse 控制台中没有看到“HelloWold”。为什么我在捆绑包已启动时看不到该消息?感谢任何简单的帮助。

注意:HellowService.jar是我之前创建的一个插件项目,在它的一个类中实现了BundleActivator在start方法中添加“HelloWorld”消息,最后导出为jar文件到目录C:/Users/student/Documents/eclipse/myPlugins/

编辑:这是我正在安装和启动的捆绑包中的 Activator 类:

package com.javaworld.sample.service.impl;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import com.javaworld.sample.service.HelloService;

public class HelloServiceActivator implements BundleActivator {

    ServiceRegistration helloServiceRegistration;


        public void start(BundleContext context) throws Exception {

            HelloServiceFactory helloServiceFactory = new HelloServiceFactory();
            helloServiceRegistration =context.registerService(HelloService.class.getName(), helloServiceFactory, null);

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


}

这是捆绑包的 MANIFEST.MF 文件:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloService
Bundle-SymbolicName: com.javaworld.sample.HelloService
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.javaworld.sample.service.impl.HelloServiceActivator
Bundle-Vendor: JAVAWORLD
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: org.osgi.framework;version="1.3.0"
Export-Package: com.javaworld.sample.service

我导出包的方式是右键单击包项目->导出->可运行Jar文件->然后我选择启动配置为BundleManager(这是安装包的类)。

我仍然没有看到“Hello World!”当我从我的应用程序启动捆绑包时的消息。

【问题讨论】:

    标签: frameworks osgi equinox osgi-bundle


    【解决方案1】:

    原来我错误地导出了捆绑包。那是因为我试图自己做。以下是捆绑包应如何导出为 jar 文件的方式:

       Open the plugin export wizard File > Export... > Plug-in Development >
       Deployable plug-ins and fragments . 
       Then select the bundle you want to export and the destination directory. Done!
    

    您现在可以使用 jar 文件的路径来安装包。就我而言,它是:

     installedBundles.add(context.installBundle(
                                  "file:C:/Users/student/Documents/eclipse/myPlugins/plugins/com.javaworld.sample.HelloService_1.0.0.201307220322.jar"));
    

    来源:http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.pde.doc.user%2Fguide%2Ftools%2Fexport_wizards%2Fexport_plugins.htm

    当然感谢@Neil Bartlett

    【讨论】:

      【解决方案2】:

      您的启动器不会等待 OSGi 框架停止。我希望这个程序启动一切,然后立即关闭,因为我们到达了main 方法的末尾。请参阅我的教程,其中我展示了如何使用 Framework.waitForStop 方法。

      话虽如此,我希望您的 HelloWorld 包的输出实际出现在关机之前。因此,该捆绑包中似乎也存在错误。也许您还没有声明激活器?我只能猜测,因为你没有提供任何细节。

      【讨论】:

      • 是的,尼尔...我不在乎它停止的那一刻,但我稍后会添加它(我现在添加它,但我仍然没有看到“你好”)。声明激活器是什么意思?我好像不会做这种事。在哪里,我应该怎么做?谢谢。
      • 如果您的意思是实现 BundleActivator 启动和停止方法,那么我已经提到我在我的问题中做到了。我在您发送的链接中没有看到任何新内容。这基本上是我开始的。
      • 答案很明显,不是吗?清单中的 Bundle-Activator 标头与类名不匹配。
      • 那是我遇到的另一个问题。我现在使用 MANIFEST 文件中所述的相同 Activator 类更新了我的包。但不幸的是,我仍然没有看到“Hello World”消息。我尝试单独运行捆绑包并出现消息,所以我想这与我将其导出为 Jar 的方式有关。我这样做的方法是右键单击项目->导出->可运行的 Jar 文件->然后我选择启动配置为 BundleManager(这是安装包的类)。然而,没有用。没用。
      • 那有什么帮助吗?对不起,如果我一直问,但我真的搜索了很多。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      • 2023-03-20
      • 2023-03-21
      • 2020-12-29
      相关资源
      最近更新 更多