本文主要是介绍The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for called method 已解决,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前面有个webSocket自动断开连接的问题,已解决,请见博客:
webSocket java.io.EOFException: null 增加心跳机制解决
然后又报了一个错:
java.lang.IllegalStateException: The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for called methodat org.apache.tomcat.websocket.WsRemoteEndpointImplBase$StateMachine.checkState(WsRemoteEndpointImplBase.java:1224) ~[tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.tomcat.websocket.WsRemoteEndpointImplBase$StateMachine.textStart(WsRemoteEndpointImplBase.java:1187) ~[tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendString(WsRemoteEndpointImplBase.java:190) ~[tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.tomcat.websocket.WsRemoteEndpointBasic.sendText(WsRemoteEndpointBasic.java:37) ~[tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at com.everestfortune.cf.config.webSocket.WebSocketServer.sendMessage(WebSocketServer.java:202) ~[classes!/:0.0.1-SNAPSHOT]at com.everestfortune.cf.config.webSocket.WebSocketServer.onMessage(WebSocketServer.java:155) ~[classes!/:0.0.1-SNAPSHOT]at sun.reflect.GeneratedMethodAccessor176.invoke(Unknown Source) ~[na:na]at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_265]at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_265]at org.apache.tomcat.websocket.pojo.PojoMessageHandlerWholeBase.onMessage(PojoMessageHandlerWholeBase.java:80) [tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.tomcat.websocket.WsFrameBase.sendMessageText(WsFrameBase.java:394) [tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.tomcat.websocket.server.WsFrameServer.sendMessageText(WsFrameServer.java:119) [tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.tomcat.websocket.WsFrameBase.processDataText(WsFrameBase.java:495) [tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:294) [tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:133) [tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:82) [tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.tomcat.websocket.server.WsFrameServer.doOnDataAvailable(WsFrameServer.java:171) [tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.tomcat.websocket.server.WsFrameServer.notifyDataAvailable(WsFrameServer.java:151) [tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.upgradeDispatch(WsHttpUpgradeHandler.java:148) [tomcat-embed-websocket-8.5.29.jar!/:8.5.29]at org.apache.coyote.http11.upgrade.UpgradeProcessorInternal.dispatch(UpgradeProcessorInternal.java:54) [tomcat-embed-core-8.5.29.jar!/:8.5.29]at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53) [tomcat-embed-core-8.5.29.jar!/:8.5.29]at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.29.jar!/:8.5.29]at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.29.jar!/:8.5.29]at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.29.jar!/:8.5.29]at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_265]at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_265]at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.29.jar!/:8.5.29]at java.lang.Thread.run(Thread.java:748) [na:1.8.0_265]
websocket推送数据的方法有:
-
session.getBasicRemote().sendText(message); //同步发送
-
session.getAsyncRemote().sendText(message); //异步发送
经过测试,在高并发的情况下,两种发送方法都会抛出上面的异常。原因是多个线程同时使用同一session发送的原因。
做如下修改后:
synchronized(session){session.getAsyncRemote().sendText(message);
}
经测试异步发送还是会抛出上述异常,同步不会出现。猜想:异步应该是new一个线程去发送,即使使用synchronized同样会出现两个session同时被不同的线程操作的时机。
决定使用:
synchronized(session){session.getBasicRemote().sendText(message);}
//在网络非常好的情况下。多线程同一个session发送数据
到这里,如果您使用的是单session,问题就应该解决了。
webSocket 的Session的定义如下:
/*** 与某个客户端的连接会话,需要通过它来给客户端发送数据*/
private Session session;
如果有多个客户端的话,亦或者同一个用户,或者打开了多个浏览器,开了多个页面
此时Session是不同的。
此时锁住其中一个Session,就不能解决这个问题了。
我是用锁住一个全局的静态的对象,来解决多线程同时访问的问题的:
/*** concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。*/private static final ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();/*** 实现服务器主动推送*/private void sendMessage(String message) throws IOException {// 尝试过锁住方法,还是不行,这里锁住webSocketMap,让多线程,访问不同对象,也能同步synchronized(webSocketMap){JSONObject jsonObject = JSON.parseObject(message);String toUserId = jsonObject.getString("user");if (StringUtils.isNotBlank(toUserId) && webSocketMap.containsKey(toUserId)) {webSocketMap.get(toUserId).getSession().getBasicRemote().sendText(message);}}//this.session.getBasicRemote().sendText(message);}
从此,就再也没报过这个问题了
补充:
刚开始给方法加synchronized,因为这个方法不是静态的,所以只能锁住当前对象,不能锁住类。
synchronized如果加在了非静态方法上,表示的是synchronized(调用方法的类的对象) {},如果加在了静态方法上,表示的是synchronized(类.class) {}
这篇关于The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for called method 已解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!