登录
  • 欢迎访问悠扬的技术博客,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站😉

玩转OSGI-ApacheFelix(二)IDEA

Apache Felix 悠扬 754次浏览 已收录

1.准备工作

      自己去官方下载,或者直接下载我第一篇文章中配置好的felix项目,自己下载的话比较麻烦,可能需要很多次进行尝试,包的版本不对,无法启动,懒得尝试或者想开箱即用的兄弟请移步到:

  1. 我的第一篇文章进行下载:点击连接
  2. 自己去官方下载:点击连接

2.下载配置idea osgi插件

玩转OSGI-ApacheFelix(二)IDEA

玩转OSGI-ApacheFelix(二)IDEA

3.新建两个maven项目

需要源码的去我上面的连接去下载一下,在上面

felix-clinet bundle

package com.nxhz.felix.client;

import com.nxhz.felix.server.Hello;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

/**
 * @author jierui
 * @package PACKAGE_NAME
 * @date 2022/7/13-9:33
 * @description:
 */
public class ClientBundleActivator implements BundleActivator {

    @Override
    public void start(BundleContext ctx) throws Exception {
        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 end---------------------");
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("stop Service:Hello");
    }
}

felix-server bundle

package com.nxhz.felix;

import com.nxhz.felix.server.Hello;
import com.nxhz.felix.server.impl.IHelloImpl;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jierui
 * @package com.nxhz.felix
 * @date 2022/7/13-9:51
 * @description:
 */
public class ServerBundleActivator implements BundleActivator {


    private List<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>();

    private static BundleContext context;

    static BundleContext getContext() {
        return context;
    }

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        ServerBundleActivator.context = bundleContext;
        System.out.println("----------------server start---------------------");
        //注册hello接口中的服务
        registrations.add(bundleContext
                .registerService(Hello.class.getName(), new IHelloImpl(), null));
        System.out.println("----------------server end---------------------");
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        ServerBundleActivator.context = null;

        for (ServiceRegistration registration : registrations) {
            System.out.println("unregistering: " + registration);
            registration.unregister();
        }
    }
}

4.配置idea osgi debug

4.1新建一个Run/Debug 启动选择OSGI Bundles

玩转OSGI-ApacheFelix(二)IDEA

4.2 配置OSGI Project Structure

玩转OSGI-ApacheFelix(二)IDEA玩转OSGI-ApacheFelix(二)IDEA玩转OSGI-ApacheFelix(二)IDEA

client manifest

Export-Packag: com.nxhz.felix
Import-Package: org.osgi.framework,com.nxhz.felix.server;version="1.0.4"

server manifest

玩转OSGI-ApacheFelix(二)IDEA

玩转OSGI-ApacheFelix(二)IDEA 

Export-Package: com.nxhz.felix.server;version="1.0.4"
Import-Package: org.osgi.framework

4.3 配置启动debug

玩转OSGI-ApacheFelix(二)IDEA玩转OSGI-ApacheFelix(二)IDEA

5.idea配置felix web console 

访问地址:http://localhost:8080/system/console/bundles/

用户名密码:admin/admin

我其实没有配置密码,需要的可以自己修改

玩转OSGI-ApacheFelix(二)IDEA玩转OSGI-ApacheFelix(二)IDEA

玩转OSGI-ApacheFelix(二)IDEA玩转OSGI-ApacheFelix(二)IDEA玩转OSGI-ApacheFelix(二)IDEA

 


版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明玩转OSGI-ApacheFelix(二)IDEA
喜欢 (1)
支付宝[]
分享 (0)
悠扬
关于作者:
10年以上工作经验:6年以上微服务架构设计搭建经验。 曾任岗位:项目经理、架构师。 擅长领域:大数据、数据库,架构设计,资源优化。 获得业绩: 1.实用新型发明专利1个,修改Apache Sharding源码设计实现分库分表程序增强方案。 2.开源项目一个:https://gitee.com/zsiyang/ruoyi-vue-atomikos (加入开源生态圈)。 3.个人技术博客地址:https://www.nxhz1688.com