IDEA felix osgi项目搭建(1)

idea创建felix项目
felix官网

环境准备

  1. jdk安装
  2. felix 项目下载
  3. 解压启动:

java -jar bin/felix.jar

felix启动,安装bundle目录所有的bundles。默认的,bundle目录含有shell相关的用户界面来与框架交互。安装到框架的bundle会复制一份到缓存目录(felix-cache)。
使用不同的缓存目录使用命令:

 java -jar bin/felix.jar <cache-path>

(进入bin目录,未启动成功? java -jar felix.jar)
使用
4. shell命令
在goto shell 输入help查看命令说明,命令名

 <scope>:<name>

安装bundles 使用felix:install 查看bundle使用felix:lb
输入stop 0 关闭系统bundle。 再次启动关联的cache安装的bundles会自动重新加载。

idea搭建开发环境

  1. 在setting中搜索osgi,框架实例点击添加felix
    在这里插入图片描述

创建文件manifest.mf
在这里插入图片描述
新建Activator.java (官网copy)

package com.ppx.demo;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;

public class Activator implements BundleActivator, ServiceListener
{
    /**
     * Implements BundleActivator.start(). Prints
     * a message and adds itself to the bundle context as a service
     * listener.
     * @param context the framework context for the bundle.
     **/
    @Override
    public void start(BundleContext context)
    {
        System.out.println("Starting to listen for service events.");
        context.addServiceListener(this);
    }

    /**
     * Implements BundleActivator.stop(). Prints
     * a message and removes itself from the bundle context as a
     * service listener.
     * @param context the framework context for the bundle.
     **/
    @Override
    public void stop(BundleContext context)
    {
        context.removeServiceListener(this);
        System.out.println("Stopped listening for service events.");

        // Note: It is not required that we remove the listener here,
        // since the framework will do it automatically anyway.
    }

    /**
     * Implements ServiceListener.serviceChanged().
     * Prints the details of any service event from the framework.
     * @param event the fired service event.
     **/
    @Override
    public void serviceChanged(ServiceEvent event)
    {
        String[] objectClass = (String[])
                event.getServiceReference().getProperty("objectClass");

        if (event.getType() == ServiceEvent.REGISTERED)
        {
            System.out.println(
                    "Ex1: Service of type " + objectClass[0] + " registered.");
        }
        else if (event.getType() == ServiceEvent.UNREGISTERING)
        {
            System.out.println(
                    "Ex1: Service of type " + objectClass[0] + " unregistered.");
        }
        else if (event.getType() == ServiceEvent.MODIFIED)
        {
            System.out.println(
                    "Ex1: Service of type " + objectClass[0] + " modified.");
        }
    }
}

项目启动配置:
打开运行配置,选择osgi bundle 选择合适的osgi框架,并将当前module添加进来
在这里插入图片描述

如果使用ide的配置文件,点击项目配置
在这里插入图片描述
然后运行,当当当,控制台输入lb,写的demo就出现了。
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值