仅在重启后karaf中的ClassCastException

时间:2018-03-12 05:41:37

标签: java osgi apache-karaf osgi-bundle karaf

我有两个版本05.18.000的捆绑包A,它使用了捆绑C中使用的B为05.18.000。

我在Bundle D中使用了不同版本05.20.000的相同捆绑包A和B.

Bundle A正试图将具体类强制转换为接口。接口和实现类都在Bundle B中。

当我在运行时同时使用C和D时,它们工作正常。

但是当我重新启动karaf容器时,Bundle A中会抛出一个类强制转换异常。

Dim username = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Foresight\identity\ASPNET_SETREG", "username", Nothing))
Dim password = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Foresight\identity\ASPNET_SETREG", "password", Nothing))

以下是错误:

@Override
public String parse(DeviceMessageEnvelope deviceMessageEnvelope) {

    String result = "NO RESULTS";
    LOGGER.debug("HL7MinaServerParserControllerBase.parse called.");
    try {
        HL7MinaServerCommunicationObject commObject = getHL7MinaServerCommunicationObject();

        commObject.initObject((MinaServerCom) getDeviceCom(), deviceMessageEnvelope, getParser());

        LOGGER.debug("HL7MinaServerParserControllerBase: Attempted to connect device com");
        boolean connected = getDeviceCom().connect();

        if (connected) {
            while (commObject.isRunning()) {
                LOGGER.debug("HL7MinaServerParserControllerBase.parse : In Loop isRunning() : " + commObject.isRunning());
                Thread.sleep(getWaitTime());
            }
            LOGGER.debug("HL7MinaServerParserControllerBase.parse : Exit from while commObject.isRunning()");
            result = commObject.getResult();
        }
    } catch (InterruptedException e) {
        LOGGER.warn("HL7MinaServerParserControllerBase.parse :  Error in HL7MinaServerParserControllerBase", e);
    } catch (CommunicationException e) {
        LOGGER.warn("HL7MinaServerParserControllerBase.parse :  Error in HL7MinaServerParserControllerBase", e);
    } catch (MinaSupportConfigurationException e) {
        LOGGER.warn("HL7MinaServerParserControllerBase.parse :  Error in HL7MinaServerParserControllerBase", e);
    } finally {
        try {
            getDeviceCom().release();
        } catch (CommunicationException e) {
            LOGGER.warn("HL7MinaServerParserControllerBase : Error trying to release the com", e);
        }
    }

    return result;
}

company-sdk-mina-support-hl7:5.20.0是提到的捆绑包A.

MinaServerComImpl和MinaServerCom是Bundle B中的实现和接口。

错误发生在这一行

[2018-03-01 22:46:38.287] [Device Id: 10237 Device Name : WA_CMS_GW] ERROR com.company.driver.factory.DeviceTask                            com.company.drvs.sdk.mina.com.server.types.MinaServerComImpl cannot be cast to com.company.drvs.sdk.mina.com.server.MinaServerCom
java.lang.ClassCastException: com.company.drvs.sdk.mina.com.server.types.MinaServerComImpl cannot be cast to com.company.drvs.sdk.mina.com.server.MinaServerCom
    at com.company.drvs.sdk.mina.com.server.types.parser.HL7MinaServerParserControllerBase.parse(HL7MinaServerParserControllerBase.java:43)[411:com.company.drvs.sdk.company-sdk-mina-support-hl7:5.20.0]
    at com.company.drvs.welchallyn.nce.parser.NCEParserController.getResult(NCEParserController.java:57)[412:com.company.drvs.welchallyn-nce.core:1.0.1]
    at com.company.drvs.welchallyn.nce.parser.NCEParserController.parse(NCEParserController.java:45)[412:com.company.drvs.welchallyn-nce.core:1.0.1]
    at com.company.drvs.welchallyn.nce.driver.Driver.execute(Driver.java:67)[412:com.company.drvs.welchallyn-nce.core:1.0.1]
    at com.company.driver.factory.DeviceTask.execute(DeviceTask.java:46)[321:com.company.dxc.DriverHarness:1.10.1.GA]
    at com.company.task.CycleTaskHarness.run(CycleTaskHarness.java:88)[312:com.company.dxc.TaskRunner:1.10.1.GA]
    at java.lang.Thread.run(Thread.java:748)[:1.8.0_161]
[2018-03-01 22:46:38.287] [Device Id: 10237 Device Name : WA_CMS_GW] WARN  com.company.task.CycleTaskHarness                                Task retry: Device Id: 10237 Device Name : WA_CMS_GW, com.company.task.TaskException: com.company.drvs.sdk.mina.com.server.types.MinaServerComImpl cannot be cast to com.company.drvs.sdk.mina.com.server.MinaServerCom
[2018-03-01 22:46:38.287] [Device Id: 10237 Device Name : WA_CMS_GW] ERROR com.company.task.CycleTaskHarness                                Task error:
com.company.task.TaskException: com.company.drvs.sdk.mina.com.server.types.MinaServerComImpl cannot be cast to com.company.drvs.sdk.mina.com.server.MinaServerCom
    at com.company.driver.factory.DeviceTask.execute(DeviceTask.java:54)[321:com.company.dxc.DriverHarness:1.10.1.GA]
    at com.company.task.CycleTaskHarness.run(CycleTaskHarness.java:88)[312:com.company.dxc.TaskRunner:1.10.1.GA]
    at java.lang.Thread.run(Thread.java:748)[:1.8.0_161]
Caused by: java.lang.ClassCastException: com.company.drvs.sdk.mina.com.server.types.MinaServerComImpl cannot be cast to com.company.drvs.sdk.mina.com.server.MinaServerCom
    at com.company.drvs.sdk.mina.com.server.types.parser.HL7MinaServerParserControllerBase.parse(HL7MinaServerParserControllerBase.java:43)[411:com.company.drvs.sdk.company-sdk-mina-support-hl7:5.20.0]
    at com.company.drvs.welchallyn.nce.parser.NCEParserController.getResult(NCEParserController.java:57)[412:com.company.drvs.welchallyn-nce.core:1.0.1]
    at com.company.drvs.welchallyn.nce.parser.NCEParserController.parse(NCEParserController.java:45)[412:com.company.drvs.welchallyn-nce.core:1.0.1]
    at com.company.drvs.welchallyn.nce.driver.Driver.execute(Driver.java:67)[412:com.company.drvs.welchallyn-nce.core:1.0.1]
    at com.company.driver.factory.DeviceTask.execute(DeviceTask.java:46)[321:com.company.dxc.DriverHarness:1.10.1.GA]
    ... 2 more

MinsServerComImpl通过spring注入,getDeviceCom()返回MinaServerComImpl的实例。

请帮忙。感谢。

2 个答案:

答案 0 :(得分:1)

通常的原因是对同一个类别有多个定义。

在Java中,类的标识是其完全限定名称 AND 定义它的ClassLoader的组合。如果两个类加载器定义一个类 - 即使在磁盘上使用相同的类数据 - 它们也将被视为JVM中的不同类。从这些类定义之一继承的任何子类都不会被视为另一个定义的子类型。

在您的情况下,您可能已经在多个捆绑包中复制了MinaServerCom类。而是确保定义仅在一个包中,应从其包中导出。其他包应该导入该包。

答案 1 :(得分:0)

ClassCastException由于一个类的任何对象无法转换为另一个类,因为它们不属于“父子”关系。

正如你所说的..这条线的问题..

commObject.initObject((MinaServerCom) getDeviceCom(), deviceMessageEnvelope, getParser());

此处getDeviceCom()返回MinsServerComImpl的对象,您尝试将其转换为MinaServerCom。那么请确保这两个班级之间有任何Is-A关系。

应该是

class MinsServerComImpl extends MinaServerCom

class MinsServerComImpl implements MinaServerCom