如何使用声明式服务确保EventAdmin不为null?

huangapple go评论62阅读模式
英文:

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。有人可以帮忙吗?谢谢 如何使用声明式服务确保EventAdmin不为null?

英文:

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(&quot;test&quot;, new HashMap&lt;String, Object&gt;());
		eventAdmin.postEvent(event);
		System.out.println(&quot;event posted&quot;);
	}

	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 = {
		&quot;event.topics=org/osgi/framework/BundleEvent/STARTED,test&quot;
	}
)


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 如何使用声明式服务确保EventAdmin不为null?

答案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(&quot;test&quot;, new HashMap&lt;String, Object&gt;());
        eventAdmin.postEvent(event);
        System.out.println(&quot;event posted&quot;);
    }

    }

</details>



huangapple
  • 本文由 发表于 2023年2月24日 03:33:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定