我正在尝试学习 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!” 当我从我的应用程序启动捆绑包时的消息。