很长时间以来,我一直在努力理解OSGi的真正含义。 它已经存在很长时间了,但是没有多少人意识到这一点。 人们已经大肆宣传它是一种非常复杂的技术。 这是我为所有Java开发人员简化的尝试。

简而言之, OSGi是一组规范,这些规范允许对使用Java技术构建的软件进行模块化组装。 它定义了组件–服务的创建和注册(在启用OSGi的容器中),以便进行内部通信。 OSGi的另一个好处是,所有这些服务都可以在运行时安装/卸载/启动/停止(即可以在运行时热部署代码)。 与在诸如WebSphere , JBoss , WebLogic的流行J2EE应用服务器中找到的Tomcat和EJB容器之类的Servlet容器实现类似, OSGi也具有诸如Equinox (这是Eclipse的基础), Apache Felix …等流行的容器实现。

面向服务的方法是OSGi的强项之一,但是当您不得不处理具有大量依赖关系的应用程序时,我觉得这很重要。 OSGi解决了“ Jar Hell”的问题。
一个例子。 假设您在应用程序中使用两个库libX和libY。 我们还假设它们每个在libZ中都有一个依赖关系,但是版本不同。 libX取决于libZ 2.0,而libY取决于libZ 1.0 如果libZ 2.0与libZ 1.0不兼容,则在同一应用程序中同时使用它们时,可能会遇到难以解决的问题。 OSGi可以处理此类问题。 OSGi支持Import-Package指令,该指令可用于为应用程序-服务应使用的任何Java包指定一个版本。 OSGi类加载器能够根据此信息找到正确的包/罐。 在我之前的示例中,如果库libX,libY和libZ与OSGi兼容,则可以将它们全部加载到同一JVM中而不会出现问题: libZ 1.0将使用Export-Package指令org.libz; 版本= 1.0 libZ 2.0将使用Export-Package指令org.libz; 版本= 2.0 libX将使用Import-Package指令org.libz; 版本= 2.0 libY将使用Import-Package指令org.libz; 版本= 1.0 OSGi还为Java应用程序带来了更强大的模块化概念。 只能在捆绑软件之外使用通过Export-Package指令导出的软件包。

在本文中,我将解释使用Eclipse Equinox容器的OSGi 。 在计算机上安装了Eclipse IDE的任何人也将OSGi容器安装在Eclipse插件的文件夹中。

OSGi容器jar文件的名称类似于org.eclipse.osgi_ <version> .jar

您可以像这样启动OSGi

java -jar org.eclipse.osgi_3.5.2.R35x_v20100126.jar -console

附件是我如何启动OSGi容器的示例屏幕截图(类似于启动Tomcat )

现在,我们已经启动了OSGi容器,让我们使用Maven创建一个“ HelloWorld” OSGi应用程序。 项目结构如下所示:

以下是该项目的pom.xmlpom.xml还添加了2个配置文件,以便创建另外2个新模块( MathServiceMathServiceClient ),本文稍后将对此进行说明。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.linkwithweb.osgi</groupId><artifactId>HelloWorld</artifactId><version>0.0.1-SNAPSHOT</version><name>HelloWorld</name><dependencies><dependency><groupId>org.osgi</groupId><artifactId>org.osgi.core</artifactId><version>4.2.0</version></dependency></dependencies><build><finalName>HelloWorld-${version}</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.1</version><configuration><source>1.5</source><target>1.5</target></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><configuration><archive><manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile></archive></configuration></plugin></plugins></build><profiles><profile><id>MathService</id><build><finalName>MathService-${version}</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.1</version><configuration><source>1.5</source><target>1.5</target></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><configuration><excludes><exclude>**/*.xml</exclude><exclude>**/*.bsh</exclude><exclude>**/*.properties</exclude></excludes><archive><manifestFile>src/main/resources/MathService/META-INF/MANIFEST.MF</manifestFile></archive></configuration></plugin></plugins></build></profile><profile><id>MathServiceClient</id><build><finalName>MathServiceClient-${version}</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.1</version><configuration><source>1.5</source><target>1.5</target></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><configuration><excludes><exclude>**/*.xml</exclude><exclude>**/*.bsh</exclude><exclude>**/*.properties</exclude></excludes><archive><manifestFile>src/main/resources/MathServiceClient/META-INF/MANIFEST.MF</manifestFile></archive></configuration></plugin></plugins></build></profile></profiles></project>

如果仔细观察pom.xml,您将看到我们创建的每个OSGi捆绑包都有3个MANIFEST.MF定义。 这么说,让我解释一下OSGi捆绑包是什么。 OSGi软件包本质上与标准Java“ jar”文件相同,但其特定配置在“ jar的Manifest”文件中定义。 OSGi容器读取“ jar”的清单文件中的所有OSGi特定条目,以激活捆绑包。 那不是很酷吗? 使用OSGi,我们避免像其他框架一样学习任何新的元数据格式!

这是我为MathServiceClient 捆绑包定义的示例Manifest.MF

Manifest-Version: 1.0
Bundle-Name: MathServiceClient
Bundle-Activator: com.linkwithweb.osgi.service.client.MathServiceClientActivator
Bundle-SymbolicName: MathServiceClient
Bundle-Version: 1.0.0
Import-Package: org.osgi.framework,com.linkwithweb.osgi.service

如您所见,除Manifest-Version以外的所有条目都是OSGi特定的。 这些条目定义了如何激活捆绑软件,捆绑软件的名称和版本,其所有从属库以及暴露给其他服务使用的扩展点。

让我向您展示如何将“ HelloWorld”捆绑软件安装到Equinox OSGi Container中。 以下是“ HelloWorld”捆绑包的MANIFEST.MF文件和Activator类。

package com.linkwithweb.osgi;import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;/*** @author Ashwin Kumar**/
public class HelloActivator implements BundleActivator {public void start(BundleContext context) {System.out.println("Hello World");}public void stop(BundleContext context) {System.out.println("Goodbye All");}
}
Manifest-Version: 1.0
Bundle-Name: HelloWorld
Bundle-Activator: com.linkwithweb.osgi.HelloActivator
Bundle-SymbolicName: HelloWorld
Bundle-Version: 1.0.0
Import-Package: org.osgi.framework

要构建捆绑包,请运行“ mvn clean package”

它将在您的Maven项目的目标文件夹中创建HelloWorld-0.0.1-SNAPSHOT.jar 。 这是显示如何在Equinox中安装和启动“ HelloWorld”捆绑软件的图像

如您所见,我们使用install命令安装捆绑软件,并使用start命令,使用捆绑软件安装后容器返回的捆绑软件ID启动捆绑软件。

现在,就捆绑软件的生命周期而言,启动捆绑软件将触发对捆绑软件的Activator类的“ start ”方法的调用,而停止捆绑软件将触发对捆绑软件的Activator类的“ stop ”方法的调用。 我们可以在容器的终端中看到上述行为的结果,该终端在模块启动时显示“ Hello World”消息!

恭喜,您已经了解了OSGi的基础知识,并且已经部署了第一个捆绑软件!

曝光和消费服务

为了解释这一点,我将实现一个非常简单的示例,在该示例中,我将发布一个可以添加两个数字的服务。

首先,我们需要定义一个接口,以向外部捆绑包(客户端)公开“添加”功能

package com.linkwithweb.osgi.service;/*** @author Ashwin Kumar**/
public interface MathService {/*** @param a* @param b* @return*/public int add(int a, int b);
}

现在执行类

package com.linkwithweb.osgi.service;/*** @author Ashwin Kumar**/
public class MathServiceImpl implements MathService {/* (non-Javadoc)* @see com.linkwithweb.osgi.service.MathService#add(int, int)*/public int add(int a, int b) {// TODO Auto-generated method stubreturn a+b;}}

接下来是Activator类,该类将“添加”服务注册到OSGi容器。

package com.linkwithweb.osgi.service;import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;/*** @author Ashwin Kumar**/
public class MathServiceActivator implements BundleActivator {/** (non-Javadoc)** @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)*/public void start(BundleContext context) {MathService service = new MathServiceImpl();// Third parameter is a hashmap which allows to configure the service// Not required in this examplecontext.registerService(MathService.class.getName(), service, null);System.out.println("Math Service Registered");}/** (non-Javadoc)** @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)*/public void stop(BundleContext context) {System.out.println("Goodbye From math service");}
}

以下是“添加”服务包的清单文件。

Manifest-Version: 1.0
Bundle-Name: MathService
Bundle-Activator: com.linkwithweb.osgi.service.MathServiceActivator
Bundle-SymbolicName: MathService
Bundle-Version: 1.0.0
Import-Package: org.osgi.framework
Export-Package: com.linkwithweb.osgi.service

如果您观察上面的清单,您会注意到我们正在导出一些软件包,以便以后使用。 同样,必须在此处定义所有运行时所需的软件包(使用Import-Package指令)。

就像本文的上一节一样,使用以下命令来构建jar文件

mvn -PMathService软件包

您可以在下面看到安装和启动OSGi捆绑软件的命令。

以下是“添加”服务的使用者的实现。 消费者打包在OSGi捆绑激活器类中,仅用于演示目的。 您可以自由地将使用者作为单独的OSGi服务实施,因为OSGi服务可以相互通信!

package com.linkwithweb.osgi.service.client;import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;import com.linkwithweb.osgi.service.MathService;/*** @author Ashwin Kumar**/
public class MathServiceClientActivator implements BundleActivator {MathService service;private BundleContext context;/** (non-Javadoc)** @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)*/public void start(BundleContext context) {this.context = context;// Register directly with the serviceServiceReference reference = context.getServiceReference(MathService.class.getName());service = (MathService) context.getService(reference);System.out.println(service.add(1, 2));}    /** (non-Javadoc)** @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)*/public void stop(BundleContext context) {System.out.println(service.add(5, 6));}
}

跟随是“添加”服务使用者捆绑包的清单文件。

Manifest-Version: 1.0
Bundle-Name: MathServiceClient
Bundle-Activator: com.linkwithweb.osgi.service.client.MathServiceClientActivator
Bundle-SymbolicName: MathServiceClient
Bundle-Version: 1.0.0
Import-Package: org.osgi.framework,com.linkwithweb.osgi.service

要创建,安装并启动“添加”服务客户端捆绑包,请遵循以下步骤:

mvn -PMathServiceClient软件包

就这样! 希望你喜欢它!

您可以在此处下载本文的源代码

参考:来自Felicitas和Beatitudo博客的 JCG合作伙伴 Aswin的OSGI,适合初学者使用Maven和Equinox(HowTo) 。

相关文章 :
  • Java Code Geeks Andygene Web原型
  • Spring,Quartz和JavaMail集成教程
  • 使用Spring将POJO公开为JMX MBean
  • 依赖注入–手动方式

翻译自: https://www.javacodegeeks.com/2011/06/osgi-using-maven-equinox.html

OSGi将Maven与Equinox结合使用相关推荐

  1. maven osgi_OSGi将Maven与Equinox结合使用

    maven osgi 很长时间以来,我一直在努力理解OSGi的真正含义. 它已经存在很长时间了,但是没有多少人意识到这一点. 它被炒作是一种非常复杂的技术. 这是我为所有Java开发人员简化的尝试. ...

  2. OSGi框架之Eclipse Equinox

    作者:俞黎敏 Equinox紧随Eclipse之后,采用天文名词,指节气中的昼夜平分时刻,是为春分,抑或秋分.从Eclipse 3.0开始,Eclipse通过选择开放服务网关协议(Open Servi ...

  3. OSGI和Maven结合

    1.创建项目 创建一个maven项目,并在该项目下创建两个maven module,一个为server,令一个为client server为发布服务的bundle,client为使用服务的bundle ...

  4. osgi简介_OSGi:简介

    osgi简介 为基于Java的系统创建的OSGi提供了模块化系统的框架. OSGi使得可以定义每个单独模块与其他模块的依赖关系,并使用户能够控制生命周期并动态更改系统的每个组件. OSGi是一个规范, ...

  5. vaadin_Vaadin附加组件和Maven

    vaadin 介绍 我喜欢Vaadin的 (其中很多)一件事是它对Vaadin框架的"附加组件"社区-他们称之为Vaadin目录 . "附加组件"是社区对框架的 ...

  6. ivy maven_将Maven与Ivy集成

    ivy maven 问题是:您在Ivy存储库中(只有那里)有一些资源,您想在基于Maven的项目中使用这些资源. 可能的解决方案: 由于Ivy可以轻松使用Maven样式的存储库(因此,您的Ivy客户端 ...

  7. osgi和spring区别_OSGI和Spring动态模块–简单的Hello World

    osgi和spring区别 在此姿势中,我们将采用使用OSGi进行的第一个实现,并使用Spring Dynamic Modules改进应用程序. Spring动态模块(Spring Dm)使基于OSG ...

  8. Vaadin附加组件和Maven

    介绍 我喜欢Vaadin的 (众多)一件事是它对Vaadin框架的"附加组件"社区-他们称之为Vaadin目录 . "附加组件"是框架中社区贡献的附加组件,可以 ...

  9. 将Maven与Ivy集成

    问题是:您在Ivy存储库中(只有那里)有一些资源,您想在基于Maven的项目中使用这些资源. 可能的解决方案: 由于Ivy可以轻松使用Maven风格的存储库(因此,您的Ivy客户端可以继续使用Ivy并 ...

最新文章

  1. 西安电子科技大学第16届程序设计竞赛 E题
  2. ASP.NE的缓存技术提高Web站点的性能
  3. 从浪漫走向坚韧:开源数据库的演变
  4. php原生读取excel文件夹,原生php实现excel文件读写的方法分析php技巧
  5. 搜索、推荐、广告中的曝光偏差问题
  6. 7-15 sdut-数据类型-4 第七次全国人口普查数据分析
  7. 关于SETUP FACTORY %AppFolder%写入注册表问题
  8. (附源码)Springboot宠物医院管理系统 毕业设计 180923
  9. 眼部卸妆液行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  10. 深度清理mac磁盘空间的方法,方方面面都清理干净
  11. Android8.1 MTK平台 修改系统默认语言和默认输入法
  12. Javascript(js)进阶学习笔记——String对象
  13. SftpGo:一款高性能的sftp server服务
  14. Cocos2d-x游戏开发之Lua 5.1 参考手册
  15. 2C+1A(JD6621*2PCS+FP6601A)智能退功率实现方案
  16. 【代码】HTML网站维护更新简易单页源码
  17. matlab习题及答案,MATLAB习题及答案
  18. Map--7-14 电话聊天狂人(25 分)
  19. Dubbo的版本处理错误
  20. pli测试50题题库_马士基笔试题目

热门文章

  1. Java 流式编程stream
  2. java jep_Java 10 – JEP 286:局部变量类型推断
  3. argparser_Java命令行界面(第22部分):argparser
  4. activiti dmn_端到端BPM(带有DMN标记)
  5. cloudfoundry_在Cloudfoundry上部署RESTful服务
  6. 投行数据_投行对Java的二十大核心访谈问答
  7. vue 脚手架测试环境_关于单元测试脚手架的几点思考
  8. spring体系结构_了解Spring Web应用程序体系结构:经典方法
  9. AWS:避免那些“神圣的法案”时刻的一些技巧
  10. apache camel_探索Apache Camel Core –文件组件