我正在写一个系统,允许用户插入自己的代码来监视传感器。
我想编写一个基本的侦听器,以侦听其他捆绑软件中的更改。我遇到了ServiceListener,这对我来说似乎很方便。我的问题是:何时从ServiceListener调用serviceChanged()?我一直在互联网上寻找它,但找不到任何东西。
非常感谢!
package be.pxl.smartcampus.MonitorListener;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceEvent;
import be.pxl.smartcampus.util.Monitor;
import be.pxl.smartcampus.util.Data;
public class Activator implements BundleActivator, ServiceListener {
public void start(BundleContext context) {
context.addServiceListener(this);
}
public void stop(BundleContext context) {
context.removeServiceListener(this);
}
public void serviceChanged(ServiceEvent event) {
if (event.getType() == ServiceEvent.MODIFIED) {
Monitor monitor = (Monitor) (event.getServiceReference());
Data data = monitor.getData();
System.out.println("SERVICE CHANGED CALLED");
// TO DO: MAKE API CALL
}
}
}
import be.pxl.smartcampus.util.Data;
import be.pxl.smartcampus.util.Monitor;
import org.osgi.framework.BundleContext;
import java.util.Hashtable;
public class Activator extends Monitor {
private Data data = new Data();
private Hashtable<String, Data> hash = new Hashtable<String, Data>();
public void start(BundleContext bundleContext) {
hash.put("first", data);
bundleContext.registerService(Activator.class.getName(), this, hash);
changeData();
}
private void changeData() {
hash.put("second", data);
}
public void stop(BundleContext bundleContext) {
}
}
最佳答案
仅更改某些HashTable的内容是不够的。您必须在setProperties
对象上调用ServiceRegistration
。