我有一个包含Product实体的持久性捆绑包。由于我想按需触发Solr服务器上的更新,因此当我持久/修改/删除实例时,我引入了一个实体侦听器:
from var_test import MyClass2 as mc # If you saved it in var_test.py
a = mc(variable1=1234)
b = mc(variable1='wheee')
assert a.variable1 is not b.variable1
assert a.properties['variable1'] == a.variable1
assert a.variable1 == 1234
为了将持久性单元集成到OSGi环境中,我利用白板模式编写了一个适配器捆绑包:
@Entity
public class Product {
// id and fields
@PrePersist
public void onPersist () {
ProductAudit.onPersist( this );
}
// other lifecycle callbacks
}
public class ProductAudit {
private static final Set<ProductListener> listeners = new HashSet<>();
// static addListener/removeListener
public void onPersist (Product p) {
listeners.forEach(l -> {
l.onPersist(p);
}
}
}
现在我有两个提供程序包-一个测试包和一个索引器包。当我同时部署两者时,将在控制台上打印:
@Component(
name="com.acme.product.audit",
immediate=true)
public class OsgiProductListener implements ProductListener {
private ServiceTracker<ProductListener,ProductListener> tracker;
@Activate
public void activate (BundleContext context) {
tracker = new ServiceTracker<>(context, ProductListener.class, new ServiceTrackerCustomizer<ProductListener, ProductListener>() {
@Override
public ProductListener addingService(ServiceReference<ProductListener> reference) {
if (validProps(reference)) {
ProductListener l = reference.getBundle().getBundleContext().getService(reference); // null
ProductListener l = context.getService(reference); // null too
System.out.println("Adding " + l);
ProductAudit.addListener(l);
return l;
}
return null;
}
// modifiedService, removedService
private boolean validProps(ServiceReference<?> reference) {
// check for enabling flag
}
});
tracker.open();
}
所有三个捆绑软件都使用相同的API版本,否则我的跟踪器不会跟踪它们。
那么,如果我尝试获得ProductListener服务,为什么会得到空值?