gpt4 book ai didi

apache-karaf - Karaf OSGI Aries 蓝图 ClassCastException : Impl cannot be cast to Interface

转载 作者:行者123 更新时间:2023-12-02 03:51:51 27 4
gpt4 key购买 nike

Karaf v2.3.0,org.apache.aries.blueprint.core:1.0.1,在从另一个包导入的服务上调用接口(interface)方法时抛出 ClassCastException。接口(interface)方法定义在接口(interface)上,由实现类实现。我无法弄清楚服务代理如何认为它是 Impl (TicketServiceImpl) 而不是接口(interface) (TicketService)。对于如何解决此问题的任何建议或意见,我都非常感谢。

异常(exception):

    Caused by: java.lang.ClassCastException: 
org.abc.TicketServiceImpl cannot be cast to
org.abc.TicketService at
Proxy3ac85313_c60c_42db_8def_ea7bd3d7411c.add(Unknown Source)
...
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.6.0_37]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)[:1.6.0_37]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)[:1.6.0_37]
at java.lang.reflect.Method.invoke(Method.java:597)[:1.6.0_37]
at org.apache.aries.blueprint.utils.ReflectionUtils.invoke(ReflectionUtils.java:297)[7:org.apache.aries.blueprint.core:1.0.1]
at org.apache.aries.blueprint.container.BeanRecipe.invoke(BeanRecipe.java:958)[7:org.apache.aries.blueprint.core:1.0.1]
...

奇怪的是,在异常发生之前,我打印了 proxy .getInterfaces() ,这意味着代理是 TicketService,而不是 TicketServiceImpl。

    MyTicketServer proxy interfaces: org.abc.TicketService, 
org.apache.aries.proxy.weaving.WovenProxy
MyTicketServer Proxy class name: Proxy3ac85313_c60c_42db_8def_ea7bd3d7411c

我还尝试在代理上强制进行类测试:

    if (myTicketService instanceof TicketService) { <-- you'd think this would help
myTicketService.add(ticket); //<-- Throws proxy ClassCastException!!
} // implies problem is in the return path through proxy back to method

最佳答案

几乎明白了... TicketService 上的 classCastException 已通过删除客户端 blueprint.xml 中客户端 bean 上的“init-method”得到修复。现在,doProcess() 不是从 init-method 启动的,而是通过 driver.onRegisterService() 启动的,它调用 client.doProcess(),它调用 TicketService.add() 没有问题,没有异常。

<bean id="driver" class="org.xyz.RetrieveDriver"></bean>
<service id="ticketRetriever" interface="org.xyz.TicketRetriever"
ref="ticketRetrieverRT">
<registration-listener ref="driver"
registration-method="onRegisterService"
unregistration-method="onUnregisterService" />
</service>

这让我觉得在服务代理完成构建之前,客户端一直在尝试使用该服务(在它的 init-method="doProcess"中)。我认为等待客户端正式注册的第三类(驱动程序)将确保代理已准备好供客户端使用。

但是,然后我向客户端添加了第二个服务属性。第二个服务在调用其方法时间歇性地抛出 ClassCastException。现在我认为这可能是一个普通的旧竞争条件;我通过添加驱动程序稍微减慢了客户端的速度,但速度还不足以阻止第二个服务在准备就绪之前被使用。这也可能是错误的。我很感激任何想法。

如果问题没有神奇地消失,我会假设服务尚未准备好,为 ClassCastException 添加一个捕获。

关于apache-karaf - Karaf OSGI Aries 蓝图 ClassCastException : Impl cannot be cast to Interface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14096802/

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