简体   繁体   English

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

[英]Using custom Configurator with WebSockets

Just a quick question with regard to extending ServerEndpointConfig.Configurator to override Tomcat's default action of instantiating the POJO class annotated with @ServerEndpoint on receiving a WebSocket request.只是一个关于扩展 ServerEndpointConfig.Configurator 以覆盖 Tomcat 的默认操作的快速问题,即在接收 WebSocket 请求时实例化用 @ServerEndpoint 注释的 POJO 类。 My reason for doing this is that my endpoint class depends on IoC dependency injection, and therefore needs to be got from the registry to have its dependencies in place.我这样做的原因是我的端点类依赖于 IoC 依赖注入,因此需要从注册表中获取它的依赖项。

My Configurator method:我的配置器方法:

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

The @ServerEndpoint annotation is placed on on my HarbourServerEndpointImpl POJO class, not the interface that it implements. @ServerEndpoint 注释放在我的 HarbourServerEndpointImpl POJO 类上,而不是它实现的接口上。 Based on the below runtime catalina.out error message the problem appears to be that the registry is returning HarbourServerEndpoint whereas Tomcat is expecting an instance of HarbourServerEndpointImpl?根据下面的运行时 catalina.out 错误消息,问题似乎是注册表返回 HarbourServerEndpoint 而 Tomcat 期待 HarbourServerEndpointImpl 的实例?

I'm hoping someone can please explain what is going wrong with my custom Configurator.我希望有人可以解释我的自定义配置器出了什么问题。

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) 2019 年 4 月 15 日 12:45:28.488 严重 [http-nio-8080-exec-915] org.apache.coyote.AbstractProtocol$ConnectionHandler.process 读取请求时出错,忽略 java.lang.ClassCastException:无法将 $HarbourServerEndpoint_39c92acc24eb8 .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

Since posting this question, I researched Tapestry-IoC and how it works.自从发布这个问题以来,我研究了 Tapestry-IoC 及其工作原理。 Turns out there is a reason for what is being returned looking a little odd ($HarbourServerEndpoint_39c9cc24eb8b2a).原来返回的内容看起来有点奇怪是有原因的($HarbourServerEndpoint_39c9cc24eb8b2a)。 It is a service proxy object, not an instance of the service implementation as I was expecting.它是一个服务代理对象,而不是我所期望的服务实现的实例。 I'm not sure how typical this is for IoC containers.我不确定这对于 IoC 容器来说有多典型。 Quoting from the Tapestry website:引自 Tapestry 网站:

"Services consist of two main parts: a service interface and a service implementation. “服务由两个主要部分组成:服务接口和服务实现。

The service interface is how the service will be represented throughout the rest of the registry.服务接口是服务在整个注册中心其余部分的表示方式。 Since what gets passed around is normally a proxy, you can't expect to cast a service object down to the implementation class (you'll see a ClassCastException instead).由于传递的通常是代理,因此您不能指望将服务对象强制转换为实现类(您将看到 ClassCastException)。 In other words, you should be careful to ensure that your service interface is complete, since Tapestry IoC effectively walls you off from back doors such as casts."换句话说,您应该小心确保您的服务接口是完整的,因为 Tapestry IoC 有效地将您与后门(例如演员表)隔离开来。”

This makes it rather tricky to acquire from the IoC registry instances of the class annotated with @ServerEndpoint, and I'm now exploring other options.这使得从使用 @ServerEndpoint 注释的类的 IoC 注册表实例获取变得相当棘手,我现在正在探索其他选项。

Regards,问候,

Chris.克里斯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM