OSGI和Spring動態模塊–簡單的Hello World

在此姿勢中,我們將采用使用OSGi進行的第一個實現,并使用Spring Dynamic Modules改進應用程序。

Spring動態模塊(Spring Dm)使基于OSGi的應用程序的開發更加容易。 這樣,服務的部署就容易得多。 您可以像其他任何Spring bean一樣注入服務。

因此,讓我們從Spring dm開始。

首先,您需要下載Spring Dm Distribution 。 在本文中,我使用了具有依賴關系的發行版,而我將僅使用以下庫:

com.springsource.net.sf.cglib-2.1.3.jar
com.springsource.org.aopalliance-1.0.0.jar
log4j.osgi-1.2.15-SNAPSHOT.jar
com.springsource.slf4j.api-1.5.0.jar
com.springsource.slf4j.log4j-1.5.0.jar
com.springsource.slf4j.org.apache.commons.logging-1.5.0.jar
org.springframework.aop-2.5.6.SEC01.jar
org.springframework.beans-2.5.6.SEC01.jar
org.springframework.context-2.5.6.SEC01.jar
org.springframework.core-2.5.6.SEC01.jar
spring-osgi-core-1.2.1.jar
spring-osgi-extender-1.2.1.jar
spring-osgi-io-1.2.1.jar

當然,您可以將Spring 2.5.6庫替換為Spring 3.0庫。 但是對于本文而言,Spring 2.5.6就足夠了。

因此,從服務捆綁開始。 回想一下,該捆綁軟件導出了一項服務:

package com.bw.osgi.provider.able;public interface HelloWorldService {void hello();
}
package com.bw.osgi.provider.impl;import com.bw.osgi.provider.able.HelloWorldService;public class HelloWorldServiceImpl implements HelloWorldService {@Overridepublic void hello(){System.out.println("Hello World !");}
}

這里沒有要做的任何更改。 現在,我們可以看到激活器:

package com.bw.osgi.provider;import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;import com.bw.osgi.provider.able.HelloWorldService;
import com.bw.osgi.provider.impl.HelloWorldServiceImpl;public class ProviderActivator implements BundleActivator {private ServiceRegistration registration;@Overridepublic void start(BundleContext bundleContext) throws Exception {registration = bundleContext.registerService(HelloWorldService.class.getName(),new HelloWorldServiceImpl(),null);}@Overridepublic void stop(BundleContext bundleContext) throws Exception {registration.unregister();}
}

因此,在這里,我們將簡單化。 讓我們刪除這個類,它對于Spring Dm不再有用。

我們將讓Spring Dm為我們導出捆綁包。 我們將為此捆綁包創建一個Spring上下文。 我們只需要在文件夾META-INF / spring中創建一個文件provider-context.xml即可。 這是XML文件中的簡單上下文,但是我們使用新的名稱空間注冊服務“ http://www.springframework.org/schema/osgi ”。 因此,讓我們開始:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:osgi="http://www.springframework.org/schema/osgi"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/osgihttp://www.springframework.org/schema/osgi/spring-osgi.xsd"><bean id="helloWorldService" class="com.bw.osgi.provider.impl.HelloWorldServiceImpl"/><osgi:service ref="helloWorldService" interface="com.bw.osgi.provider.able.HelloWorldService"/>
</beans>

OSGi特有的唯一內容是osgi:service聲明。 此行表明我們使用接口HelloWorldService作為服務名稱將helloWorldService注冊為OSGi服務。

如果將上下文文件放在META-INF / spring文件夾中 ,Spring Extender將自動檢測到它,并創建一個應用程序上下文。

現在,我們可以轉到消費者捆綁包。 在第一階段,我們創建了該消費者:

package com.bw.osgi.consumer;import javax.swing.Timer;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import com.bw.osgi.provider.able.HelloWorldService;public class HelloWorldConsumer implements ActionListener {private final HelloWorldService service;private final Timer timer;public HelloWorldConsumer(HelloWorldService service) {super();this.service = service;timer = new Timer(1000, this);}public void startTimer(){timer.start();}public void stopTimer() {timer.stop();}@Overridepublic void actionPerformed(ActionEvent e) {service.hello();}
}

目前,這里沒有任何更改。 可以使用@Resource注釋代替構造函數的注入,但這在Spring 2.5.6和Spring Dm中不起作用(但在Spring 3.0中很好用)。

現在激活器:

package com.bw.osgi.consumer;import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;import com.bw.osgi.provider.able.HelloWorldService;public class HelloWorldActivator implements BundleActivator {private HelloWorldConsumer consumer;@Overridepublic void start(BundleContext bundleContext) throws Exception {ServiceReference reference = bundleContext.getServiceReference(HelloWorldService.class.getName());consumer = new HelloWorldConsumer((HelloWorldService) bundleContext.getService(reference));consumer.startTimer();}@Overridepublic void stop(BundleContext bundleContext) throws Exception {consumer.stopTimer();}
}

不再需要注射。 我們可以在這里保持計時器的啟動,但是再一次,我們可以使用框架的功能來啟動和停止計時器。 因此,讓我們刪除激活器并創建一個應用程序上下文以創建使用者并自動啟動它,并將其放入META-INF / spring文件夾中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:osgi="http://www.springframework.org/schema/osgi"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/osgihttp://www.springframework.org/schema/osgi/spring-osgi.xsd"><bean id="consumer" class="com.bw.osgi.consumer.HelloWorldConsumer" init-method="startTimer" destroy-method="stopTimer"lazy-init="false" ><constructor-arg ref="eventService"/></bean><osgi:reference id="eventService" interface="com.bw.osgi.provider.able.HelloWorldService"/>
</beans>

我們使用了init-method和destroy-method屬性來開始和停止框架的時間,并使用構造函數arg注入對服務的引用。 使用osgi:reference字段并使用接口作為服務的鍵來獲取對該服務的引用。

這就是我們與該捆綁包有關的全部。 比第一個版本簡單得多嗎? 除了簡化之外,您還可以看到源不依賴于OSGi或Spring Framework,這是純Java語言,這是一個很大的優勢。

Maven POM與第一階段的相同,只是我們可以減少對osgi的依賴。

提供者:

<?xml version="1.0" encoding="UTF-8"?>
<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>OSGiDmHelloWorldProvider</groupId><artifactId>OSGiDmHelloWorldProvider</artifactId><version>1.0</version><packaging>bundle</packaging><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.0.2</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><extensions>true</extensions><configuration><instructions><Bundle-SymbolicName>OSGiDmHelloWorldProvider</Bundle-SymbolicName><Export-Package>com.bw.osgi.provider.able</Export-Package><Bundle-Vendor>Baptiste Wicht</Bundle-Vendor></instructions></configuration></plugin></plugins></build> 
</project>

消費者:

<?xml version="1.0" encoding="UTF-8"?>
<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>OSGiDmHelloWorldConsumer</groupId><artifactId>OSGiDmHelloWorldConsumer</artifactId><version>1.0</version><packaging>bundle</packaging><dependencies><dependency><groupId>OSGiDmHelloWorldProvider</groupId><artifactId>OSGiDmHelloWorldProvider</artifactId><version>1.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.0.2</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><extensions>true</extensions><configuration><instructions><Bundle-SymbolicName>OSGiDmHelloWorldConsumer</Bundle-SymbolicName><Bundle-Vendor>Baptiste Wicht</Bundle-Vendor></instructions></configuration></plugin></plugins></build>
</project>

我們可以使用maven install構建兩個捆綁包。 因此,讓我們在Felix中測試我們的東西:

wichtounet@Linux-Desktop:~/Desktop/osgi/felix$ java -jar bin/felix.jar
_______________
Welcome to Apache Felix Gogog! install file:../com.springsource.slf4j.org.apache.commons.logging-1.5.0.jar
Bundle ID: 5
g! install file:../com.springsource.slf4j.log4j-1.5.0.jar
Bundle ID: 6
g! install file:../com.springsource.slf4j.api-1.5.0.jar
Bundle ID: 7
g! install file:../log4j.osgi-1.2.15-SNAPSHOT.jar
Bundle ID: 8
g! install file:../com.springsource.net.sf.cglib-2.1.3.jar
Bundle ID: 9
g! install file:../com.springsource.org.aopalliance-1.0.0.jar
Bundle ID: 10
g! install file:../org.springframework.core-2.5.6.SEC01.jar
Bundle ID: 11
g! install file:../org.springframework.context-2.5.6.SEC01.jar
Bundle ID: 12
g! install file:../org.springframework.beans-2.5.6.SEC01.jar
Bundle ID: 13
g! install file:../org.springframework.aop-2.5.6.SEC01.jar
Bundle ID: 14
g! install file:../spring-osgi-extender-1.2.1.jar
Bundle ID: 15
g! install file:../spring-osgi-core-1.2.1.jar
Bundle ID: 16
g! install file:../spring-osgi-io-1.2.1.jar
Bundle ID: 17
g! start 5 7 8 9 10 11 12 13 14 15 16 17
log4j:WARN No appenders could be found for logger (org.springframework.osgi.extender.internal.activator.ContextLoaderListener).
log4j:WARN Please initialize the log4j system properly.
g! install file:../OSGiDmHelloWorldProvider-1.0.jar
Bundle ID: 18
g! install file:../OSGiDmHelloWorldConsumer-1.0.jar
Bundle ID: 19
g! start 18
g! start 19
g! Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
stop 19
g!

如您所見,它運行完美!

總之,Spring Dm確實使使用OSGi的開發更加容易。 使用Spring Dm,您還可以啟動捆綁包。 它還使您可以制作Web捆綁包并輕松使用OSGi綱要的服務。

以下是這兩個項目的來源:

  • OSGiDmHelloWorldProvider來源
  • OSGiDmHelloWorldConsumer來源

這是兩個內置的Jars:

  • OSGiDmHelloWorldProvider-1.0.jar
  • OSGiDmHelloWorldConsumer-1.0.jar

這是完整的文件夾,包括Felix和Spring Dm: osgi-hello-world.tar.gz

參考: OSGI和Spring動態模塊–來自我們的JCG合作伙伴 Baptiste Wicht的 @ @Blog(“ Baptiste Wicht”)的 Simple Hello World 。

相關文章 :
  • OSGi –帶有服務的簡單Hello World
  • OSGi將Maven與Equinox結合使用
  • 真正的模塊化Web應用程序:為什么沒有開發標準?
  • Java模塊化方法–模塊,模塊,模塊

翻譯自: https://www.javacodegeeks.com/2011/11/osgi-and-spring-dynamic-modules-simple.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/374333.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/374333.shtml
英文地址,請注明出處:http://en.pswp.cn/news/374333.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C語言代碼規范(五)函數參數個數

一個函數的參數的數目過多&#xff08;尤其是超過8個&#xff09;顯然是一種不可取的編程風格。參數的數目直接影響調用函數的速度&#xff0c;參數越多&#xff0c;調用函數越慢。 參數的數目少&#xff0c;程序就顯得精練、簡潔&#xff0c;這有助于檢查和發現程序中的錯誤。…

vijos P1740 聰明的質檢員

題目鏈接:傳送門 題目大意:給你n個物品&#xff0c;每件物品有重量 W 和價值 V&#xff0c;給m個區間&#xff0c;和一個標準值。(n,m最大200000) 要求找到一個值x&#xff0c;使得m個所有區間的權值和與標準值的差的絕對值最小。單個區間權值計算公式(數目num0&#xff0c;價值…

為什么有的開關電源需要加自舉電容?

一、什么是自舉電路&#xff1f; 1.1 自舉的概念 首先&#xff0c;自舉電路也叫升壓電路&#xff0c;是利用自舉升壓二極管&#xff0c;自舉升壓電容等電子元件&#xff0c;使電容放電電壓和電源電壓疊加&#xff0c;從而使電壓升高。有的電路升高的電壓能達到數倍電源電壓。…

VS2010報錯 error:LINK1123:轉換到COF期間失敗,文件無限或損壞

右鍵工程-配置屬性-清單工具-輸入和輸出&#xff0c;嵌入清單一項重新選擇為否&#xff0c;如下圖 修改后重新生成和運行&#xff0c;發現程序正常運行了。

springboot 整合mybatis_SpringBoot整合Mybatis、MybatisPuls

文末視頻講解SpringBoot的版本是2.2.0一、整合Mybatis1-1、引入pom文件<dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>8.0.19version> dependency> <dependency> &l…

iOS 開發中遇到的問題

1. 關于糾結很久的KVO崩潰問題&#xff0c;其真正原因是&#xff0c;在刪除roomItem的KVO之前,將這個對象已經賦值為nil,所以實際上并沒有刪除他的observer&#xff0c;因此而崩潰&#xff1b;長時間糾結的原因是受.cxx_destruct影響了思路 2.拷貝block 因為block變量默認是聲明…

為舊版代碼創建存根–測試技術6

任何閱讀此博客的人都可能已經意識到&#xff0c;目前我正在開發一個包含大量舊代碼的項目&#xff0c;這些舊代碼龐大&#xff0c;擴展且編寫時從未進行過任何測試。 在使用此遺留代碼時&#xff0c;有一個行為異常的類非常普遍&#xff0c;整個團隊都一次又一次地犯錯。 為了…

C學習雜記(一)常見誤會

一、sizeof是關鍵字&#xff0c;不是函數。 二、strlen是函數。

python性能解決_我們如何發現并解決Python代碼中性能下降的問題

Python部落(python.freelycode.com)組織翻譯&#xff0c;禁止轉載&#xff0c;歡迎轉發。 作者&#xff1a;Omer Lachish 最近&#xff0c;我們已經開始使用RQ庫代替Celery庫作為我們的任務運行引擎。第一階段&#xff0c;我們只遷移了那些不直接進行查詢工作的任務。這些任務包…

easyui $.parser.parse 頁面重新渲染

一些dom元素是動態拼接上的easui的樣式&#xff0c;由于頁面已經渲染過了&#xff0c;所以需要手動執行渲染某個部件或者整個頁面 $.parser.parse(); // parse all the page $.parser.parse(#cc); // parse the specified node $.parser.parse($("#grid").parent());…

Java EE6裝飾器:在注入時裝飾類

軟件中常見的設計模式是裝飾器模式 。 我們上一堂課&#xff0c;然后在它周圍包裝另一堂課。 這樣&#xff0c;當我們調用類時&#xff0c;我們總是在到達內部類之前經過周圍的類。 Java EE 6允許我們通過CDI創建裝飾器&#xff0c;作為其AOP功能的一部分。 如果我們想實現仍然…

C語言代碼規范(六)浮點型變量邏輯比較

無論是float還是double類型的變量&#xff0c;都有精度限制。所以一定要避免將浮點變量用""或"!"與數字比較&#xff0c;應該設法轉化成為">"或"<"形式。 不建議使用的例子&#xff1a; if(0.0 x) if(0.0 ! x) 強烈推薦的例…

圖靈機器人調用數據恢復_機器人也能撩妹?python程序員自制微信機器人,替他俘獲女神芳心...

機器人也有感情還記得王傳君飾演的《星語心愿之再愛》這部電影嗎&#xff1f;王傳君飾演的天才程序員“王鵬鵬”因工作原因不能陪伴照顧身在異地的女朋友“林亦男”&#xff0c;呆萌宅男“王鵬鵬”開發出一款以自己為原型的“王鵬鵬8.0”程序去陪伴異地戀的女友&#xff0c;后來…

Spark排錯與優化

一. 運維 1. Master掛掉,standby重啟也失效 Master默認使用512M內存&#xff0c;當集群中運行的任務特別多時&#xff0c;就會掛掉&#xff0c;原因是master會讀取每個task的event log日志去生成spark ui&#xff0c;內存不足自然會OOM&#xff0c;可以在master的運行日志中看到…

在MySQL上使用帶密碼的GlassFish JDBC安全性

我在該博客上最成功的文章之一是有關在GlassFish上使用基于表單的身份驗證來建立JDBC安全領域的文章 。 對這篇文章的一些評論使我意識到&#xff0c;要真正使它安全&#xff0c;應該做的還很多。 開箱即用的安全性 圖片&#xff1a; TheKenChan &#xff08; CC BY-NC 2.0 &a…

mgo寫入安全機制

mgo寫入安全機制 mongo寫入安全mgo寫入安全mongo寫入安全 mongo本身也有一整套的寫入安全機制,但是在這篇的內容里只介紹一小部分相關部分.先放一個鏈接可以跳過本節不看直接看這個 鏈接. WriteConcern.NONE:沒有異常拋出WriteConcern.NORMAL:僅拋出網絡錯誤異常&#xff0c;沒…

C學習雜記(二)筆試題:不使用任何中間變量如何將a、b的值進行交換

常見的方法如下 void swap1(int *a, int *b) {int temp *a;*a *b;*b temp; } 不使用中間變量的方法 void swap2(int *a, int *b) {*a *a *b;*b *a - *b;*a *a - *b; } 這種方法是不可取的&#xff0c;因為ab和a-b的運算可能會導致數據溢出。 void swap3(int *a, in…

利用python進行數據分析_利用python進行數據分析復現(1)

&#xfeff;一直以來&#xff0c;都想學習python數據分析相關的知識&#xff0c;總是拖拖拉拉&#xff0c;包括這次這個分享也是。《利用python進行數據分析 第2版》是一次無意之間在簡書上看到的一個分享&#xff0c;我決定將很詳細。一直都想著可以復現一下。但總有理由&…

在運行時交換出Spring Bean配置

如今&#xff0c;大多數Java開發人員都定期與Spring打交道&#xff0c;而我們當中的許多人已經熟悉了Spring的功能和局限性。 最近&#xff0c;我遇到了一個我從未遇到過的問題&#xff1a;引入了基于運行時引入的配置來重新連接Bean內部的功能。 這對于簡單的配置更改或交換掉…

Proximal Algorithms--Accelerated proximal gradient method

4.3 Accelerated proximal gradient method&#xff1a; 加速近端梯度方法&#xff1a; 基本的近端梯度方法的所謂的“加速”版本&#xff0c;就是在算法中包含了一個外推(extrapolation)步驟&#xff0c;一個簡單的版本是&#xff1a; yk1:xkωk(xk?xk?1)xk1:proxλkg(yk1?…