osgi的通信代码
发布事件
BundleContext ctx = FrameworkUtil.getBundle(this.getClass())
.getBundleContext();
ServiceReference<EventAdmin> ref = ctx.getServiceReference(EventAdmin.class);
EventAdmin eventAdmin = ctx.getService(ref);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("DATA", new Date());
Event event = new Event("viewcommunication/syncEvent", properties);
eventAdmin.sendEvent(event);
event = new Event("viewcommunication/asyncEvent", properties);
eventAdmin.postEvent(event);
监听(订阅)事件
EventHandler handler = new EventHandler() {
public void handleEvent(final Event event) {
Date date=(Date) event.getProperty("DATA");
System.out.println("dfdfdfdf"+date);
}
};
Dictionary<String, String> properties = new Hashtable<String, String>();
properties.put(EventConstants.EVENT_TOPIC, "viewcommunication/*");
ctx.registerService(EventHandler.class, handler, properties);
在rcp中使用这种方式需要注意的就是:
org.eclipse.osgi.services;bundle-version="3.3.0",
org.eclipse.equinox.event;bundle-version="1.2.100"
这两个插件必须依赖
其中 org.eclipse.equinox.event 必须为自动加载