org.osgi.service.event.EventHandler无法侦听EventAdmin服务发布的事件

org.osgi.service.event.EventHandler无法侦听EventAdmin服务发布的事件,osgi,equinox,Osgi,Equinox,我正在尝试运行OSGi EventAdmin服务的演示应用程序,但我实现的EventHandler无法侦听EventAdmin发布者发布的事件: 下面是事件发布者的代码,后面是侦听器(EventHandler)的代码: 侦听器的代码: public class Listener implements BundleActivator, EventHandler { public void start(BundleContext context) { Dictionary d = new

我正在尝试运行OSGi EventAdmin服务的演示应用程序,但我实现的EventHandler无法侦听EventAdmin发布者发布的事件:

下面是事件发布者的代码,后面是侦听器(EventHandler)的代码:

侦听器的代码:

public class Listener implements BundleActivator, EventHandler {    

public void start(BundleContext context) {
Dictionary d = new Hashtable();

d.put(EventConstants.EVENT_TOPIC, "lnu/test/event/Demo" );

context.registerService( EventHandler.class.getName(),
this, d );
System.out.println("event handler is registered now");
}

public void stop( BundleContext context) {}

public void handleEvent(Event event ) {
System.err.println("Event has been captured");
System.out.println("getTopic: "+event.getTopic());
System.out.println("getproperty: "+event.getProperty("XYZ"));
}
}
代码中的print语句显示事件已由发布者发布,并且侦听器已向EventHandler服务注册,但它仍然没有调用侦听器端的handleEvent方法,我不知道为什么?也不明白幕后发生了什么。没有运行时异常/错误

使用的IDE是EclipseJuno构建id:20120614-1722和Equinox

运行配置中包括以下目标平台捆绑包:

  • org.eclipse.osgi
  • org.eclipse.equinox.event
  • org.eclipse.equinox.util
  • org.eclipse.osgi.services

  • 有人能告诉我我错过了什么或做错了什么吗?或者如果您有一些指向OSGi EventAdmin服务工作示例的链接?

    我猜您的侦听器包是在发布者包发布事件之后注册的


    由于这个原因,在bundle的start方法中测试这一点很容易出错,除非您控制bundle的启动顺序。对于这个简单的测试,我建议您在发布服务器中启动一个单独的线程,每隔几秒钟发布一次事件。监听器应该在注册后开始获取它们。

    我猜您的监听器包是在发布者包发布事件之后注册的


    由于这个原因,在bundle的start方法中测试这一点很容易出错,除非您控制bundle的启动顺序。对于这个简单的测试,我建议您在发布服务器中启动一个单独的线程,每隔几秒钟发布一次事件。侦听器应在注册后开始获取它们。

    确认您的侦听器包正在导入与EventAdmin包相同的org.osgi.service.event包。您的侦听器包可能包含org.osgi.service.event包,因此没有使用与EventAdmin包相同的org.osgi.service.event包。这可能是EventAdmin捆绑包不调用EventHandler服务的原因。它可能是其他东西,但这是需要检查的。

    确认您的侦听器包正在导入与EventAdmin包相同的org.osgi.service.event包。您的侦听器包可能包含org.osgi.service.event包,因此没有使用与EventAdmin包相同的org.osgi.service.event包。这可能是EventAdmin捆绑包不调用EventHandler服务的原因。可能是其他原因,但这是需要检查的。

    亲爱的@Robin谢谢您的帮助:)您的猜测是正确的,我调查了一下,发现在发布者发布事件后,侦听器捆绑包正在注册。非常感谢您的建议。亲爱的@Robin谢谢您的善意帮助:)您的猜测是正确的,我调查了一下,发现在发布者发布事件后,侦听器捆绑包正在注册。非常感谢您的建议。亲爱的@BJ谢谢您的帮助。publisher和listener都导入了相同的org.osgi.service.event包。但是问题是,在发布者发布事件之后,侦听器捆绑包正在注册。再次感谢你的帮助:)亲爱的@BJ谢谢你的帮助。publisher和listener都导入了相同的org.osgi.service.event包。但是问题是,在发布者发布事件之后,侦听器捆绑包正在注册。再次感谢您的帮助:)
    public class Listener implements BundleActivator, EventHandler {    
    
    public void start(BundleContext context) {
    Dictionary d = new Hashtable();
    
    d.put(EventConstants.EVENT_TOPIC, "lnu/test/event/Demo" );
    
    context.registerService( EventHandler.class.getName(),
    this, d );
    System.out.println("event handler is registered now");
    }
    
    public void stop( BundleContext context) {}
    
    public void handleEvent(Event event ) {
    System.err.println("Event has been captured");
    System.out.println("getTopic: "+event.getTopic());
    System.out.println("getproperty: "+event.getProperty("XYZ"));
    }
    }