使用 SpringFramework 发布 STOMP 消息时偶尔出现 NullPointerException

Occasional NullPointerException when publishing STOMP message using SpringFramework

我 运行 在使用 Spring (5.2.0) 发布 STOMP 消息时偶尔会遇到一些 NPE。 不幸的是,我无法准确提供重现的步骤,但我希望能深入了解这是如何发生的。

我查看了来源,我唯一能想到的是该频道为空。我希望得到确认和可能的原因,或者其他解释。

客户端没有断开连接,但似乎此错误导致客户端在收到 1002 代码时断开连接。

附件是堆栈跟踪。任何帮助或见解将不胜感激。谢谢!

org.springframework.messaging.MessageDeliveryException: Failed to send message to ExecutorSubscribableChannel[clientInboundChannel]; nested exception is java.lang.NullPointerException
    at org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:146)
    at org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:122)
    at org.springframework.web.socket.messaging.StompSubProtocolHandler.handleMessageFromClient(StompSubProtocolHandler.java:302)
    at org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.handleMessage(SubProtocolWebSocketHandler.java:335)
    at org.springframework.web.socket.handler.WebSocketHandlerDecorator.handleMessage(WebSocketHandlerDecorator.java:75)
    at org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator.handleMessage(LoggingWebSocketHandlerDecorator.java:56)
    at org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator.handleMessage(ExceptionWebSocketHandlerDecorator.java:58)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.handleTextMessage(StandardWebSocketHandlerAdapter.java:114)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.access[=10=]0(StandardWebSocketHandlerAdapter.java:43)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.onMessage(StandardWebSocketHandlerAdapter.java:85)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.onMessage(StandardWebSocketHandlerAdapter.java:82)
    at org.apache.tomcat.websocket.WsFrameBase.sendMessageText(WsFrameBase.java:395)
    at org.apache.tomcat.websocket.server.WsFrameServer.sendMessageText(WsFrameServer.java:119)
    at org.apache.tomcat.websocket.WsFrameBase.processDataText(WsFrameBase.java:495)
    at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:294)
    at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:133)
    at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:82)
    at org.apache.tomcat.websocket.server.WsFrameServer.doOnDataAvailable(WsFrameServer.java:171)
    at org.apache.tomcat.websocket.server.WsFrameServer.notifyDataAvailable(WsFrameServer.java:151)
    at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.upgradeDispatch(WsHttpUpgradeHandler.java:148)
    at org.apache.coyote.http11.upgrade.UpgradeProcessorInternal.dispatch(UpgradeProcessorInternal.java:54)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
    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)
Caused by: java.lang.NullPointerException: null
    at org.springframework.messaging.support.AbstractMessageChannel$ChannelInterceptorChain.applyPreSend(AbstractMessageChannel.java:178)
    at org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:132)
    ... 28 common frames omitted```

与此问题无关;但你应该尽量跟上维护版本;当前 5.2.x 版本是 5.2.6.

NPE 意味着您以某种方式在拦截器列表中得到了 null

抽象通道中的第 178 行正在调用列表中的每个拦截器。

https://github.com/spring-projects/spring-framework/blob/927b8c15ef20eaaa4002d4b2170cc536a6d6aa35/spring-messaging/src/main/java/org/springframework/messaging/support/AbstractMessageChannel.java#L178

我认为获得 NPE 的唯一方法是将 null 添加到列表中。

for (ChannelInterceptor interceptor : interceptors) {
    Message<?> resolvedMessage = interceptor.preSend(messageToUse, channel); // line 178
    if (resolvedMessage == null) {
        ...