OSGi#1:Equinox 初探

  1. 云栖社区>
  2. 博客>
  3. 正文

OSGi#1:Equinox 初探

kisimple 2016-04-06 11:54:35 浏览1069
展开阅读全文
From a code point of view, Equinox is an implementation of the OSGi core framework specification, a set of bundles that implement various optional OSGi services and other infrastructure for running OSGi-based systems.

上面是Equinox官网的描述,Equinox是一个OSGi实现,具体就不赘述了,下面来看看Equinox该怎么玩,直接上代码。

按照惯例,来个Hello World的栗子。先打出一个输出Hello World的bundle,

package me.kisimple.just4fun.osgi;

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

public class IBundleActivator implements BundleActivator {

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("Hello,World.");
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("Goodbye,World.");
    }

}

MANIFEST.MF这么写,
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloWorld
Bundle-SymbolicName: me.kisimple.just4fun.osgi.HelloWorld
Bundle-Version: 1.0.0
Bundle-Activator: me.kisimple.just4fun.osgi.IBundleActivator
Bundle-Vendor: KISIMPLE
Bundle-Localization: systembundle
Import-Package: org.osgi.framework

然后打包,
> jar cvfm me.kisimple.just4fun_1.0.0.jar MANIFEST.MF me\kisimple\just4fun\osgi\IBundleActivator.class

接下来需要去官网下载Equinox的SDK,我下的是equinox-SDK-LunaSR2.zip。然后参考官网的quick start文档,我们新建一个container目录,该目录有如下文件,
E:\Projects> tree container /F
E:\PROJECTS\CONTAINER
│  org.eclipse.equinox.common_3.6.200.v20130402-1505.jar
│  org.eclipse.osgi_3.10.2.v20150203-1939.jar
│  org.eclipse.update.configurator_3.3.300.v20140518-1928.jar
│
├─configuration
│      config.ini
│
└─plugins
        me.kisimple.just4fun_1.0.0.jar
        org.apache.felix.gogo.command_0.10.0.v201209301215.jar
        org.apache.felix.gogo.runtime_0.10.0.v201209301036.jar
        org.apache.felix.gogo.shell_0.10.0.v201212101605.jar
        org.eclipse.equinox.console_1.1.0.v20140131-1639.jar

除了me.kisimple.just4fun_1.0.0.jarorg.eclipse.update.configurator_3.3.300.v20140518-1928.jar,其他jar包都是从下载下来的SDK中可以拿到的,me.kisimple.just4fun_1.0.0.jar是上面我们打的输出Hello World的bundle,而org.eclipse.update.configurator_3.3.300.v20140518-1928.jar则是从eclipse(忘记安装的是哪个版本了>_<)的安装目录下的plugins目录中获取(本身eclipse也使用了Equinox,所以也有configuration/config.ini文件和plugins目录)。

configuration/config.ini如下,

osgi.bundles=org.eclipse.equinox.common@2:start, org.eclipse.update.configurator@3:start

alright,接下来就可以启动Equinox了,
E:\Projects\container>java -jar org.eclipse.osgi_3.10.2.v20150203-1939.jar -console
osgi> _

org.eclipse.osgi_3.10.2.v20150203-1939.jarMain-Classorg.eclipse.core.runtime.adaptor.EclipseStarter,除了-console,还支持一些其他选项。源码可以使用git获得,
> git clone http://git.eclipse.org/gitroot/equinox/rt.equinox.framework.git

接下来可以在命令行中启动我们的bundle了。先用ss命令看下bundle的情况,
osgi> ss
"Framework is launched."

id      State       Bundle
0       ACTIVE      org.eclipse.osgi_3.10.2.v20150203-1939
1       ACTIVE      org.eclipse.equinox.common_3.6.200.v20130402-1505
2       ACTIVE      org.eclipse.update.configurator_3.3.300.v20140518-1928
3       RESOLVED    me.kisimple.just4fun.osgi.HelloWorld_1.0.0
4       ACTIVE      org.apache.felix.gogo.command_0.10.0.v201209301215
5       ACTIVE      org.apache.felix.gogo.runtime_0.10.0.v201209301036
6       ACTIVE      org.apache.felix.gogo.shell_0.10.0.v201212101605
7       ACTIVE      org.eclipse.equinox.console_1.1.0.v20140131-1639

启动我们的bundle,
osgi> start 3
Hello,World.

这时候再用ss命令可以看到我们的bundle的状态变成了ACTIVE了,
3       ACTIVE      me.kisimple.just4fun.osgi.HelloWorld_1.0.0

停掉我们的bundle,
osgi> stop 3
Goodbye,World.

这时候bundle的状态就会又回到了RESOLVED

参考资料

网友评论

登录后评论
0/500
评论
kisimple
+ 关注