Event monitoring in osgi
osgi event monitoring requires at least two packages, import
(1) org.osgi.framework
(2) org.osgi.framework.hooks.bundle The
main code is as follows, feasible:
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;
//Get all bundles installed in the osgi environment
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());
}
}
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326360209&siteId=291194637
Ranking