osgi實戰學習之路:3. osgi分層概念及相互合作demo

來源:互聯網
上載者:User

原始碼下載
分層:


modual:

主要作用於包級管理與共用代碼

lifecycle:

主要作用於運行期間的模組管理與訪問osgi底層架構

service:

主要作用於多模組之間的相互連信

demo:

hello-provider/pom.xml

<?xml version="1.0"?><projectxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>com.demo</groupId><artifactId>pom</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>hello-provider</artifactId><packaging>bundle</packaging><name>hello-provider</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><dependency><groupId>org.osgi</groupId><artifactId>org.osgi.core</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><configuration><instructions><Export-Package>org.hello.provider</Export-Package><Import-Package>org.osgi.framework</Import-Package><Bundle-Activator>org.hello.provider.impl.UserServiceActivator</Bundle-Activator><Private-Package>org.hello.*</Private-Package></instructions></configuration></plugin></plugins></build></project>

hello-provider/BundleActivator:

package org.hello.provider.impl;import org.hello.provider.IUserService;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class UserServiceActivator implements BundleActivator {public void start(BundleContext context) throws Exception {System.out.println("registerService......");context.registerService(IUserService.class.getName(), new UserService(), null);}public void stop(BundleContext context) throws Exception {}}


hello-client/pom.xml:


<?xml version="1.0"?><projectxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>com.demo</groupId><artifactId>pom</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>hello-client</artifactId><packaging>bundle</packaging><name>hello-client</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><dependency><groupId>org.osgi</groupId><artifactId>org.osgi.core</artifactId></dependency><dependency><groupId>com.demo</groupId><artifactId>hello-provider</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><extensions>true</extensions><configuration><instructions><Import-Package>org.hello.provider,org.osgi.framework</Import-Package><Bundle-Activator>org.hello.client.Client</Bundle-Activator><Private-Package>com.demo.hello.*</Private-Package></instructions></configuration></plugin></plugins></build></project>


hello-client/BundleActivator:

package org.hello.client;import org.hello.provider.IUserService;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceReference;public class Client implements BundleActivator {public void start(BundleContext context) throws Exception {ServiceReference    reference=context.getServiceReference(IUserService.class.getName());System.out.println(((IUserService)context.getService(reference)).add());}public void stop(BundleContext context) throws Exception {}}


將bundle安裝到本地倉庫且部署到karaf(參考前一篇)
啟動bundle

通過以下命令查看bundle的id

list

通過以下命令,啟動bundle

bundle:start 78

參考樣本






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.