idea搭建osgi项目开发学习

栏目: 服务器 · 发布时间: 6年前

内容简介:本文介绍了用idea搭建osgi项目开发的过程,演示使用的jdk8,equinor osgi framework。下载地址:本文下载的是equinox-SDK-4.11.zip,下载后进行解压,后面需要用到这个解压目录。

本文介绍了用idea搭建osgi项目开发的过程,演示使用的jdk8,equinor osgi framework。

equinor下载

下载地址: https://download.eclipse.org/...

本文下载的是equinox-SDK-4.11.zip,下载后进行解压,后面需要用到这个解压目录。

idea搭建osgi项目开发学习

idea创建工程

File -> New -> Project,选择Java,点击Next,创建一个空工程。

idea搭建osgi项目开发学习

继续点击Next。

idea搭建osgi项目开发学习

填写项目名称,这里叫osgi_demo。

idea搭建osgi项目开发学习

工程下创建osgi模块

创建api、server、client三个模块。

创建模块时勾选OSGI作为开发环境,Use library从刚才下载的equinox解压的目录下的plugins目录中选择org.eclipse.osgi_3.13.300.v20190218-1622.jar。

idea搭建osgi项目开发学习

创建模块完成之后,打开idea的preferences,在Languages & Frameworks找到OSGI Framework Instances选项。

idea搭建osgi项目开发学习

添加Equinox,Home directory选择刚才解压的Equinox目录。

idea搭建osgi项目开发学习

编写演示代码

结构如下图

idea搭建osgi项目开发学习

api模块中定义接口类IHelloService

package osgi.demo.api;

public interface IHelloService {
    /**
     * 和某人打招呼
     * @param somebody
     * @return
     */
    String sayHello(String somebody);
}

server模块接口实现类HelloServiceImpl

package osgi.demo.server;

import osgi.demo.api.IHelloService;

public class HelloServiceImpl implements IHelloService {
    @Override
    public String sayHello(String somebody) {
        return "hello " + somebody;
    }
}

server模块服务注册类HelloServerBundle

package osgi.demo.server;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import osgi.demo.api.IHelloService;

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

public class HelloServerBundle implements BundleActivator {
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        IHelloService service = new HelloServiceImpl();
        Dictionary<String , Object> properties = new Hashtable<>();
        //服务注册
        bundleContext.registerService(IHelloService.class, service, properties);
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
    }
}

client模块调用服务类HelloClientBundle

package osgi.demo.client;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import osgi.demo.api.IHelloService;

import java.util.Objects;

public class HelloClientBundle implements BundleActivator {
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        //获取到IHelloService服务引用
        ServiceReference<IHelloService> reference = bundleContext.getServiceReference(IHelloService.class);
        if (Objects.nonNull(reference)) {
            //发现服务
            IHelloService service = bundleContext.getService(reference);
            if (Objects.nonNull(service)) {
                System.out.println(service.sayHello("jecyhw"));
            }
            //注销服务
            bundleContext.ungetService(reference);
        }
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {

    }
}

各模块osgi配置

api模块配置,导出接口定义所在包osgi.demo.api。

idea搭建osgi项目开发学习

server模块配置,配置HelloServerBundle类作为该bundle的启动类。

idea搭建osgi项目开发学习

client模块配置,配置HelloClientBundle类作为该bundle的启动类。

idea搭建osgi项目开发学习

osgi启动配置并运行

选择Edit Configurations。

idea搭建osgi项目开发学习

添加OSGI Bundles。

idea搭建osgi项目开发学习

配置如下。

idea搭建osgi项目开发学习

client模块调用了server的服务,按照依赖关系,server模块需要先启动,把服务注册在osgi框架中,client模块才能调用到,Start level是用来定义bundle模块的启动优先级,值越小,启动优先级越高。

Framework start level是整个osgi框架的启动级别,也就是整个项目的启动级别,大于这个值的bundle模块是不会被启动的。如果这个值为1,client模块的启动级别为2,client模块是不会被启动的,可以调整试试。

点击OK之后,就可以运行了。

idea搭建osgi项目开发学习

运行结果截图。

idea搭建osgi项目开发学习


以上所述就是小编给大家介绍的《idea搭建osgi项目开发学习》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Compilers

Compilers

Alfred V. Aho、Monica S. Lam、Ravi Sethi、Jeffrey D. Ullman / Addison Wesley / 2006-9-10 / USD 186.80

This book provides the foundation for understanding the theory and pracitce of compilers. Revised and updated, it reflects the current state of compilation. Every chapter has been completely revised ......一起来看看 《Compilers》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器