maven osgi_OSGi将Maven与Equinox结合使用
maven osgi
很長時間以來,我一直在努力理解OSGi的真正含義。 它已經(jīng)存在很長時間了,但是沒有多少人意識到這一點(diǎn)。 它被炒作是一種非常復(fù)雜的技術(shù)。 這是我為所有Java開發(fā)人員簡化的嘗試。簡而言之, OSGi是一組規(guī)范,這些規(guī)范支持模塊化組裝使用Java技術(shù)構(gòu)建的軟件。 它定義了組件-服務(wù)的創(chuàng)建和注冊(在啟用OSGi的容器中),以便進(jìn)行內(nèi)部通信。 OSGi的另一個好處是,所有這些服務(wù)都可以在運(yùn)行時安裝/卸載/啟動/停止(即可以在運(yùn)行時熱部署代碼)。 與在諸如WebSphere , JBoss , WebLogic的流行J2EE應(yīng)用程序服務(wù)器中找到的Tomcat和EJB容器之類的Servlet容器實(shí)現(xiàn)類似, OSGi也有一些流行的容器實(shí)現(xiàn),例如Equinox (這是Eclipse的基礎(chǔ)), Apache Felix …等
面向服務(wù)的方法是OSGi的強(qiáng)項(xiàng)之一,但是當(dāng)您不得不處理帶有大量依賴關(guān)系的應(yīng)用程序時,我覺得這很重要。 OSGi解決了“地獄”的問題。
一個例子。 假設(shè)您在應(yīng)用程序中使用兩個庫libX和libY。 我們還假設(shè)它們每個在libZ中都有一個依賴關(guān)系,但是版本不同。 libX取決于libZ 2.0,而libY取決于libZ 1.0
如果libZ 2.0與libZ 1.0不兼容,則在同一應(yīng)用程序中同時使用它們時,可能會遇到難以解決的問題。 OSGi可以處理此類問題。 OSGi支持Import-Package指令,該指令可用于為應(yīng)用程序-服務(wù)應(yīng)使用的任何Java包指定一個版本。 OSGi類加載器能夠根據(jù)此信息找到正確的包/罐。 在我之前的示例中,如果庫libX,libY和libZ與OSGi兼容,則可以將它們?nèi)考虞d到同一JVM中而不會出現(xiàn)問題: 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應(yīng)用程序帶來了更強(qiáng)大的模塊化概念。 在捆綁包之外只能使用使用Export-Package指令導(dǎo)出的包。
在本文中,我將解釋使用Eclipse Equinox容器的OSGi 。 在其計(jì)算機(jī)上安裝了Eclipse IDE的任何人,也在Eclipse插件的文件夾中也安裝了OSGi容器。
OSGi容器jar文件的名稱類似于org.eclipse.osgi_ <version> .jar
您可以像這樣啟動OSGi
java -jar org.eclipse.osgi_3.5.2.R35x_v20100126.jar -console附件是我如何啟動OSGi容器的示例屏幕截圖(類似于啟動Tomcat )
現(xiàn)在,我們已經(jīng)啟動了OSGi容器,讓我們使用Maven創(chuàng)建一個“ HelloWorld” OSGi應(yīng)用程序。 項(xiàng)目結(jié)構(gòu)如下所示:
以下是該項(xiàng)目的pom.xml 。 pom.xml還添加了2個配置文件,以便創(chuàng)建2個新模塊( MathService和MathServiceClient ),本文稍后將對此進(jìn)行說明。
<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>如果仔細(xì)觀察pom.xml,您將看到我們創(chuàng)建的每個OSGi捆綁包都有3個MANIFEST.MF定義。 這么說,讓我解釋一下OSGi捆綁包是什么。 OSGi軟件包本質(zhì)上與標(biāo)準(zhǔn)Java“ jar”文件相同,但其特定配置在“ jar的”清單文件中定義。 OSGi容器讀取“罐子”清單文件中所有OSGi特定的條目,以激活捆綁包。 那不是很酷嗎? 使用OSGi,我們避免像其他框架一樣學(xué)習(xí)任何新的元數(shù)據(jù)格式!
這是我為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特定的。 這些條目定義了如何激活捆綁軟件,捆綁軟件的名稱和版本,其所有從屬庫以及暴露給其他服務(wù)使用的擴(kuò)展點(diǎn)。
讓我向您展示如何將“ 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要構(gòu)建捆綁包,請運(yùn)行“ mvn clean package”
它將在您的Maven項(xiàng)目的目標(biāo)文件夾中創(chuàng)建HelloWorld-0.0.1-SNAPSHOT.jar 。 這是顯示如何在Equinox中安裝和啟動“ HelloWorld”捆綁軟件的圖像
如您所見,我們使用install命令安裝捆綁軟件,并使用start命令,使用捆綁軟件安裝后容器返回的捆綁軟件ID啟動捆綁軟件。
現(xiàn)在,就捆綁軟件的生命周期而言,啟動捆綁軟件將觸發(fā)對捆綁軟件的Activator類的“ start ”方法的調(diào)用,而停止捆綁軟件將觸發(fā)對捆綁軟件的Activator類的“ stop ”方法的調(diào)用。 我們可以在容器的終端中看到上述行為的結(jié)果,該終端在模塊啟動時顯示“ Hello World”消息!
恭喜,您已經(jīng)了解了OSGi的基礎(chǔ)知識,并且已經(jīng)部署了第一個捆綁軟件!
曝光和消費(fèi)服務(wù)
為了解釋這一點(diǎn),我將實(shí)現(xiàn)一個非常簡單的示例,在該示例中,我將發(fā)布一個可以添加兩個數(shù)字的服務(wù)。
首先,我們需要定義一個接口,以向外部捆綁包(客戶端)公開“添加”功能
package com.linkwithweb.osgi.service;/*** @author Ashwin Kumar**/ public interface MathService {/*** @param a* @param b* @return*/public int add(int a, int b); }現(xiàn)在執(zhí)行類
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類,該類將“添加”服務(wù)注冊到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");} }以下是“添加”服務(wù)包的清單文件。
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如果您觀察上面的清單,您會注意到我們正在導(dǎo)出一些軟件包,以便以后使用。 同樣,必須在此處定義所有運(yùn)行時所需的軟件包(使用Import-Package指令)。
就像本文的上一節(jié)一樣,使用以下命令來構(gòu)建jar文件
mvn -PMathService軟件包
您可以在下面看到安裝和啟動OSGi捆綁軟件的命令。
以下是“添加”服務(wù)的使用者的實(shí)現(xiàn)。 消費(fèi)者打包在OSGi捆綁激活器類中,僅用于演示目的。 您可以自由地將使用者作為單獨(dú)的OSGi服務(wù)實(shí)施,因?yàn)镺SGi服務(wù)可以相互通信!
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));} }跟隨是“添加”服務(wù)使用者捆綁包的清單文件。
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要創(chuàng)建,安裝并啟動“添加”服務(wù)客戶端捆綁包,請遵循以下步驟:
mvn -PMathServiceClient軟件包
就這樣! 希望你喜歡它!
您可以在此處下載本文的源代碼
參考:來自Felicitas和Beatitudo博客的 JCG合作伙伴 Aswin的OSGI,適合初學(xué)者使用Maven和Equinox(HowTo) 。
相關(guān)文章 :- Java Code Geeks Andygene Web原型
- Spring,Quartz和JavaMail集成教程
- 使用Spring將POJO公開為JMX MBean
- 依賴注入–手動方式
翻譯自: https://www.javacodegeeks.com/2011/06/osgi-using-maven-equinox.html
maven osgi
總結(jié)
以上是生活随笔為你收集整理的maven osgi_OSGi将Maven与Equinox结合使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zorro佐罗软件安装教程及一键新机使用
- 下一篇: SonarQube中的Maven项目的单