Over the next week or two, EclipseZone will be running a series of short posts on OSGi. Taken together they should form a smooth path into mastering the art of OSGi programming, but each post will introduce just one new technique and it should be possible to work through in under ten minutes. Also, we want to show how simple OSGi development can be, so we will not be using Eclipse for development - just a text editor and the basic command line tools will do. So, welcome to the "Getting started with OSGi" series.
Actually this first post will be a little longer than the others, because we need to set up a very basic working environment. Before getting started, we need an OSGi framework to run on. There are three open source implementations to choose from: Apache Felix , Knopflerfish , and Equinox . The code we're going to write will be identical no matter which one you choose, but the instructions for running it will be a little different. Since this is EclipseZone we will use Equinox, the runtime that Eclipse itself is built on. You can pull a copy of it right out of your existing Eclipse installation: just find the file org.eclipse.osgi_3.2.1.R32x_v20060919.jar and copy it to an empty directory (NB the version string might be a little different depending on what version of Eclipse you have). If you don't have a copy of Eclipse anywhere, then you can download just that Jar file from http://download.eclipse.org/eclipse/equinox/ .
To keep the commands short, let's rename the Jar file to equinox.jar . Now bring up a Command Prompt in our development directory and run the command
> java -jar equinox.jar -console
In a few seconds, the osgi> prompt should appear. Congratulations, you are now running OSGi!
The osgi> prompt gives us access to commands in Equinox to control the framework. If you like, type help to see a list of commands, and have a play with them. Done that? Now type ss . This is the most frequently used command; it stands for "short status" and it shows us the list of bundles that are installed, and what their current status is. (A "bundle" is a module in OSGi terminology. Or if you are an Eclipse developer, you may know them as plug-ins; bundles and plug-ins are basically the same things.)
Equinox should print out the following:
Framework is launched.
id State Bundle
0 ACTIVE system.bundle_3.2.1.R32x_v20060919
This tells us that there is one bundle installed and active, and it is the System Bundle. This is a special bundle in OSGi that is always present, and it represents the framework itself.
Now, we're going to write our own bundle. In the same directory as before, create a file called HelloActivator.java and copy the following code into it:
import org.osgi.framework.*;
public class HelloActivator implements BundleActivator {
public void start(BundleContext context) {
System.out.println("Hello EclipseZone Readers!");
}
public void stop(BundleContext context) {
System.out.println("Goodbye EclipseZone Readers!");
}
}
A bundle also needs a manifest file that declares various metadata about the bundle, e.g. its name, version, etc. So create a file called HelloWorld.mf and copy the following text into it. Make very sure that this file ends with a blank line, otherwise the jar command line tool will truncate the file.
Manifest-Version: 1.0
Bundle-Name: HelloWorld
Bundle-Activator: HelloActivator
Bundle-SymbolicName: HelloWorld
Bundle-Version: 1.0.0
Import-Package: org.osgi.framework
Now open a new Command Prompt (because we want to leave OSGi running) and build the Jar with the following commands:
> javac -classpath equinox.jar HelloActivator.java
> jar -cfm HelloWorld.jar HelloWorld.mf HelloActivator.class
Going back into the OSGi console, type install file:HelloWorld.jar . The reply should be "Bundle id is 1" . Type ss again and you will see the following:
Framework is launched.
id State Bundle
0 ACTIVE system.bundle_3.2.1.R32x_v20060919
1 INSTALLED HelloWorld_1.0.0
Our HelloWorld bundle is installed... but it's not yet active. We'll look into what these states mean in a later post, but for now we just need to start the bundle by typing start 1 . The "1" is the ID of the bundle from the first column. When you do this you should see the message "Hello EclipseZone Readers!". Now type stop 1 and you will see "Goodbye EclipseZone Readers!". Repeat this until you get bored. Don't forget to do ss occasionally to see the state of the bundle changing.
What's happening here? Our code implements the BundleActivator interface, allowing the framework to notify us of important lifecycle events. When the bundle is started, the framework calls the start method, and when the bundle is stopped, the framework calls the stop method. The other thing going on here is the line in the manifest file "Bundle-Activator: HelloActivator" , which tells the framework which class in our bundle is the activator. Normally the name we give is a fully-qualified class name, but we were lazy and used the default package.
And that concludes our first installment. See you next time.
分享到:
相关推荐
深入理解OSGi:Equinox原理、应用与最佳实践.pdfOSGi应用开发
NULL 博文链接:https://zhouwendong006.iteye.com/blog/381680
NULL 博文链接:https://zhouwendong006.iteye.com/blog/408415
NULL 博文链接:https://zhouwendong006.iteye.com/blog/382882
深入理解OSGi:Equinox原理、应用与最佳实践源代码,同时附带equinox-SDK-3.8源代码。
OSGi with CAR-Bundle.ppt
深入理解OSGi:Equinox原理、应用与最佳实践,书本源代码
《深入理解OSGi:Equinox原理、应用与最佳实践》共14章,分4个部分。第一部分(第1 章):走近OSGi,主要介绍了什么是OSGi以及为什么要使用OSGi。第二部分(第2~4章):OSGi规范与原理,对最新的OSGi R5.0中的核心...
深入理解OSGi:Equinox原理、应用与最佳实践源码
《深入理解OSGi:Equinox原理、应用与最佳实践》自从1999年OSGi联盟成立以来,OSGi技术随着Java一起飞速发展,它已经成为一种被广泛认可的软件架构技术和方法,许多世界著名的IT企业都加入到OSGi的阵营之中,OSGi...
《深入理解OSGi:Equinox原理、应用与最佳实践》一书的附赠光盘
《深入理解OSGi:Equinox原理、应用与最佳实践》,作者周志明 / 谢小明,本书是原创Java技术图书领域继《深入理解Java虚拟机》后的又一实力之作,也是全球首本基于最新OSGi R5.0规范的著作。
《Eclipse RCP与Spring OSGi:技术详解与最佳实践》由资源的Eclipse专家亲自执笔,并得到了Eclipse官方技术社区的强烈推荐,权威性毋庸置疑!内容全面,系统讲解了利用Eclipse RCP和Spring OSGi开发大规模Java应用的...
全面解读OSGi规范,深刻揭示OSGi原理,详细讲解OSGi服务,系统地介绍Equinox框架的用法,并通过源代码分析其工作机制,包含大量可操作性极强的解决方案和最佳实践。
Building Modular Cloud Apps with OSGi
OSGI bundle 在线用户管理 邮件发送
NULL 博文链接:https://springsfeng.iteye.com/blog/1259203
Getting Started with OSGi 教你如何开发OSGi服务.