宽屏网站欣赏,长沙微网站建设,深圳建设企业,进入微信官方网站下载很长时间以来#xff0c;我一直在努力理解OSGi的真正含义。 它已经存在很长时间了#xff0c;但是没有多少人意识到这一点。 人们已经大肆宣传它是一种非常复杂的技术。 这是我为所有Java开发人员简化的尝试。 简而言之#xff0c; OSGi是一组规范#xff0c;这些规范允许对… 很长时间以来我一直在努力理解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类加载器能够根据此信息找到正确的包/罐。 在我之前的示例中如果库libXlibY和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.xml 。 pom.xml还添加了2个配置文件以便创建另外2个新模块 MathService和MathServiceClient 本文稍后将对此进行说明。 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.linkwithweb.osgi/groupIdartifactIdHelloWorld/artifactIdversion0.0.1-SNAPSHOT/versionnameHelloWorld/namedependenciesdependencygroupIdorg.osgi/groupIdartifactIdorg.osgi.core/artifactIdversion4.2.0/version/dependency/dependenciesbuildfinalNameHelloWorld-${version}/finalNamepluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion2.3.1/versionconfigurationsource1.5/sourcetarget1.5/target/configuration/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-jar-plugin/artifactIdconfigurationarchivemanifestFilesrc/main/resources/META-INF/MANIFEST.MF/manifestFile/archive/configuration/plugin/plugins/buildprofilesprofileidMathService/idbuildfinalNameMathService-${version}/finalNamepluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion2.3.1/versionconfigurationsource1.5/sourcetarget1.5/target/configuration/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-jar-plugin/artifactIdconfigurationexcludesexclude**/*.xml/excludeexclude**/*.bsh/excludeexclude**/*.properties/exclude/excludesarchivemanifestFilesrc/main/resources/MathService/META-INF/MANIFEST.MF/manifestFile/archive/configuration/plugin/plugins/build/profileprofileidMathServiceClient/idbuildfinalNameMathServiceClient-${version}/finalNamepluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion2.3.1/versionconfigurationsource1.5/sourcetarget1.5/target/configuration/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-jar-plugin/artifactIdconfigurationexcludesexclude**/*.xml/excludeexclude**/*.bsh/excludeexclude**/*.properties/exclude/excludesarchivemanifestFilesrc/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 ab;}} 接下来是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和EquinoxHowTo 。 相关文章 Java Code Geeks Andygene Web原型 SpringQuartz和JavaMail集成教程 使用Spring将POJO公开为JMX MBean 依赖注入–手动方式 翻译自: https://www.javacodegeeks.com/2011/06/osgi-using-maven-equinox.html