我正在尝试学习OSGI。 (主要是捆绑的动态加载和卸载)。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!!");
}
}
是的,它的工作原理。根本没有错误。但是,我在路径中安装的是一个jar文件的包:C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar
在其启动方法中包含一个“HelloWorld”。我在eclipse控制台中没有看到“HelloWold”。为什么我看不到那个消息,虽然捆绑包已经启动了?我感谢任何简单的帮助。
注:HellowService.jar
是我前面创建的插件项目,在其类中的一个实现BundleActivator的在开始添加方法“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
我导出包的方式是右键单击bundle项目 - >导出 - >运行jar文件 - >然后我选择启动配置为BundleManager(这是安装包的类)。
我还没有看到“Hello World!”当我从我的应用程序启动包时出现消息。
是,尼尔......我不为一时照顾,当它停止,但我会稍后添加它(现在我添加了它,但我仍然看不到'你好')。声明激活器是什么意思?我似乎没有这样做。哪里,我该怎么做?谢谢。 –
以下是关于Bundle-Activator的更多信息:http://wiki.osgi。org/wiki/Bundle-Activator –
如果你的意思是实现BundleActivator启动和停止方法,那么我已经提到过我在我的问题中这样做了。我没有在您发送的链接中看到任何新内容。这基本上是我开始的。 –