我的 OSGi-bundle 多次无意中启动和停止

My OSGi-bundle is starting and stopping unintentionally multiple times

我实现了一个 OSGi-bundle,它根据 service.xml 注入了一些其他服务。事实证明,如果激活策略设置为延迟,我的包确实会被激活。其他服务注入正确。

主要问题是,根据注入的其他服务的数量,捆绑软件会启动和停止数次。如果我将注入服务的数量减少到只有一个,则捆绑包只会启动一次。通过注入 5 个其他服务,它启动和停止 3 次,其中最后一次启动没有停止,因此启动 TCP 服务器的服务工作正常。尽管它工作正常,但这种行为很奇怪并且完全不是故意的。

我不知道如何避免这种情况,因为我不知道这是怎么发生的。

日志:

[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting a 23347814 for instance 28219381
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting b 260627 for instance 28219381
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting c 22735430 for instance 28219381
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting d 4256633 for instance 28219381
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting e service 27446331 for instance 28219381
[Component Resolve Thread] INFO A.B.SimServiceComponent - Starting SimService now . . . 28219381
[Component Resolve Thread] INFO A.OTHER - starting component
[Component Resolve Thread] INFO A.OTHER - starting component
[Component Resolve Thread] INFO A.OTHER - starting component
[Component Resolve Thread] INFO A.OTHER - starting component
[Component Resolve Thread] INFO A.OTHER - starting component
[Component Resolve Thread] INFO A.B.SimServiceComponent - Stopping SimService now . . . 28219381
[Component Resolve Thread] INFO A.B.SimServiceComponent - SimService stopped.
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting a 23347814 for instance 606383
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting b 260627 for instance 606383
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting c 28883317 for instance 606383
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting d 4256633 for instance 606383
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting e service 27446331 for instance 606383
[Component Resolve Thread] INFO A.B.SimServiceComponent - Starting SimService now . . . 606383
[Component Resolve Thread] INFO A.B.SimServiceComponent - Stopping SimService now . . . 606383
[Component Resolve Thread] INFO A.B.SimServiceComponent - SimService stopped.
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting a 23347814 for instance 33352835
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting b 260627 for instance 33352835
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting c 31287346 for instance 33352835
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting d 4256633 for instance 33352835
[Component Resolve Thread] INFO A.B.SimServiceComponent - Setting e service 27446331 for instance 33352835
[Component Resolve Thread] INFO A.B.SimServiceComponent - Starting SimService now . . . 33352835 

service.xml:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" immediate="true" name="SimService">
   <implementation class="SimServiceComponent"></implementation>
   <service>
        <provide interface="ServiceComponent"></provide>
   </service>
   <reference bind="setA" cardinality="1..1" interface="A" name="A" policy="static"/>
   <reference bind="setB" cardinality="1..1" interface="B" name="B" policy="static"/>
   <reference bind="setC" cardinality="1..1" interface="C" name="C" policy="static"/>
   <reference bind="setD" cardinality="1..1" interface="D" name="D" policy="static"/>
   <reference bind="setE" cardinality="1..1" interface="E" name="E" policy="static"/>
</scr:component>

服务setter的激活方法及示例:

public void setA(final A a) {
        LOG.info("Setting a {} for instance {} ", a.hashCode(), hashCode());
        _a = a;
 }

 public void setB(final B b) {
        LOG.info("Setting b {} for instance {} ", b.hashCode(), hashCode());
        _b = b;
 }


 public void activate(BundleContext bundleContext) throws Exception {
        LOG.info("Starting SimService now . . . {}", hashCode());       
 }

感谢您的帮助:-)

编辑:

此包内的服务不被任何其他服务使用bundle/service,因此不存在循环依赖性。

将哈希码标识添加到您的日志消息是明智之举!它表明虽然服务 ABDE 是不变的,但服务 C 的身份每次都在变化。

这意味着原始 C 服务由于某种原因已从服务注册表中注销。因为您的 SimServiceComponent 组件具有对服务 C 的强制性 1..1 引用,所以当 C 消失时它会被迫停用。然后服务 C 返回(更准确地说,注册了一个新的 C 服务),允许重新激活您的组件。

所以您需要调查 C 服务循环的原因。