gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:54:43 24 4
gpt4 key购买 nike

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

主要问题是,根据注入(inject)的其他服务的数量, bundle 会启动和停止多次。如果我将注入(inject)的服务数量减少到只有一个,则 bundle 仅启动一次。通过注入(inject) 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

服务.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 内的服务不会被任何其他 bundle /服务使用,因此不存在循环依赖关系。

最佳答案

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

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

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

关于java - 我的 OSGi-bundle 多次无意中启动和停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49981982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com