1、创建项目

创建一个maven项目,并在该项目下创建两个maven module,一个为server,令一个为client

server为发布服务的bundle,client为使用服务的bundle

2、代码实现

与之前的helloworld一样,写代码

server中定义接口,接口的实现类,实现BundleActivator的类(发布服务)

client中,实现BundleActivator的类(使用服务)

结构如下:

1、server模块

IHelloWorld接口

public interface IHelloWorld {String sayHello(String name);
}

实现接口HelloWorldImp

public class HelloWorldImp implements IHelloWorld {@Overridepublic String sayHello(String name) {return "你好" + name;}
}

HelloWorldBundle(实现BundleActivator的类,为该bundle的启动类)

public class HelloWorldBundle implements BundleActivator{@Overridepublic void start(BundleContext bundleContext) throws Exception {System.out.println("HelloWorldBundle");IHelloWorld server = new HelloWorldImp();Dictionary<String, Object> properties = new Hashtable<>();bundleContext.registerService(IHelloWorld.class, server, properties);System.out.println("发布服务");}@Overridepublic void stop(BundleContext bundleContext) throws Exception {}
}

2、client模块

ClinetBundle(该bundle的启动类)

public class ClinetBundle implements BundleActivator {@Overridepublic void start(BundleContext bundleContext) throws Exception {System.out.println("Client");ServiceReference<IHelloWorld> reference = bundleContext.getServiceReference(IHelloWorld.class);System.out.println("reference-------" + reference);if (Objects.nonNull(reference)){IHelloWorld server = bundleContext.getService(reference);if (Objects.nonNull(server)){System.out.println("收到服务");System.out.println(server.sayHello("zsy"));}bundleContext.ungetService(reference);}}@Overridepublic void stop(BundleContext bundleContext) throws Exception {}
}

3、pom.xml配置

1、主pom

<?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>osgi_maven</groupId><artifactId>osgi_maven</artifactId><packaging>pom</packaging><version>1.0.0</version><modules><module>server</module><module>client</module></modules><name>osgi_maven</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><dependency><groupId>osgi_maven</groupId><artifactId>server</artifactId><version>${project.version}</version></dependency><dependency><groupId>org.eclipse</groupId><artifactId>osgi</artifactId><version>3.9.1-v20130814-1242</version><scope>provided</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies></dependencyManagement></project>

2、server模块pom.xml

注意点:<packaging>bundle</packaging>这个配置一定要存在,否则maven-bundle-plugin打包时,configuration中的配置信息不会打到MANIFEST文件中,导致bundle安装后,start不会启动该bundle

<?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"><parent><artifactId>osgi_maven</artifactId><groupId>osgi_maven</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>server</artifactId><packaging>bundle</packaging><name>server</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.eclipse</groupId><artifactId>osgi</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><version>2.4.0</version><extensions>true</extensions><configuration><instructions><Bundle-Version>${project.version}</Bundle-Version><Bundle-SymbolicName>$(replace;${project.artifactId};-;_)</Bundle-SymbolicName><Export-Package>oage.demo.server;version="${project.version}"</Export-Package><Import-Package>org.osgi.framework;version="${project.version}"</Import-Package><Bundle-Activator>oage.demo.server.HelloWorldBundle</Bundle-Activator></instructions></configuration></plugin></plugins></build></project>

3、client模块的pom.xml

注意点:client模块依赖了server模块中的IHelloWorld接口,在打包的时候在configuration中的instructions中要添加<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>,这样该依赖才会打入到包中,启动时才能找到依赖,否则后报错

<?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"><parent><artifactId>osgi_maven</artifactId><groupId>osgi_maven</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>client</artifactId><packaging>bundle</packaging><name>client</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.eclipse</groupId><artifactId>osgi</artifactId></dependency><dependency><groupId>osgi_maven</groupId><artifactId>server</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><version>2.4.0</version><extensions>true</extensions><configuration><instructions><Embed-Dependency>*;scope=compile|runtime</Embed-Dependency><Bundle-Version>${project.version}</Bundle-Version><Bundle-SymbolicName>$(replace;${project.artifactId};-;_)</Bundle-SymbolicName><Export-Package>osgi.demo.client;version="${project.version}"</Export-Package><Import-Package>org.osgi.framework,oage.demo.server;version="${project.version}"</Import-Package><Bundle-Activator>osgi.demo.client.ClinetBundle</Bundle-Activator></instructions></configuration></plugin></plugins></build>
</project>

4、打包Bundle

使用命令mvn clean package,打包,看到如下输出,及打包成功。在模块下会生成一个target文件夹,该文件夹中会存在已打包好的jar包

5、启动felix,并安装运行bundle

下载felix,并解压,目录结构如下(无plugins目录),新建plugins目录,将打好的两个包放到plugins目录下。

在该层使用shell命令java -jar ./bin/felix.jar,将felix运行起来,看到如下情况,即运行成功。

使用 install file:plugins/文件名 安装bundle

start启动bundle,要先启动server,后启动client,因为server为发布服务,如果后启动client找不到可用服务

OSGI和Maven结合相关推荐

  1. OSGi将Maven与Equinox结合使用

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

  2. osgi简介_OSGi:简介

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

  3. vaadin_Vaadin附加组件和Maven

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

  4. ivy maven_将Maven与Ivy集成

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

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

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

  6. Vaadin附加组件和Maven

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

  7. 将Maven与Ivy集成

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

  8. OSGi –带有服务的简单Hello World

    在本文中,我们将使用OSGi开发一个简单的Hello World应用程序. 我们将使用Felix作为OSGi容器 . 在下一篇文章中,我们将继续使用该应用程序,并使用Spring Dynamic Mo ...

  9. OSGI和Spring动态模块–简单的Hello World

    在此姿势中,我们将采用使用OSGi进行的第一个实现,并使用Spring Dynamic Modules改进应用程序. Spring动态模块(Spring Dm)使基于OSGi的应用程序的开发更加容易. ...

最新文章

  1. 【数据结构】图的深度优先遍历 广度优先遍历
  2. Oracle的括号转义字符,SQL中通配符、转义符与括号的使用
  3. 删除sybase里面的锁_一起来学习分布式锁
  4. linux c basename dirname函数 从路径得到 文件名 目录名
  5. singleton模式四种线程安全的实现
  6. Spring.NET学习笔记(4)-对象作用域和类型转换
  7. 2021牛客暑期多校训练营1 A.Alice and Bob 博弈 SG函数
  8. 【TensorFlow】常用的损失函数及其TensorFlow实现
  9. C语言输出空格逐级递减,2021年计算机2级c语言笔试部分-20210514115908.doc-原创力文档...
  10. 超图Cesium二三维切换
  11. 移动应用开发专业简介(610212)
  12. JSTL 核心标签库标签共有13个
  13. 利用Jsoup解析HTML
  14. SonicWall宣布推出全新渠道伙伴计划,为中小型企业的网络安全提供可靠防护
  15. java 其他文件转pdf_java 其他文件转成pdf java生成pdf
  16. 手机中.android_secure文件夹中的文件能删除吗,安卓手机里的各“文件夹”都是什么?能删吗?-手机相册在哪个文件夹...
  17. 使用第三方SDK进行网页授权
  18. AT指令表(中文详解)
  19. 快速升级Anaconda中的Spyder
  20. 旧款 mac 电脑重装 OS X Lion 10.7.5 系统的 U 盘安装盘制作及遇到问题和解决方法

热门文章

  1. readlink() 函数
  2. 怎样把视频压缩小一点?这些软件使用技巧你不能不知道
  3. input 输入框限制只能输入两位有效小数
  4. 实施交付项目经理的记录-金融版
  5. 金融科技领先者Broadridge选择CloudBees CI来加速软件交付
  6. app安全测试怎么测?
  7. ant design pro vue 动态路由 流程详解
  8. 牛客国庆集训派对Day6 I 清明梦超能力者黄YY
  9. 数据库-行存储及列存储区别
  10. MySQL(复合查询)