英文:
How do I use Declarative Services to ensure that EventAdmin is not null?
问题
以下是你提供的内容的中文翻译:
我是新手使用OSGi,尝试创建一个简单的EventPublisher/-Admin应用程序,使用DS确保EventAdmin不为空。但我不确定如何正确使用DS。
Activator类:
package publishertest;
import java.util.HashMap;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
public class Activator implements BundleActivator {
private static BundleContext context;
@Reference
EventAdmin eventAdmin;
static BundleContext getContext() {
return context;
}
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
Event event = new Event("test", new HashMap<String, Object>());
eventAdmin.postEvent(event);
System.out.println("event posted");
}
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
}
EventHandler类:
package publishertest;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
@Component(
property = {
"event.topics=org/osgi/framework/BundleEvent/STARTED,test"
}
)
public class ServiceComponent implements EventHandler {
public void handleEvent(Event event) {
System.out.println(event.getTopic());
}
}
添加@Reference注释会导致BundleException。有人可以帮忙吗?谢谢
英文:
Im new to OSGi and I tried to create a simple EventPublisher/-Admin application using DS to ensure that the EventAdmin is not null. But I'm not sure how to use the DS in the right way.
The Activator class:
package publishertest;
import java.util.HashMap;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
public class Activator implements BundleActivator {
private static BundleContext context;
@Reference
EventAdmin eventAdmin;
static BundleContext getContext() {
return context;
}
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
Event event = new Event("test", new HashMap<String, Object>());
eventAdmin.postEvent(event);
System.out.println("event posted");
}
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
}
The EventHandler class:
package publishertest;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
@Component(
property = {
"event.topics=org/osgi/framework/BundleEvent/STARTED,test"
}
)
public class ServiceComponent implements EventHandler {
public void handleEvent(Event event) {
System.out.println(event.getTopic());
}
}
Adding the @Reference annotation leads to a BundleException. Can somebody help? thanks
答案1
得分: 2
以下是您要翻译的内容:
"你在这里混合了不兼容的技术。一个 DS 组件不应该实现 BundleActivator。在你的情况下,你的类被初始化为一个 bundle activator,DS 注解被忽略了。这就是为什么 eventAdmin 是 null。
相反,你应该使用 DS 来激活你的组件,使用 @Component 注解和构造函数上的 @Activator 注解。
你的代码应该像这样:
@Component(immediate=true)
public class MyClass {
@Activate
public MyClass(@Reference EventAdmin eventAdmin) {
Event event = new Event("test", new HashMap<String, Object>());
eventAdmin.postEvent(event);
System.out.println("event posted");
}
}
```"
<details>
<summary>英文:</summary>
You are mixing up incompatible technologies here. A DS component should not implement BundleActivator.
In your case your class is initialized as a bundle activator the DS annotations are ignored. This is why eventAdmin is null.
Instead you should DS to activate your component via @Component annotation and @Activator annotation on constructor.
Your code should look like this:
@Component(immediate=true)
public class MyClass {
@Activate
public MyClass(@Reference EventAdmin eventAdmin) {
Event event = new Event("test", new HashMap<String, Object>());
eventAdmin.postEvent(event);
System.out.println("event posted");
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论