osgi 引用不同版本的jar_Hello, IntelliJ IDEA + OSGi + Equinox

本文介绍了初学者如何通过IntelliJ IDEA和Equinox学习OSGi,从HelloWorld示例到服务注册与消费,一步步构建模块化Java应用。重点讲解了OSGi框架、Bundle、服务组件和动态加载的概念。
摘要由CSDN通过智能技术生成

最近在学习 OSGi,其实就是这两天。很久以前我曾经是一个 Java 开发工程师,后来做了测试就再也没有碰过 Java 了。重新开始学习服务端,还是很有意思。OSGi 是什么呢?
百度提供了一个搜索功能,一搜一大把。
OSGi(Open Service Gateway Initiative)技术是面向Java的动态模型系统。这个框架实现了一个优雅、完整和动态地组价模型。应用程序(称为 bundle )无序重新引导可以被远程安装、启动、升级和卸载。
OSGi 服务平台提供在多种网络设备上无需重启的动态改变构造的功能。为了最小化耦合度和促使这些耦合度可管理,OSGi 技术提供一种面向服务的架构,它能使这些组件动态地发现对方。
OSGi 联盟已经开发了例如像 HTTP服务器、配置、日志、安全、用户管理、XML 等很多公共功能标准组件接口。这些组件的兼容性插件实现可以从进行了不同优化和使用代价的不同计算机服务提供商得到。然而,服务接口能够基于专有权基础上开发。
OSGi 的主要职责就是为了让开发者能够创建动态化、模块化的 Java 系统。
这篇文章讲的比较好:https://www.cnblogs.com/jingmoxukong/p/4546947.html。不过感觉没有长时间写过 Java 企业开发的同学,理解 OSGI 还是比较困难的,反正我还是一知半解。OSGi 的框架
OSGi 的框架有很多,Equinox 看上去资料比较多,而且工作中也会有关系,所以就选择了 Equinox 来入门。Equinox 项目是 Eclipse 开源组织提供的 OSGi 框架的实现。Equinox 项目包括 OSGi R4 版本规范核心框架的实现,一系列 OSGi 标准服务 Bundle 及运行基于 OSGi 的系统的一些基础构件。用户可以直接访问该项目在 Eclipse 的网址 Equinox 获取概括性的信息。目前,关于Equinox的进展大部分资料及项目进展存在于Eclipse Wiki上。
目前,Equinox 项目包括 OSGi 核心框架的实现,OSGi 标准服务 Bundle 实现,OSGi 的服务器端(J2EE 实现)应用,Equinox 部署更新框架及一些研究方向(未成熟发布的构想如JMX管理,安全管理,面向方面的设计与应用等)。IntelliJ IDEA + Equinox
概念太难懂,不如直接实操。我的环境是:

  1. IntelliJ IDEA ULTIMATE 2019.1
  2. jdk 9
  3. equinox-SDK-4.11

首先创建一个空的 OSGi 项目, Library 这里先选择 Set up library later

23246503c8e7b143a3831cfc431fb9e6.png


在 ide 的设置里,配置 Equinox

b2f06fe47d0a1a09379548b9a7eeebae.png


在你的空项目里添加 Equinox 的依赖

a5fa77f7875e649ab4d693453b9b34bd.png


接下来我们创建一个 HelloWorldActivator.java,package com.hengwen.osgi;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class HelloWorldActivator implements BundleActivator {
@Overridepublic void start(BundleContext context) throws Exception {
System.out.println("Hello, World!");
}
@Overridepublic void stop(BundleContext context) throws Exception {
System.out.println("Goodbye, World!");
}
}
这里会提示 Bundle activator is not registered in manifest。我们在 Module Setting 里设置下Manifest。一般来说应该会把Manifest文件放在项目里编辑,不过 idea 可以通过ide设置,然后会打到 Jar 包中去。

b158f8e592e35a8cf36ed55a3c99bc4f.png


然后,添加一个自定义运行:

e13b7b7d5f902a2e824aeaf4c914bf85.png

30b6441fb5dccaddf09998df4023f5cd.png


运行之后,会在终端输出信息,可以看到我start 209 stop 209。209 是我们这个 HelloWorld bundle 的序号。到这里一个简单的 HelloWorld 模块就完成了。接下来,我们再添加一个新的 Bundle,给前面这个 Bundle 使用。在项目目录上右击添加新的module。

104aaecbd6c41ba408a1caae8018f0b9.png


我们创建一个接口 HelloService,一个实现类 HelloServiceImpl,目录结构就如下:

b129829680554ddf3d2854dda5099d99.png

package com.hengwen.osgi.service;public interface HelloService {public String sayHello();
}package com.hengwen.osgi.service.impl;import com.hengwen.osgi.service.HelloService;public class HelloServiceImpl implements HelloService {
@Overridepublic String sayHello() {
System.out.println("Inside HelloServiceImple.sayHello()");return "Say Hello";
}
}
现在,我们为它配置 Manifest,因为目前它没有 activator,所以就简单配置下。

3b4830236ac21097379138ff470525e3.png


注意这里配置了一个:Export-Package: com.hengwen.osgi.service,是为了其他的 bundle 能调用它,否则下面的代码就会报 HelloService not exported

1b1863cc0fe80af8a7db15935b0231ba.png


到目前为止,代码还是无法运行,虽然编译都正常(因为可见性已经解决),但是在 OSGi 的容器中,HelloServiceImpl 不能直接使用。我们需要注册服务,使用服务。我们再创建一个 activator

e96b38cce1f401da5d1cdfd76bf01954.png

package com.hengwen.osgi.service.impl;import com.hengwen.osgi.service.HelloService;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceRegistration;public class HelloServiceActivator implements BundleActivator {
ServiceRegistration helloServiceRegistration;
@Overridepublic void start(BundleContext context) throws Exception {
HelloService helloService = new HelloServiceImpl();
helloServiceRegistration =context.registerService(HelloService.class.getName(), helloService, null);
}
@Overridepublic void stop(BundleContext context) throws Exception {
helloServiceRegistration.unregister();
}
}
我们把 HelloServiceActivator 配置到 Manifest 里

ee7e43f26f9fc864d8bf70bdbb8372d1.png


这样服务就注册好了。我们接下来就使用服务,修改前面那个 bundle 的 HelloWorldActivator,package com.hengwen.osgi;import com.hengwen.osgi.service.HelloService;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceReference;public class HelloWorldActivator implements BundleActivator {
ServiceReference helloServiceReference;
@Overridepublic void start(BundleContext context) throws Exception {
System.out.println("Hello World!!");
helloServiceReference= context.getServiceReference(HelloService.class.getName());
HelloService helloService =(HelloService)context.getService(helloServiceReference);
System.out.println(helloService.sayHello());
}
@Overridepublic void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World!!");
context.ungetService(helloServiceReference);
}
}
现在我们再来运行一次,需要把新的这个bundle 加到 osgi 的运行配置中去。

4b9753c55ace7ae7659d96589ec10ba6.png

36d7d7c15dec04a1cdea2090d4e5d923.png


调用就成功了。这样差不多一个简单的 Hello, IntelliJ IDEA + OSGi + Equinox 就完成了。大家有兴趣可以自己试试看。参考文档

  1. https://www.javaworld.com/article/2077837/java-se-hello-osgi-part-1-bundles-for-beginners.html —— Hello, OSGi, Part 1: Bundles for beginners
  2. https://www.cnblogs.com/garfieldcgf/p/6378443.html —— 深入理解OSGI:Java模块化之路
  3. https://blog.csdn.net/afandaafandaafanda/article/details/43083957 —— Equinox 和 OSGI 介绍
  4. http://osgi.com.cn/article/7289219
  5. https://www.jianshu.com/p/2c0c8091e3de


作者: Lihuazhang (恒温)

原文链接:https://testerhome.com/topics/19214

转载文章时务必注明原作者及原始链接,并注明「发表于 TesterHome 」,并不得对作品进行修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值