gpt4 book ai didi

java - 将自定义配置器与 WebSockets 结合使用

转载 作者:行者123 更新时间:2023-11-28 23:58:17 24 4
gpt4 key购买 nike

关于扩展 ServerEndpointConfig.Configurator 以覆盖 Tomcat 在接收到 WebSocket 请求时实例化带有 @ServerEndpoint 注释的 POJO 类的默认操作的快速问题。我这样做的原因是我的端点类依赖于 IoC 依赖注入(inject),因此需要从注册表中获取它的依赖关系。

我的配置器方法:

@Override 
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
return endpointClass.cast(RegistryProxy.getService(HarbourServerEndpoint.class));
}

@ServerEndpoint 注释位于我的 HarbourServerEndpointImpl POJO 类上,而不是它实现的接口(interface)上。根据以下运行时 catalina.out 错误消息,问题似乎是注册表正在返回 HarbourServerEndpoint,而 Tomcat 需要一个 HarbourServerEndpointImpl 实例?

我希望有人能解释一下我的自定义配置器出了什么问题。

15-Apr-2019 12:45:28.488 SEVERE [http-nio-8080-exec-915] org.apache.coyote.AbstractProtocol$ConnectionHandler.process Error reading request, ignored java.lang.ClassCastException: Cannot cast $HarbourServerEndpoint_39c9cc24eb8b2a to com.optomus.harbour.services.HarbourServerEndpointImpl at java.lang.Class.cast(Class.java:3369) at com.optomus.harbour.services.HarbourServerEndpointConfigurator.getEndpointInstance(HarbourServerEndpointConfigurator.java:17) at org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:44)

    at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:133)

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:846)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471)

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:748)

Finally, with no casting at all, the compiler gives the error:

Error:(17, 40) java: incompatible types: inference variable T has incompatible bounds 
equality constraints: com.optomus.harbour.services.HarbourServerEndpoint
upper bounds: T,java.lang.Object

最佳答案

自发布此问题后,我研究了 Tapestry-IoC 及其工作原理。事实证明,返回的内容看起来有点奇怪是有原因的($HarbourServerEndpoint_39c9cc24eb8b2a)。它是一个服务代理对象,而不是我所期望的服务实现实例。我不确定这对于 IoC 容器来说有多典型。引自 Tapestry 网站:

“服务由两个主要部分组成:服务接口(interface)和服务实现。

服务接口(interface)是服务在注册表其余部分中的表示方式。由于传递的通常是代理,因此您不能期望将服务对象强制转换为实现类(您会看到 ClassCastException)。换句话说,你应该小心确保你的服务接口(interface)是完整的,因为 Tapestry IoC 有效地让你远离后门,比如强制转换。”

这使得从 IoC 注册表中获取带有 @ServerEndpoint 注释的类的实例变得相当棘手,我现在正在探索其他选项。

问候,

克里斯。

关于java - 将自定义配置器与 WebSockets 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55738298/

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