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

时间:2018-04-23 13:10:17

标签: java osgi osgi-bundle equinox

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

主要问题是,捆绑包启动和停止多次,具体取决于注入的其他服务的数量。如果我将注入的服务数量减少到只有一个,则只启动一次。通过注入5个其他服务,它启动和停止3次,其中最后一次启动没有停止,因此启动TCP服务器的服务正常工作。即使它工作正常,这种行为很奇怪,完全无意。

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

LOG:

[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>

激活服务设定者的方法和样本:

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());       
 }

感谢您的帮助: - )

编辑:

此捆绑包内的服务不会被任何其他捆绑/服务使用,因此没有循环依赖关系。

1 个答案:

答案 0 :(得分:3)

将哈希码标识添加到您的日志消息是明智之举!它表明虽然服务SQL> CREATE TABLE MANUFACTURER(M_NAME varchar2(75)); SQL> CREATE TABLE LINEITEM(l_manufacturer_name varchar2(75),l_partkey int); SQL> INSERT INTO MANUFACTURER VALUES('abc'); SQL> INSERT INTO MANUFACTURER VALUES('def'); SQL> INSERT INTO LINEITEM VALUES('abc',1); SQL> INSERT INTO LINEITEM VALUES('def',2); SQL> INSERT INTO LINEITEM VALUES('abc',1); SQL> INSERT INTO LINEITEM VALUES('def',2); SQL> UPDATE LINEITEM a SET a.l_manufacturer_name = (SELECT CASE WHEN MOD(b.L_PARTKEY, 2) = 0 THEN (SELECT M_NAME FROM MANUFACTURER m WHERE ROWNUM = 1) WHEN MOD(b.L_PARTKEY, 2) = 1 THEN (SELECT M_NAME FROM (SELECT M_NAME, ROWNUM AS MYROW FROM (SELECT M_NAME, ROWNUM FROM MANUFACTURER m)) WHERE MYROW = 2) END AS MANUFACTURER_NAME FROM LINEITEM b WHERE a.l_partkey = b.l_partkey); ORA-01427: single-row subquery returns more than one row SQL> UPDATE LINEITEM a SET a.l_manufacturer_name = (SELECT CASE WHEN MOD(max(b.L_PARTKEY), 2) = 0 THEN (SELECT M_NAME FROM MANUFACTURER m WHERE ROWNUM = 1) WHEN MOD(max(b.L_PARTKEY), 2) = 1 THEN (SELECT M_NAME FROM (SELECT M_NAME, ROWNUM AS MYROW FROM (SELECT M_NAME, ROWNUM FROM MANUFACTURER m)) WHERE MYROW = 2) END AS MANUFACTURER_NAME FROM LINEITEM b WHERE a.l_partkey = b.l_partkey GROUP BY b.L_PARTKEY ); 4 row updated. ABD是不变的,但服务E的身份每次都会发生变化。

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

因此,您需要了解C服务循环的原因。