- 浏览: 1151591 次
- 性别:
- 来自: 火星郊区
博客专栏
-
OSGi
浏览量:0
文章分类
- 全部博客 (695)
- 项目管理 (48)
- OSGi (122)
- java (79)
- Vaadin (5)
- RAP (47)
- mysql (40)
- Maven (22)
- SVN (8)
- 孔雀鱼 (10)
- hibernate (9)
- spring (10)
- css (3)
- 年审 (6)
- ant (1)
- jdbc (3)
- FusionCharts (2)
- struts (4)
- 决策分析 (2)
- 生活 (10)
- 架构设计 (5)
- 破解 (2)
- 狼文化 (4)
- JVM (14)
- J2EE (1)
- 应用服务器 (1)
- 我的链接 (5)
- 数学 (2)
- 报表 (1)
- 百科 (6)
- Flex (7)
- log4j (2)
- PHP (1)
- 系统 (2)
- Web前端 (7)
- linux (6)
- Office (1)
- 安全管理 (5)
- python (2)
- dom4j (1)
- 工作流 (3)
- 养生保健 (4)
- Eclipse (8)
- 监控开发 (1)
- 设计 (3)
- CAS (1)
- ZK (41)
- BluePrint (3)
- 工具 (1)
- SWT (7)
- google (2)
- NIO (1)
- 企业文化 (2)
- Windoes (0)
- RCP (7)
- JavaScript (10)
- UML (1)
- 产品经理 (2)
- Velocity (10)
- C (1)
- 单元测试 (1)
- 设计模式 (2)
- 系统分析师 (2)
- 架构 (4)
- 面试 (2)
- 代码走查 (1)
- MongoDB (1)
- 企业流程优化 (1)
- 模式 (1)
- EJB (1)
- Jetty (1)
- Git (13)
- IPV6 (1)
- JQuery (8)
- SSH (1)
- mybatis (10)
- SiteMesh (2)
- JSTL (1)
- veloctiy (1)
- Spring MVC (1)
- struts2 (3)
- Servlet (1)
- 权限管理 (1)
- Java Mina (1)
- java 系统信息 (6)
- OSGi 基础 (3)
- html (1)
- spring--security (6)
- HTML5 (1)
- java爬虫搜索 (1)
- mvc (3)
最新评论
-
Tom.X:
http://osgia.com/
将web容器置于OSGi框架下进行web应用的开发 -
chenyuguxing:
你好, 为什么我的bundle export到felix工程中 ...
在Apache Felix中运行bundle -
string2020:
<niceManifest>true</ni ...
Bundle Plugin for Maven -
jsonmong:
OSGI,是未来的主流,目前已相当成熟。应用OSGI比较好的, ...
基于OSGi的声明式服务 -
zyhui98:
貌似是翻译过来的,有很少人在linux上做开发吧
如何成为“10倍效率”开发者
Introduction
In this tutorial we will build a sample application composed of two components and an API. The following diagram shows the bundle architecture (simplified):
In the tutorial we create the top three bundles (rectangles):
- The API bundle exports a service interface,
Greeting
. - The Provider bundle imports the interface and publishes an instance of the service.
- The Command bundle imports the interface and binds to the service instance, and also publishes a
Command
service that is used by the Felix Shell bundle.
Installing Bndtools
Please refer to the Installation Instructions .
Create an API Project
First we need to create a Bndtools OSGi Project. This is just a standard Eclipse Java Project, with an additional builder for constructing OSGi bundles.
-
From the File menu, select New -> Bndtools OSGi Project .
-
If this is the first time you have used Bndtools, you will now see the “Welcome” dialog. Click Next followed by Finish to allow Bndtools to setup a configuration project and import a basic repository.
-
On the next page, enter
org.example.api
as the name of the project. Select at least J2SE–1.5 for the JRE execution environment. -
Next you are offered a choice of project templates to start off your project. Select Empty Project and click Finish . The new project will be created.
Important Points:
- Bndtools projects are based on standard Eclipse Java (JDT) projects.
- Bndtools uses a
cnf
project containing workspace-wide configuration that is normally shared between developers. It may also contain a repository of bundles. - A file named
bnd.bnd
is created at the top of each Bndtools project, which controls the settings for the project. The same settings are used by bnd when it is invoked from an offline ANT build.
Write and Export the API
OSGi offers strong decoupling of producers and consumers of functionality. This is done by encouraging an API-based (or in Java terms, interface-based) programming model, where producers of functionality implement APIs and the consumers of functionality bind only to APIs, not any particular implementation. For our example we will use a fairly trivial API.
In the src
directory of the new project, create a package named org.example.api
. In the new package create a Java interface named Greeting
, as follows:
package org.example.api;
public interface Greeting {
String sayHello(String name);
}
Define the Bundle
The project we have created defines a single bundle with a Bundle Symbolic Name (BSN) of org.example.api
(i.e., the same as the project name). As soon as we created the project, a bundle file named org.example.api.jar
was created in the generated
directory, and it will be rebuilt every time we change the bundle definition or its source code.
However, the bundle is currently empty, because we have not defined
any Java packages to include in the bundle. This is an important
difference of Bndtools with respect to other tools: bundles are always
empty until we explicitly add some content. You can verify this by
double-clicking the bundle file and viewing its contents: it will have
only META-INF/MANIFEST.MF
and OSGI-OPT/bnd.bnd
entries.
We want to add the package org.example.api
to the exported packages of the bundle. So open the bnd.bnd
file at the top of the project and select the Contents
tab. Now the package can be added in one of two ways:
- Click the “+” icon in the header of the Export Packages
section, then select
org.example.api
from the dialog and click OK … or - Drag-and-drop the package
org.example.api
from Eclipse’s Package Explorer view into the Export Packages list.
(TIP: Advanced users may prefer to enter Export-Package: org.example.api
manually in the Source
tab).
As soon as this is done, a popup dialog appears titled “Missing Package Info”. This dialog is related to package versioning: it is asking us to declare the version of this exported package. Click OK .
The Contents tab should now appear as in the following screenshot:
Save the file, and the bundle will be rebuilt to include the selected export. We can confirm by opening the Imports/Exports view and selecting the bundle file in the Package Explorer . Note the package has been assigned version 1.0.0:
Important Points:
- The project configuration and the bundle contents are defined by
bnd.bnd
. - The identity of a bundle — its “Bundle Symbolic Name” or BSN — is controlled by the project name. In this case, the bundle’s BSN is equal to the project name.
- Bundles are always empty until we explicitly add contents to them. Adding a package to the Export Packages
panel included that package in the bundle, and also declared it as an export in the
META-INF/MANIFEST.MF
. - Normally bundles contain more than just a single interface. This example is intentionally simplistic.
Create an Implementation Project
We will now create another project that defines two bundles: a provider and a client of the Greeting
API.
Create the Project
Create another Bndtools project, named org.example.impls
. At the Project Templates
step, select Component Development (Declarative Services)
and click Finish
.
Add the API as a Build Dependency
We need to add the API project as a build-time dependency of this new project.
The bnd.bnd
file of the newly created project will have opened automatically. Click the Build
tab and add org.example.api
in either of the following ways:
-
Click the “+” icon in the toolbar of the Build Path panel. Double-click
org.example.api
under “Workspace” in the resulting dialog; it will move over to the right-hand side. Click Finish -
OR drag-and-drop
org.example.api
from the Repositories view into the Build Path panel.
In either case, the org.example.api
bundle will appear in the Build Path
panel with the version annotation “latest”:
Important Points:
- Build-time dependencies of the project can be added in the Build Path
panel of the
bnd.bnd
editor. - Adding dependencies in this way (i.e. rather than via Eclipse’s existing “Add to Build Path” menu) ensures that exactly the same dependencies are used when building offline with ANT.
Write an Implementation
We will write a class that implements the Greeting
interface. When the project was created from the template, Java source for a class named org.example.ExampleComponent
was generated. Open this source file now and make it implement Greeting
:
@Component
public class ExampleComponent implements Greeting {
public String sayHello(String name) {
return "Hello " + name;
}
}
Note the use of the @Component
annotation. This enables
our bundle to use OSGi Declarative Services to declare the API
implementation class. This means that instances of the class will be
automatically created and registered with the OSGi service registry.
However the annotation is build-time only, and does not pollute our
class with runtime dependencies — in other words, this is a “Plain Old
Java Object” or POJO.
Test the Implementation
We should write a test case to ensure the implementation class works as expected. In the test
folder, a test case class already exists named org.example.ExampleComponentTest
. Write a test method as follows:
public class ExampleComponentTest extends TestCase {
public void testSaysHello() throws Exception {
String result = new ExampleComponent().sayHello("Bob");
assertEquals("Hello Bob", result);
}
}
Now right-click on the file and select Run As > JUnit Test .
Verify that the JUnit view shows a green bar. If not, go back and fix the code!
Note that, since this is a unit test rather than an integration test, we did not need to run an OSGi Framework; the standard JUnit launcher is used. Again, this is possible because the component under test is a POJO.
Build the Implementation Bundle
As in the previous project, a bundle is automatically built based on the content of bnd.bnd
. In the current project however, we want to build two
separate bundles. To achieve this we need to enable a feature called “sub-bundles”.
Right-click on the project org.example.impls
and select New > Bundle Descriptor
. In the resulting dialog, type the name provider
and click Finish
.
A popup dialog will ask whether to enable sub-bundles. Click OK .
Some settings will be moved from bnd.bnd
into the new provider.bnd
file. You should now find a bundle in generated
named org.example.impls.provider.jar
which contains the org.example
package and a Declarative Services component declaration in OSGI-INF/org.example.ExampleComponent.xml
.
Important Points:
- Bndtools project can output either a single bundle or multiple bundles.
- In the case of single-bundle projects, the contents of that bundle are defined in
bnd.bnd
. - In the case of multi-bundle projects, the contents of each bundle is defined in a separate
.bnd
file. Thebnd.bnd
file is still used to define project-wide settings, such as build dependencies.
Run an OSGi Framework
We’d now like to run OSGi. To achieve this we need to create a “Run Descriptor” that defines the collection of bundles to run, along with some other run-time settings.
Right-click on the project org.example.impls
and select New > Run Descriptor
. In the resulting dialog, enter run
as the file name and click Next
. The next page of the dialog asks us to select a template; choose Apache Felix 4 with Shell
and click Finish
.
In the editor for the new run.bndrun
file, click on Run OSGi
near the top-right corner. Shortly, the Felix Shell prompt “->
” will appear in the Console
view. Type the ps
command to view the list of bundles:
-> ps
START LEVEL 1
ID State Level Name
[ 0] [Active ] [ 0] System Bundle (4.0.0)
[ 1] [Active ] [ 1] Apache Felix Shell Service (1.4.2)
[ 2] [Active ] [ 1] Apache Felix Shell TUI (1.4.1)
Next we want to include the org.example.impls.provider
bundle. This can be done in the following ways:
- Click the “+” icon in the toolbar of the Run Requirements
panel. In the dialog, double-click
org.example.impls.provider
under “Workspace” and click Finish . -
OR
drag-and-drop
org.examples.impls.provider
from under Workspace in the Run Repositories to the Run Requirements panel.
Either way, the Run Requirements panel should now look like this:
Check Auto-resolve on save
and then save the file. Returning to the Console
view, type ps
again:
-> ps
START LEVEL 1
ID State Level Name
[ 0] [Active ] [ 0] System Bundle (4.0.0)
[ 1] [Active ] [ 1] Apache Felix Shell Service (1.4.2)
[ 2] [Active ] [ 1] Apache Felix Shell TUI (1.4.1)
[ 3] [Active ] [ 1] org.example.api (0)
[ 4] [Active ] [ 1] org.example.impls.provider (0)
The provider bundle has been added to the runtime dynamically. Note that the API bundle was also added because it was resolved as a dependency of the provider.
Important Points:
- Run-time configurations can be defined in a
.bndrun
file. Multiple different run configurations can be used, resulting in different sets of bundles, different OSGi Framework implementations etc. - The set of bundles to include is derived from the Run Requirements list. Bndtools uses OBR resolution to resolve a list of bundles including their static dependencies.
- If the OSGi Framework is still running, then saving the
bndrun
file will cause the list of bundles to be dynamically updated. So we can add and remove bundles without restarting. - Editing an existing bundle — including editing the Java code that comprises it — will also result in the bundle being dynamically updated in the runtime.
Add the Declarative Services Runtime
The provider bundle uses Declarative Services; however for the component to be activated we need to include the runtime part of Declarative Services, known as the Service Component Runtime (SCR).
In the Run Repositories
panel there is a search box; type scr
and hit enter: org.apache.felix.scr
will appear as a match. Drag this bundle into the Run Requirements
panel.
Save the file. This time a dialog appears during save, because the SCR bundle has optional dependencies that we might want to include. Just click Finish .
In the Console
view, type ps
again. Both the Declarative Services and OSGi Compendium (“cmpn”) bundles have been added:
-> ps
START LEVEL 1
ID State Level Name
[ 0] [Active ] [ 0] System Bundle (4.0.0)
[ 1] [Active ] [ 1] Apache Felix Shell Service (1.4.2)
[ 2] [Active ] [ 1] Apache Felix Shell TUI (1.4.1)
[ 3] [Active ] [ 1] org.example.api (0)
[ 4] [Active ] [ 1] org.example.impls.provider (0)
[ 5] [Active ] [ 1] Apache Felix Declarative Services (1.6.0)
[ 6] [Active ] [ 1] osgi.cmpn (4.2.1.201001051203)
We can now look at the services published by our provider bundle using the command inspect service capability 4
… (or the short form inspect s c 4
):
-> inspect s c 4
org.example.impls.provider (4) provides services:
-------------------------------------------------
component.id = 0
component.name = org.example.ExampleComponent
objectClass = org.example.api.Greeting
service.id = 27
Our bundle now publishes a service under the Greeting
interface.
Write a Command Component
Finally we will write a component that consumes the Greeting service and publishes a shell command that can be invoked from the Felix shell.
First we need to make the Felix shell API available to compile against. Open bnd.bnd
and change to the Build
tab. Add org.apache.felix.shell
to the list of build dependencies, and save the file:
Now create a new Java package under the src
folder named org.example.command
. In this package create a class GreetingCommand
as follows:
import java.io.PrintStream;
import java.util.StringTokenizer;
import org.apache.felix.shell.Command;
import org.example.api.Greeting;
import aQute.bnd.annotation.component.*;
@Component
public class GreetingCommand implements Command {
private Greeting greetingSvc;
@Reference
public void setGreeting(Greeting greetingSvc) {
this.greetingSvc = greetingSvc;
}
public void execute(String line, PrintStream out, PrintStream err) {
StringTokenizer tokenizer = new StringTokenizer(line);
tokenizer.nextToken(); // discard first token
String name = "";
if (tokenizer.hasMoreTokens())
name = tokenizer.nextToken();
System.out.println(greetingSvc.sayHello(name));
}
public String getName() {
return "greet";
}
public String getShortDescription() {
return "Example command";
}
public String getUsage() {
return "greet <name>";
}
}
Create a Bundle for the Command Component
The command component is not part of the provider bundle, because it lives in a package that was not included. We could add it to the provider bundle, but it would make more sense to create a separate bundle for it.
Right-click again on the org.example.impls
project and select New > Bundle Descriptor
again. Enter the name as command
and click Finish
.
Add the package org.example.command
to the Private Packages
panel of the newly created file. As before, this can be done using the “+” button in the toolbar or by drag-and-drop.
We also need to declare that the bundle contains Declarative Services components. Change to the Components tab of the editor and click the Add button. In the name field, enter “*” (i.e. asterisk). Now save the file.
Add the Command Bundle to the Runtime
Switch back to the editor for run.bndrun
. In the Run Requirements
tab, add the org.example.impls.command
bundle, and save the file.
The command bundle will now appear in the list of bundles when typing ps
:
-> ps
START LEVEL 1
ID State Level Name
[ 0] [Active ] [ 0] System Bundle (4.0.0)
[ 1] [Active ] [ 1] Apache Felix Shell Service (1.4.2)
[ 2] [Active ] [ 1] Apache Felix Shell TUI (1.4.1)
[ 3] [Active ] [ 1] org.example.api (0)
[ 4] [Active ] [ 1] org.example.impls.provider (0)
[ 5] [Active ] [ 1] Apache Felix Declarative Services (1.6.0)
[ 6] [Active ] [ 1] osgi.cmpn (4.2.1.201001051203)
[ 7] [Active ] [ 1] org.example.impls.command (0)
Finally, the greet
command will now be available from the Felix shell:
-> greet Neil
Hello Neil
发表评论
-
关于Felix Log Service
2012-12-07 16:44 1571OSGi服务纲要规范中定义了服务于OSGi平台的通用日志服 ... -
Maven 3 Felix 4 Eclipse 的搭建与部署(部分转载自别人文章)
2012-10-18 10:24 20374.1.开发环境搭建 4.2开发工具 Maven 3 F ... -
【绝对路径】OSGi环境中获取Plugin/Bundle中文件资源的绝对路径
2012-10-08 10:53 2507摘要:在进行Eclipse RCP开发的过程中,需要使用一 ... -
OpenCore:基于OSGi开发纯插件体系结构的WEB应用程序
2012-09-21 17:46 1426随着OSGi/Equinox逐渐成为Java EE服务端的基础 ... -
OSGi技术在Java Web开发中的应用
2012-09-20 11:26 1416随着 Java SE 对模块化功能原生支持的一再推迟(据最 ... -
OSGI典型的应用案例
2012-09-20 11:26 1638OSGI典型的应用案例主要有两个:分别是Eclipse和BMW ... -
OSGi特点
2012-09-20 11:26 12541、JRE版本无关性。虽然Java一直被人们认为是“Write ... -
OSGI与JMX 的关系
2012-09-19 17:09 1061不过重点是: JMX 本来设计的用途就只为了管理,我们不 ... -
在equinox环境开发web应用的"利器" -- registerResources()方法 详解
2012-09-19 17:07 1235registerResources()方法详解 1、简介 ... -
在equinox环境开发web应用的"利器" -- 序
2012-09-19 17:05 1367在equinox环境中开发web应用必须要借助一些工具包提供的 ... -
equinox环境下web应用资源的部署
2012-09-19 17:04 1309osgi的equinox实现环境下,web服务器和web应用都 ... -
OSGi产生的背景--在繁荣的混乱之中走出困惑
2012-09-19 16:58 1169软件的复杂性正在以惊 ... -
将web容器置于OSGi框架下进行web应用的开发
2012-09-16 14:26 3543将web容器置于OSGi框架下,其实就是将web容器做成OSG ... -
在Eclipse中开发OSGi Bundle
2012-09-16 14:26 1335Eclipse为开发OSGI Bundle提供了良好的支持,它 ... -
【第一代服务注册形式】 - 将一个Bundle注册为服务
2012-09-14 10:09 11581、创建业务接口类及其实现类 Java代码 ... -
Declarative Services规范简介及应用
2012-09-14 10:08 1424Declarative Services 是一 ... -
用FileInstall管理Bundle的动态安装、启动、卸载
2012-09-14 10:07 13351、文件目录如下: F:\study_osgi ... -
服务工厂 - Bundle消费者返回不同的服务对象
2012-09-14 10:03 1210一般情况下,服务对象在注册后,任何其它的Bundle在请求 ... -
服务跟踪(ServiceTracker)
2012-09-14 09:58 1162当多个Bundle使用同一 ... -
OSGi容器中Bundle之间Synchronous Communication
2012-09-11 17:07 1565OSGi Core定义了一个服务层,提供了一个Bundl ...
相关推荐
FLUENT 2020R2 tutorial guide PDF及案例源文件 1.What’s In This Manual The ANSYS Fluent Tutorial Guide contains a number of tutorials that teach you how to use ANSYS Flu- ent to solve different types ...
operating_system_tutorial.pdf operating_system_tutorial.pdf operating_system_tutorial.pdf operating_system_tutorial.pdf operating_system_tutorial.pdf operating_system_tutorial.pdf operating_system_...
Tutorial Master是一个编辑器扩展,允许您创建您的游戏互动教程惊人的快速和容易! Tutorial Master2 已经建立了从地面到成为最好的教程制作解决方案之一!教导新玩家(以及潜在用户)如何以你所希望的方式玩游戏! 用户...
Sun权威教程--《J2EE Tutorial中文版》 作者:Stephanie Bodoff,Dale Green,Kim Haase,Eric Jendrock,Monica Pawlan,Beth Stearns 翻译参与人员:sharetop,worldheart,zhaoy,bruce等 出版商:铁道出版社...
Ansys Fluent Tutorial Guide 2022 R1官方示例(全套官方案例): 包括FLUENT模块所有官方案例三维示例网格文件(31个),还包括帮助文档PDF文件Ansys_Fluent_Tutorial_Guide_2022_R1.pdf,学习FLUENT各个官方案例的...
《EDEM教程1 - 传送带》是一份官方的学习资料,专为仿真爱好者设计,旨在帮助他们理解和掌握EDEM软件的使用。EDEM(Eulerian Discrete Element Method)是一种基于离散元方法的颗粒流体动力学软件,广泛应用于各种...
### ArcGIS Desktop Tutorial Data知识点详解 #### 一、ArcGIS Desktop简介 ArcGIS Desktop是一款功能强大的地理信息系统(GIS)软件套件,由Esri公司开发并维护。它为用户提供了一个全面的工具集来创建、编辑、...
ANSYS 2021R2 Fuent_Tutorial_Package.zip,包括FLUENT模块所有官方案例三维示例网格文件,还有包括帮助文档PDF文件Ansys_Fluent_Tutorial_Guide_2021_R2.pdf,学习FLUENT各个官方案例的仿真步骤设置过程,依照各个...
《Computational Fourier Optics:a MATLAB tutorial》是关于傅里叶光学的一个教程,结合了MATLAB编程,旨在帮助读者深入理解和应用傅里叶光学的基本原理。傅里叶光学是光学领域的一个重要分支,主要研究光学系统...
Understanding .NET - A Tutorial and Analysis Understanding .NET - A Tutorial and Analysis Understanding .NET - A Tutorial and Analysis
FLUENT 6.3 Tutorial Guide 全套官方案例文件包括 ①FLUENT 6.3 Tutorial Guide.pdf(文件详述FLUENT仿真操作步骤) ②FLUENT 6.3 Tutorial Mesh and Solution Files(包含22个教程案例的所有网格模型文件和求解文件...
《Ansys Fluent Tutorial Guide 2022 R1》是Ansys公司于2022年1月发布的官方教程文档,旨在为用户提供Ansys Fluent软件的详细操作指导和案例仿真分析。Ansys Fluent是一款强大的计算流体动力学(CFD)软件,广泛应用...
**ANSYS Fluent 19.2 教程详解** ANSYS Fluent是一款强大的计算流体动力学(CFD)软件,广泛应用于工程领域的流体流动、热传递和化学反应模拟。在19.2版本的教程中,用户可以深入学习如何有效地使用这款软件进行...
Alex Smola et al.A tutorial on support vector regression.Statistics and Computing,2004 In this tutorial we give an overview of the basic ideas underlying Support Vector (SV) machines for function ...
《Ruby on Rails Tutorial》中文版(原书第2版,涵盖 Rails 4) Ruby 是一门很美的计算机语言,其设计原则就是“让编程人员快乐”。David Heinemeier Hansson 就是看重了这一点,才在开发 Rails 框架时选择了 Ruby...
Verdi User Guide and Tutorial.pdf April 1, 2009 350页,全英文高清晰
Java 8 简明教程 Java 8 Tutorial中文版 “Java并没有没落,人们很快就会发现这一点” 欢迎阅读我编写的Java 8介绍。本教程将带领你一步一步地认识这门语言的新特 性。通过简单明了的代码示例,你将会学习到如何使用...
ANSYS Fluent Tutorial Guide 2019R1 ANSYS Fluent帮助文档
java tutorialjava tutorial.chm