接【osgi】第一个bundle helloworld(1)
在eclipse新建bundle其实很简单,新建一个插件工程如下图。
provider为发布这个bundle的公司名称,Activator可以不改。
点击finish,创建bundle就完成了。接下来做一些小处理以后把bundle运行起来。
找到src下面的Activator.java。打开可以看到start和stop方法,start在bundle启动时运行,stop在bundle停止时运行。添加代码如下:
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
System.out.println("Hello world!!!");
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
System.out.println("Goodbye world!!!");
}
然后保存,运行方法在bundle helloworld(1)中已经提到,不同地方是需要把helloworld bundle也选中,如下图。
点击run,看到osgi> Hello world!!!表示已经成功。
下面介绍一下osgi console的部分命令,现在你可以在helloworld直接尝试
- ss: 该命令显示所有已安装的Bundles及它们的状态,它将显示Bundle ID,Bundle的简短名称及Bundle状态;
- start<bundleid>: 该命令将启动一个Bundle;
- stop<bundleid>: 该命令将停止一个Bundle;
- update<bundleid>: 该命令使用新的JAR文件更新一个Bundle;
- install<bundleid>: 该命令将一个新的Bundle安装到OSGi容器;
- uninstall<bundleid>: 从OSGi容器中卸载一个已安装的Bundle。