EventAdmin is null in maven-osgi project

后端 未结 1 833
我在风中等你
我在风中等你 2021-01-26 04:14

I made an maven-osgi project where an activator should send an osgi event but for some reason EventAdmin is always null.

Here is my java class

package co         


        
相关标签:
1条回答
  • 2021-01-26 04:26

    You have implemented a classic anti-pattern in OSGi: you assume that the EventAdmin service will already be available when your bundle starts. This is an inherently unsafe assumption because your bundle might in fact be started before the bundle that provides the EventAdmin service.

    There is a wrong way and a right way to fix this. The wrong way is to insist that your bundle must be started after EventAdmin. That way leads to start-order dependencies and extreme fragility. See: http://wiki.osgi.org/wiki/Avoid_Start_Order_Dependencies

    The right way is to use a framework such as Declarative Services (DS) to declare a component inside your bundle that references the EventAdmin service. Then DS will activate your component and inject it with the EventAdmin instance as soon as it becomes available. See: http://wiki.osgi.org/wiki/Declarative_Services

    0 讨论(0)
提交回复
热议问题