Intellij Idea 15中开发Maven+osgi项目(Apache felix环境)
ccwgpt 2024-10-01 08:00 33 浏览 0 评论
1.Intellij Idea创建Maven项目
利用maven模板快速创建maven项目,如图操作,填写g(groupid)a(artifactid)v(version)。
maven home directory 选择系统默认或者自己下载下来的maven主目录。
2.将项目模块化
利用maven的依赖以及集成的特性,将项目模块化。
在主module下创建module并继承主module。
这里我创建了server以及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"> <modelVersion>4.0.0</modelVersion> <groupId>com.renming.osgi.helloworld</groupId> <artifactId>helloworld</artifactId> <version>1.0.0</version> <modules> <module>server</module> <module>client</module> </modules> <packaging>pom</packaging> <name>helloworld</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.renming.osgi.helloworld</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>
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>com.renming.osgi.helloworld</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> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </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> com.renming.osgi.helloworld.server.inter;version="${project.version}" </Export-Package> <Import-Package> org.osgi.framework </Import-Package> <Bundle-Activator> com.renming.osgi.helloworld.Activator </Bundle-Activator> </instructions> </configuration> </plugin> </plugins> </build> </project>
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>com.renming.osgi.helloworld</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>com.renming.osgi.helloworld</groupId> <artifactId>server</artifactId> </dependency> <dependency> <groupId>org.eclipse</groupId> <artifactId>osgi</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </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> <Import-Package> org.osgi.framework,com.renming.osgi.helloworld.server.inter;version="${project.version}" </Import-Package> <Bundle-Activator> com.renming.osgi.helloworld.Activator </Bundle-Activator> </instructions> </configuration> </plugin> </plugins> </build> </project>
主pom和子pom拥有继承关系,同时进行了模块化。
3.编写简单的测试用例
通过实现BundleActivator接口,可以实现与osgi框架环境的通信。
这里需要实现start以及stop,分别在bundle启动以及终止的时候被调用。
这里让它在server bundle启动的时候注册一个简单的服务:
package com.renming.osgi.helloworld; import java.util.ArrayList; import java.util.List; import com.renming.osgi.helloworld.server.impl.HelloImpl; import com.renming.osgi.helloworld.server.inter.Hello; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; public class Activator implements BundleActivator { private List<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>(); private static BundleContext context; static BundleContext getContext() { return context; } public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; System.out.println("----------------hello start---------------------"); //注册hello接口中的服务 registrations.add(bundleContext .registerService(Hello.class.getName(), new HelloImpl("Hello, OSGi"), null)); System.out.println("----------------hello start---------------------"); } public void stop(BundleContext bundleContext) throws Exception { Activator.context = null; for (ServiceRegistration registration : registrations) { System.out.println("unregistering: " + registration); registration.unregister(); } } }
需要注意的是编写完代码后,需要配置server模块pom中打包插件,完整的上面已经列出了。
然后是cilent模块中测试代码,需要从bundleContext中获取到对应的服务,只需要提供服务名就可以了。具体代码如下:
package com.renming.osgi.helloworld; import com.renming.osgi.helloworld.server.inter.Hello; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; public class Activator implements BundleActivator { public void start(BundleContext ctx) { System.out.println("----------------hello client start---------------------"); ServiceReference ref = ctx.getServiceReference(Hello.class.getName()); if (ref != null) { Hello hello = null; try { hello = (Hello) ctx.getService(ref); if (hello != null) hello.sayHello(); else System.out.println("Service:Hello---object null"); } catch (RuntimeException e) { e.printStackTrace(); } finally { ctx.ungetService(ref); hello = null; } } else { System.out.println("Service:Hello---not exists"); } System.out.println("----------------hello client start---------------------"); } public void stop(BundleContext ctx) throws Exception { } }
4.配置Apache felix环境
下载:http://felix.apache.org/downloads.cgi
解压之后拷贝bin、conf、bundle目录到主目录下方便执行以及修改配置,同时创建plugins文件夹用于存放我们编写的bundle。
最后目录结构如下:
---执行bin中felix.jar
---控制台显示g!无报错信息则执行成功
---执行命令lb,可以查看已启动bundle
---然后打包client模块以及server模块,并将打包的jar包拷贝到plugins中
---执行命令 install file:plugins/XXX.jar,会显示ID,然后执行start ID,最后执行lb查看是否启动成功,并打印对应的信息
由于client以及server都是在本地调用,如果需要远程调用可以参考这篇文章:
http://www.cnblogs.com/lw900320/archive/2012/06/26/2563221.html
相关推荐
- 软件开发接口通信总结(软件开发 接口)
-
常用的接口通信开发:1.http协议通信:主要通过post,get方式提交,通信较耗时,至少几百毫秒,但是如果采用线程池做,在初次建立连接时,耗时,建立连接后,通信挺快的,十几毫秒可以搞定。httpC...
- 《哪吒2》中物理知识很多,但无量仙翁喝尿的解释又完美又搞笑~
-
随着《哪吒2》票房的不断攀升,导演饺子背后的故事不断被挖掘大家发现,学历高的导演就是不一样,高考600多分的导演拍出来的作品里面全是知识点啊,尤其是包含了很多物理知识点。大哪吒的头发为什么是向上的?无...
- 用毕加索风格打造动画版“头号玩家”
-
提香·韦切利奥的《乌尔比诺的维纳斯》。桑德罗·波提切利的《维纳斯的诞生》。毕加索的《手里捧着书的女子》。安迪·沃霍尔的《双面猫王》。凡·高的《邮差约瑟鲁林》。马奈的《奥林匹亚》。巴齐耶的《雷诺阿画像》...
- 《章鱼噼的原罪》开播即好评 诡谲美学何时能突破电视框架?
-
由「タイザン5」创作的短篇漫画《章鱼噼的原罪》,自2021年底在少年Jump+连载以来,仅用上下两集单行本的体量,便以「幸福表象下的惊悚内核」引爆话题。作品中扭曲的友情叙事与冲击性画面形成强烈反差,单...
- 妖猫睡猫招财猫,都是怎么“化人”的?
-
陆颖瑶江户时期的猫常常被当作浮世绘的题材,最初是在美人画的角落里作为美人的可爱宠物而登场,后来被画家拟人化,成为役者画、玩具画等的创作对象,澎湃新闻获悉,日本大阪历史博物馆即将举办“国芳、广重、国贞...
- 俄媒:俄空投“章鱼”反坦克炮测试坚固性
-
参考消息网9月15日报道据俄罗斯卫星社莫斯科9月14日报道,俄罗斯国家技术集团公司向卫星社表示,从塔台上投落现代化自行反坦克炮“章鱼”-SDM1,以测试其在伞投期间承受过载的能力。俄罗斯国家技术集团公...
- 新大众文艺生产模式下的网络热梗(大众文艺是scd吗)
-
作者:郑绩当二次元向元宇宙进化,ACGN(动画、漫画、游戏、小说)仅成为虚拟空间的某个维度,以“用户”为名的主体开创出新大众文艺生产模式。孕诞的过程如此丝滑,稍不留神,我们已迷走于强烈复杂的数字现实...
- 安徽业主私人定制188平农村合院,因地制宜建出优质别墅!
-
理想的生活,不仅要有一座院子、一家人、四季春秋,还要有闲适的时间与知己往来。人生得意须尽欢,一席阔厅,既是茶余饭后阖家相聚的生活主场,也是主人尊崇品味的直观呈现。下面这套农村宅院,一入家门、二赏庭院、...
- 用数学融智学人力资源模型的核心架构:建立可量化的理论框架
-
用数学融智学人力资源模型的核心架构建立可量化的理论框架一、潜能开发三阶模型1.潜能探测函数其中:2.认知锻造方程采用1+3倍增算子:其中:二、九五智尊分类模型人才特征空间映射训练优化目标三、实战...
- 优化作文结构:三种实用框架解析(作文框架梳理)
-
优化作文结构:三种实用框架解析一、案例启示:结构优化的重要性子涵写作初用“总分总”结构(如诚信主题),但中间部分未细分导致混乱。优化后:1.引论:以商鞅立木为信案例引入,总述诚信为立身之本。2....
- 农村自建房新宠!半框架结构凭啥这么火?内行人揭开3个扎心真相
-
最近回老家发现一个怪现象:几乎家家盖新房都爱用"半框架结构"。隔壁王叔家刚完工的二层小楼,外墙红砖还没勾缝,里面的水泥柱子已经支棱起来了。这玩意儿到底有啥魔力?我蹲工地三天,问了十几个...
- Apache Log4j高危漏洞,燃爆大厂、燃烧Java开源框架
-
8分钟阅读.一、背景本周对IT界的Java工程师来说,应该都有一个比较难忘的夜晚。夜半迷迷糊糊接到安全部的电话要求立即、马上升级Log4j的版本,修复安全漏洞。What?来不及…就投入了战斗。尤其大厂...
- 我的公司信息(我的公司信息英语作文)
-
软件工程师(平台开发方向)面议福建->厦门本科不限全职职位诱惑:精英团队、福利多样、假期丰富,你会是吉比特的有缘人吗职位描述岗位职责:1、负责游戏平台的功能开发任务;2、搭建游戏...
- 深入了解 Java Spring:从基础到安全防范
-
在Java开发的广阔领域中,Spring框架无疑是一颗璀璨的明星。它经历了从传统企业级开发到现代云原生、响应式编程的演变,每个大版本都在配置简化、性能提升、技术集成等方面持续创新。今天,就让我们...
- 【紧急预警】关于 Apache Log4j 2 任意代码执行漏洞的高危风险通告
-
概述我中心多家网络威胁数据联盟成员单位近日监测到ApacheLog4j2存在任意代码执行漏洞,经过分析,由于ApacheLog4j2新增的lookup功能未对输入进行严格的判断,存在递归...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- MVC框架 (46)
- spring框架 (46)
- 框架图 (58)
- flask框架 (53)
- quartz框架 (51)
- abp框架 (47)
- jpa框架 (47)
- laravel框架 (46)
- springmvc框架 (49)
- 分布式事务框架 (65)
- scrapy框架 (56)
- shiro框架 (61)
- 定时任务框架 (56)
- java日志框架 (61)
- JAVA集合框架 (47)
- mfc框架 (52)
- abb框架断路器 (48)
- grpc框架 (55)
- ppt框架 (48)
- 内联框架 (52)
- cad怎么画框架 (58)
- ps怎么画框架 (47)
- ssm框架实现登录注册 (49)
- oracle字符串长度 (48)
- oracle提交事务 (47)