BlueprintListener的使用

OSGI里面有很多种Listener,添加一个Listener一般是调用BundleContext里面的addListener,比如

BundleContext.addBundleListener();
BundleContext.addFrameworkListener();
BundleContext.addServiceListener();

但是,BlueprintListener有点不一样

下面有个简单的用法

package com.pp;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.blueprint.container.BlueprintListener;

public class BlueprintListenerExample implements BundleActivator
{
	private ServiceRegistration<BlueprintListener> registration = null;
	
	public void start(BundleContext context) throws Exception
	{
		BlueprintListener blueprintListener = (event) -> {
			System.out.println("BlueprintListener " + event.getBundle().getSymbolicName() + ", type=" + event.getType());
		};
		
		registration = context.registerService(BlueprintListener.class, blueprintListener, null); 
	}

	public void stop(BundleContext context) throws Exception
	{
		registration.unregister();
	}
}


然后,在MANIFEST.MF文件里面增加Bundle-Activator:com.pp.BlueprintListenerExample
这样,就添加了一个BlueprintListener

你可能感兴趣的:(BlueprintListener的使用)