osgi中事件监听

osgi事件监听至少需要两个包,导入
[color=green](1)org.osgi.framework
(2) org.osgi.framework.hooks.bundle[/color]

主要代码如下,可行:

public class Activator implements BundleActivator,FrameworkListener,BundleListener,ServiceListener {

private static BundleContext context;

static BundleContext getContext() {
return context;
}

/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
//获取osgi环境中安装的所有bundle
for(Bundle bundle:bundleContext.getBundles()){
System.out.println("bundle symbolic name: " + bundle.getSymbolicName());
System.out.println("osgi.framework="+context.getProperty("osgi.framework"));
}
context.addFrameworkListener(this);
context.addBundleListener(this);
context.addServiceListener(this);
}

/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}

@Override
public void serviceChanged(ServiceEvent event) {
// TODO Auto-generated method stub
if((event.getType() & ServiceEvent.REGISTERED) != 0) {
System.err.println("Service Register: " + event.getServiceReference());
}
}

@Override
public void bundleChanged(BundleEvent event) {
// TODO Auto-generated method stub
if((event.getType() & BundleEvent.STARTED) != 0) {
System.err.println("bundle started: " + event.getBundle());
} else if((event.getType() & BundleEvent.STOPPED) != 0) {
System.err.println("Bundle Stopped: " + event.getBundle());
}
}

//处理框架事件
@Override
public void frameworkEvent(FrameworkEvent event) {
// TODO Auto-generated method stub
if((event.getType() & FrameworkEvent.ERROR) != 0){
System.err.println("Framework Error: " + event.getBundle());
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值