环境准备
- jdk安装
- felix 项目下载
- 解压启动:
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搭建开发环境
- 在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就出现了。