首页 文章

OSGI Bundle已安装并已启动,但没有可见的输出

提问于
浏览
0

我正在努力学习OSGI . (主要是捆绑动态加载和卸载) .

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



    }

}

是的,它有效 . 完全没有错误 . 但是,我安装的bundle是路径中的jar文件: C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar 在其start方法中包含"HelloWorld" . 虽然捆绑包已启动,但我没有看到该消息?我感谢任何简单的帮助 .

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

Edit :这是我正在安装和启动的软件包中的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

我导出捆绑包的方式是右键单击捆绑项目 - > Export-> Runnable Jar File->然后我选择启动配置为BundleManager(这是安装捆绑包的类) .

我还是没有看到“Hello World!”从我的应用程序启动捆绑包时出现错误消息

2 回答

  • 0

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

    话虽如此,我希望你的HelloWorld包的输出真正出现在关机之前 . 所以似乎该捆绑包中也存在错误 . 也许你还没有宣布激活剂?我只能猜测,因为你没有提供任何细节 .

  • 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

相关问题