基于Maven构建OSGI应用(Maven和OSGI结合)

news2024/10/5 20:22:02

基于Maven构建OSGI应用。

使用Maven来构建项目,包括项目的创建、子模块buldle的创建等。使用OSGI来实现动态模块化管理,实现模块的热插拔效果(即插即用)。

创建一个Maven项目:helloworld,并在该项目下创建两个Maven 子模块:helloworld-client、helloworld-server。

创建 helloworld maven项目、填写参数及Advanced Settings:

创建 helloworld-server maven子模块:

同样的方式再创建 helloworl-client maven 子模块。

接下来就是 hellworld-server、helloworld-client 编码以及OSGI及编译打包配置。

OSGI及编译打包配置,直接通过修改3个pom文件(1个主pom、2个子模块的pom)来配置,最终配置结果如下:

1)代码结构:

client->Activator.java:

package com.xxx.osgi.helloworld.client;

import com.xxx.osgi.helloworld.server.HelloWorldImpl;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import java.util.Objects;

/**
 * @author frank
 * @date 2023/12/8
 */
public class Activator implements BundleActivator {
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("helloworld-client: start");
        System.out.println("helloworld-client: call server getHelloMsg()");

        ServiceReference<HelloWorldImpl> reference = bundleContext.getServiceReference(HelloWorldImpl.class);
        if (Objects.nonNull(reference)) {
            HelloWorldImpl service = bundleContext.getService(reference);
            if (Objects.nonNull(service)) {
                String msg = service.getHelloMsg("Frank");
                System.out.println("SUCCESS: return msg is:\n" + msg);
            } else {
                System.out.println("ERROR: service not found!");
            }
            bundleContext.ungetService(reference);
        } else {
            System.out.println("ERROR: service reference not found!");
        }
    }

    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("helloworld-client: stop");
    }
}

server->Activator.java

package com.xxx.osgi.helloworld.server;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Objects;

/**
 * @author frank
 * @date 2023/12/9
 */
public class Activator implements BundleActivator {
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("helloworld-server: start");
        HelloWorldImpl server = new HelloWorldImpl();
        Dictionary<String, Object> properties = new Hashtable<String, Object>();
        bundleContext.registerService(HelloWorldImpl.class, server, properties);
        System.out.println("helloworld-server: 服务已发布(注册)!");
    }

    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("helloworld-server: stop");
    }
}

server->IHelloWorld.java

package com.xxx.osgi.helloworld.server;

/**
 * @author frank
 * @date 2023/12/9
 */
public interface IHelloWorld {
    String getHelloMsg(String name);
}

server->HelloWorldImpl.java

package com.xxx.osgi.helloworld.server;

/**
 * @author frank
 * @date 2023/12/9
 */
public class HelloWorldImpl implements IHelloWorld {
    public String getMethodName() {
        return "[" + Thread.currentThread().getStackTrace()[2].getMethodName() + ":" +
                Thread.currentThread().getStackTrace()[2].getLineNumber() + "] ";
    }

    public String getHelloMsg(String name) {
        return getMethodName() + " HelloWorld " + name;
    }
}

2)主pom:

<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>org.xxx.osgi</groupId>
  <artifactId>helloworld</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <modules>
    <module>helloworld-client</module>
    <module>helloworld-server</module>
  </modules>
  <packaging>pom</packaging>

  <name>helloworld</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>5</maven.compiler.source>
    <parent.maven.bundle.plugin.version>2.4.0</parent.maven.bundle.plugin.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <!-- 该版本 maven 仓库找不到,如果要用该版本可以在 Project Structure->Project Settings->Modules 中设置:-->
      <!-- 设置 OSGI:General->Configure OSGI Core Library->Use Library 指定本地 jar 文件静态添加 osgi lib  -->
      <!--
      <groupId>org.eclipse</groupId>
      <artifactId>osgi</artifactId>
      <version>3.18.600.v20231110-1900</version>
      <scope>provided</scope>
      -->
      <!-- 该版本maven仓库可以找到,可以用这个版本。在 pom 中指定 osgi lib 的 dependency 依赖 -->
      <groupId>org.eclipse</groupId>
      <artifactId>osgi</artifactId>
      <version>3.10.0-v20140606-1445</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>

3)hellworld-client 子模块pom:

<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>helloworld</artifactId>
        <groupId>org.xxx.osgi</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>helloworld-client</artifactId>
    <packaging>bundle</packaging>

    <name>helloworld-client</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- osgi lib 使用主 pom 中定义的依赖,子模块不再重复定义 osgi lib dependency -->
        <dependency>
            <groupId>org.xxx.osgi</groupId>
            <artifactId>helloworld-server</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!-- osgi 打包配置,使用 maven-bundle-plugin 插件进行 osgi 打包 bundle jar -->
                <!-- 使用maven-bundle-plugin打包方式时指定manifest文件不生效,但可在 instructions 中配置 manifest 参数 -->
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>${parent.maven.bundle.plugin.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <!-- 如果要把依赖的 jar 也一起打包进去,在 Import-Package 中指定、并设置 Embed-Dependency -->
                        <!-- <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency> -->
                        <Bundle-Name>${project.name}</Bundle-Name>
                        <Bundle-SymbolicName>$(replace;${project.artifactId};-;_)</Bundle-SymbolicName>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Bundle-Activator>com.xxx.osgi.helloworld.client.Activator</Bundle-Activator>
                        <Import-Package>org.osgi.framework,com.xxx.osgi.helloworld.server;version=${project.version}"</Import-Package>
                        <Export-Package>
                            com.xxx.osgi.helloworld.client;version="${project.version}"
                        </Export-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

4)helloworld-server 子模块pom:

<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>helloworld</artifactId>
        <groupId>org.xxx.osgi</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>helloworld-server</artifactId>
    <packaging>bundle</packaging>

    <name>helloworld-server</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- osgi lib 使用主 pom 中定义的依赖,子模块不再重复定义 osgi lib dependency -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!-- osgi 打包配置,使用 maven-bundle-plugin 插件进行 osgi 打包 bundle jar -->
                <!-- 使用maven-bundle-plugin打包方式时指定manifest文件不生效,但可在 instructions 中配置 manifest 参数 -->
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>${parent.maven.bundle.plugin.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <!-- 如果要把依赖的 jar 也一起打包进去,在 Import-Package 中指定、并设置 Embed-Dependency -->
                        <!-- <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency> -->
                        <Bundle-Name>${project.name}</Bundle-Name>
                        <Bundle-SymbolicName>$(replace;${project.artifactId};-;_)</Bundle-SymbolicName>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Bundle-Activator>com.xxx.osgi.helloworld.server.Activator</Bundle-Activator>
                        <Import-Package>org.osgi.framework</Import-Package>
                        <Export-Package>
                            com.xxx.osgi.helloworld.server;version="${project.version}"
                        </Export-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

注意:

i)OSGI框架(OSGI Library)通过pom配置后自动manve刷新就可以自动在 Project Settings-> Modules 中自动生成 OSGI 配置了,包括OSGI Library也自设置了,这里无需手动修改其他OSGI配置,默认即可。根据pom自动生成的OSGI配置如下:

Configure OSGI Core Library 点击打开显示如下:

版本号就是pom中指定OSGI dependency 的版本。

ii)pom文件中配置打包插件使用:maven-bundle-plugin 插件,该插件是专门为OSGI打包提供的插件,但是它不能导出 META-INF 中的内容到 Export-Package jar 包中。也就是说使用 maven-bundle-plugin 插件打包导出的 bundle jar 包中的 manifest 只能通过 pom.xml 文件中的 maven-bundle-plugin 打包参数项来配置,不能直接指定使用自己项目中指定的 manifest 文件(指定了也不生效)。另外,Project Settings -> Modules 中的 Manifest Generation 配置也没有用。

另外,maven-bundle-plugin 打包插件支持了一个标签:
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>,
有了这个标签,可以直接把依赖的 jar 打入 bundle jar 包中去。

 5)编译打包:

mvn clean package

执行命令,就会生成目标jar文件:

6)添加 debug / run 配置,可在idea中运行或调试:

为client和server分别添加一个 debug/run 配置,Bundle name配置中添加 4个 必须依赖的系统jar和各自子模块的jar:

添加 debug/run 配置并运行后,会自动生成  out 目录:

7)拷贝生成的 client & server bundle(jar) 到OSGI环境执行:

我本地Windows配置的OSGI运行环境位于:d:\osgi\equinox\

d:> cd d:\osgi\equinox\
d:\osgi\equinox> ls 
org.eclipse.osgi_3.18.600.v20231110-1900.jar
plugins
start.bat
d:\osgi\equinox> mkdir  my_bundles
d:\osgi\equinox> cp e:\ws2\qf\helloworld\helloworld-client\target\helloworld-client-1.0.0-SNAPSHOT.jar my_bundles\helloworld-client-1.0.0-SNAPSHOT.jar
d:\osgi\equinox> cp e:\ws2\qf\helloworld\helloworld-server\target\helloworld-server-1.0.0-SNAPSHOT.jar my_bundles\helloworld-server-1.0.0-SNAPSHOT.jar

8)执行bundles:

install & start bundles,server需要先启动、再启动client:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1297484.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

[ROS2] --- service

1 service介绍 1.1 service概念 话题通信是基于订阅/发布机制的&#xff0c;无论有没有订阅者&#xff0c;发布者都会周期发布数据&#xff0c;这种模式适合持续数据的收发&#xff0c;比如传感器数据。机器人系统中还有另外一些配置性质的数据&#xff0c;并不需要周期处理&…

迅为3588开发板 sudo: 无法解析主机:/DNS配置

环境申明 RK3588 ubuntu 22.04 jammy 迅为开发板 hostname 看是否有Host .&#xff0c;如果没有&#xff0c; sudo vim /etc/hostname在里面加一行&#xff0c;我这就这一个 iTOP-RK3588hosts 修改本地hosts sudo vim /etc/hosts127.0.0.1 localhost localhost iTOP-RK3…

ai人工智能洗稿软件免费有哪些好用?【最新AI洗稿软件盘点】

在当今信息时代&#xff0c;内容创作已成为人们工作和生活中不可或缺的一部分。为了提高创作效率&#xff0c;越来越多的人转向人工智能洗稿软件。本文将专心分享一些优质的免费AI洗稿软件。 免费AI洗稿软件的崛起 免费AI洗稿软件的崛起为许多创作者带来了便利&#xff0c;使他…

贪心算法背包问题c

在背包问题中&#xff0c;贪心算法通常用来解决0-1背包问题&#xff0c;也就是每种物品都有固定数量&#xff0c;你可以选择拿或者不拿&#xff0c;但不可以拿走部分。以下是一个用C语言实现的贪心算法的例子&#xff1a; #include <stdio.h>#define MAX_N 1000#define …

卷王开启验证码后无法登陆问题解决

问题描述 使用 docker 部署&#xff0c;后台设置开启验证&#xff0c;重启服务器之后&#xff0c;docker重启&#xff0c;再次访问系统&#xff0c;验证码获取失败&#xff0c;导致无法进行验证&#xff0c;也就无法登陆系统。 如果不了解卷王的&#xff0c;可以去官网看下。…

从零开始搭建企业管理系统(三):集成 Spring Data Jpa

集成 Spring Data Jpa 什么是 Jpa什么是 Spring Data Jpa什么是 HibernateJPA、Spring Data Jpa、Hibernate 之间的关系集成 Spring Data JpaPOM 依赖配置文件UserEntity启动程序Jpa 配置Jpa 注解UserRepositoryUserServiceUserServiceImplUserControllerBaseEntity 什么是 Jpa…

.NET Core 依赖注入 Microsoft.Extensions.DependencyInjection

文章目录 前言什么是依赖注入C# 使用依赖注入框架介绍 Microsoft.Extensions.DependencyInjectionNuget安装简单单例使用打印结果 自动装配举例自动装配测试用例打印结果自动装配执行顺序测试用例有歧义构造函数渐进式构造函数循环依赖 自动装配结论 手动装配手动注入别名注入 …

ShardingSphere数据分片之分表操作

1、概述 Apache ShardingSphere 是一款分布式的数据库生态系统&#xff0c; 可以将任意数据库转换为分布式数据库&#xff0c;并通过数据分片、弹性伸缩、加密等能力对原有数据库进行增强。 Apache ShardingSphere 设计哲学为 Database Plus&#xff0c;旨在构建异构数据库上…

L1-030:一帮一

题目描述 “一帮一学习小组”是中小学中常见的学习组织方式&#xff0c;老师把学习成绩靠前的学生跟学习成绩靠后的学生排在一组。本题就请你编写程序帮助老师自动完成这个分配工作&#xff0c;即在得到全班学生的排名后&#xff0c;在当前尚未分组的学生中&#xff0c;将名次最…

使用Postman进行自动化集成测试

1 前言 笔者在使用Node开发HTTP接口的过程中&#xff0c;发现当接口数量越来越多&#xff0c;且接口之间互相依赖时&#xff0c;接口测试流程就会变得十分繁琐&#xff0c;且容易出错。那如何才能高效且全面地对接口进行测试呢&#xff1f; 通过实践&#xff0c;笔者发现可以…

程序员的职业连续性就那么重要吗?

大家好&#xff0c;我是风筝&#xff0c;微信搜「古时的风筝」&#xff0c;更多干货 年初的时候时候一个同学跟我聊天说起最近面试的经历。说投了不少简历&#xff0c;但是面试的机会不多&#xff0c;而且有的负责照片的 HR 直接跟他说&#xff1a;“你的工作连续性不达标&…

AI专题报告:AI多模态提升商业化价值,应用多点开花验证景气度

今天分享的AI系列深度研究报告&#xff1a;《AI专题报告&#xff1a;AI多模态提升商业化价值&#xff0c;应用多点开花验证景气度》。 &#xff08;报告出品方&#xff1a;太平洋证券&#xff09; 报告共计&#xff1a;21页 1 一周行情回顾 上周上证综指、深证成指、创业板指…

两个月软考-高项上岸

文章目录 前言结缘软考功亏一篑有始有终2个月计划资料部分计划截图 总结 前言 我们看小说或者电视剧电影都会看到这样的情节&#xff0c;主角一开始锦衣玉食&#xff0c;突然家道中落&#xff0c;啥都没了&#xff0c;主角再一路奋起重新找回了属于自己的一切&#xff1b;还有…

视频汇聚/音视频流媒体视频平台/视频监控EasyCVR分享页面无法播放,该如何解决?

国标GB28181安防视频监控/视频集中存储/云存储EasyCVR平台可拓展性强、视频能力灵活、部署轻快&#xff0c;可支持的主流标准协议有国标GB28181、RTSP/Onvif、RTMP等&#xff0c;以及支持厂家私有协议与SDK接入&#xff0c;包括海康Ehome、海大宇等设备的SDK等。平台既具备传统…

数据库系统概论期末经典大题讲解(范式提升、求闭包、求主码)

上一次我们介绍了数据库中关系代数查询&#xff0c;从选择、投影到连接等操作符&#xff0c;探索了数据库查询 大家可以移步我的文章&#xff1a;数据库系统概论期末经典大题讲解&#xff08;用关系代数进行查询&#xff09;-CSDN博客 今天&#xff0c;我们将继续沿着数据库系统…

111.am40刷机折腾记4-firefly镜像-dp正常显示

1. 平台&#xff1a; rk3399 am40 4g32g 2. 内核&#xff1a;firefly的内核&#xff08;整体镜像&#xff09; 版本&#xff1a; linux4.4.194 3. 交叉编译工具 &#xff1a;暂时不编译 4. 宿主机&#xff1a;ubuntu18.04 5. 需要的素材和资料&#xff1a;boot-am40-202…

14、SQL注入——HTTP文件头注入

文章目录 一、HTTP Header概述1.1 HTTP工作原理1.2 HTTP报文类型1.3 较重要的HTTP Header内容 二、HTTP Header注入2.1 HTTP Header注入的前提条件2.2 常见的HTTP Header注入类型 一、HTTP Header概述 1.1 HTTP工作原理 1.2 HTTP报文类型 &#xff08;1&#xff09;请求报文 …

CrystalDiskInfo中文版(硬盘检测工具) v9.1.1.0 绿色汉化版-供大家学习研究参考

更新内容 重新支持三星SATA SSD寿命报告 增加对ZHITAI SC001的支持 新增SK hynix Gold S31支持 增加了KLEVV NEO N610的支持。 改进的Micron/Crucial SATA SSD支持 已更改 卸载程序将显示一个确认对话框&#xff0c;用于删除设置。 强大功能 1.拥有多国语言&#xff0c;…