java.lang.IllegalStateException:远程端点处于 [TEXT_PARTIAL_WRITING] 状态,这是被调用方法的无效状态

Posted

技术标签:

【中文标题】java.lang.IllegalStateException:远程端点处于 [TEXT_PARTIAL_WRITING] 状态,这是被调用方法的无效状态【英文标题】:java.lang.IllegalStateException: The remote endpoint was in state [TEXT_PARTIAL_WRITING] which is an invalid state for called method 【发布时间】:2017-06-13 17:33:34 【问题描述】:

我的代码:

myStompClient = new WebSocketStompClient(new SockJsClient(list));
mySession = myStompClient
        .connect(WEBSOCKET_URI, bankHeaders, new StompSessionHandlerAdapter() 
            @Override
            public void afterConnected(StompSession session, StompHeaders connectedHeaders) 
                StompHeaders stompHeadersSubscribe = new StompHeaders();
                stompHeadersSubscribe.setDestination(CONSUMER_TECH_QUEUE);
                session.subscribe(stompHeadersSubscribe, new MyStompFrameHandler());// <-- this line fails
                .....
        ).get(5, TimeUnit.SECONDS);

有时我会在以下日志中看到:

org.springframework.messaging.MessageDeliveryException: nested exception is java.lang.IllegalStateException: The remote endpoint was in state [TEXT_PARTIAL_WRITING] which is an invalid state for called method, failedMessage=GenericMessage [payload=byte[0], headers=simpMessageType=SUBSCRIBE, stompCommand=SUBSCRIBE, nativeHeaders=destination=[/user/queue/consumer/tech], id=[0], simpSubscriptionId=0, simpSessionId=e8a5b506-9c09-1a7b-4674-4aecb6567426, simpDestination=/user/queue/consumer/tech]
    at org.springframework.messaging.simp.stomp.DefaultStompSession.execute(DefaultStompSession.java:281)
    at org.springframework.messaging.simp.stomp.DefaultStompSession.subscribe(DefaultStompSession.java:312)
    at com.finvale.SimpleBankSimulator$1.afterConnected(SimpleBankSimulator.java:98)
    at org.springframework.messaging.simp.stomp.DefaultStompSession.handleMessage(DefaultStompSession.java:421)
    at org.springframework.web.socket.messaging.WebSocketStompClient$WebSocketTcpConnectionHandlerAdapter.handleMessage(WebSocketStompClient.java:342)
    at org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.handleMessageFrame(AbstractClientSockJsSession.java:271)
    at org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.handleFrame(AbstractClientSockJsSession.java:213)
    at org.springframework.web.socket.sockjs.client.WebSocketTransport$ClientSockJsWebSocketHandler.handleTextMessage(WebSocketTransport.java:162)
    at org.springframework.web.socket.handler.AbstractWebSocketHandler.handleMessage(AbstractWebSocketHandler.java:43)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.handleTextMessage(StandardWebSocketHandlerAdapter.java:110)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.access$000(StandardWebSocketHandlerAdapter.java:42)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:81)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:78)
    at org.apache.tomcat.websocket.WsFrameBase.sendMessageText(WsFrameBase.java:399)
    at org.apache.tomcat.websocket.WsFrameBase.processDataText(WsFrameBase.java:500)
    at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:295)
    at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:130)
    at org.apache.tomcat.websocket.WsFrameClient.processSocketRead(WsFrameClient.java:79)
    at org.apache.tomcat.websocket.WsFrameClient.access$300(WsFrameClient.java:31)
    at org.apache.tomcat.websocket.WsFrameClient$WsFrameClientCompletionHandler.completed(WsFrameClient.java:137)
    at org.apache.tomcat.websocket.WsFrameClient$WsFrameClientCompletionHandler.completed(WsFrameClient.java:120)
    at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126)
    at sun.nio.ch.Invoker$2.run(Invoker.java:218)
    at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: The remote endpoint was in state [TEXT_PARTIAL_WRITING] which is an invalid state for called method
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase$StateMachine.checkState(WsRemoteEndpointImplBase.java:1224)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase$StateMachine.textPartialStart(WsRemoteEndpointImplBase.java:1182)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendPartialString(WsRemoteEndpointImplBase.java:222)
    at org.apache.tomcat.websocket.WsRemoteEndpointBasic.sendText(WsRemoteEndpointBasic.java:49)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketSession.sendTextMessage(StandardWebSocketSession.java:203)
    at org.springframework.web.socket.adapter.AbstractWebSocketSession.sendMessage(AbstractWebSocketSession.java:101)
    at org.springframework.web.socket.sockjs.client.WebSocketClientSockJsSession.sendInternal(WebSocketClientSockJsSession.java:126)
    at org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.sendMessage(AbstractClientSockJsSession.java:152)
    at org.springframework.web.socket.messaging.WebSocketStompClient$WebSocketTcpConnectionHandlerAdapter.send(WebSocketStompClient.java:383)
    at org.springframework.messaging.simp.stomp.DefaultStompSession.execute(DefaultStompSession.java:278)
    ... 26 more

有时会发生,有时 - 不会。

我无法理解原因,请澄清。

【问题讨论】:

【参考方案1】:

因为你试图从很多线程连接到 websocket。 你应该使用下面的行:

synchronized(session) 
            session.subscribe(stompHeadersSubscribe, new MyStompFrameHandler())
        

【讨论】:

【参考方案2】:

我遇到了这个问题,并想出了一个不同的方法,据我所知(这种问题似乎在 *** 中报告了大约 4 次...)。

捕获错误,如果写入忙,请等待一段时间以完成并重试,就像这样。想法?

static public void write(Session sess, String msg) 
    /*
     * to thwart the error: 'remote endpoint was in state [TEXT_FULL_WRITING] which
     * is an invalid state for called method use basic'
     * try, try again...
     */
    RemoteEndpoint.Basic basicRemote = sess.getBasicRemote();
    int i = 0; // tries...
    boolean bWriteSucceeded = false;
    if (verbose) 
        ; // you know... dump something somewhere...
    
    if (sess.isOpen()) 
        for (i = 0; i < 10 && !bWriteSucceeded; i++) 
            try 
                // Crap. Sometimes getting this:
                // The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid
                // state for called method
                basicRemote.sendText(msg);
                bWriteSucceeded = true;
             catch (IOException e) 
                // Enhance the auto-generated catch block to try again...
                // TODO: check for if (e == java.io.EOFException))
                if (i < 10 && !bWriteSucceeded) 
                    msleep(20);
                    System.out.println("Write tried:" + i + "times and failed.");
                    continue;
                 // Admit failure.
                System.out.println("Write failed: " + i + " retries. Dumping stackTrace");
                e.printStackTrace();
            
        
    
    // succeed on first time and say nothing...
    if (bWriteSucceeded && i > 0)
        System.out.println("Write took " + i + " tries to succeed.");
    else
        System.out.println("Write failed after " + i + " retries.");

而我的 msleep 版本只是:

static void msleep(int ms) 
    try 
        System.out.printf("Sleeping...");
        Thread.sleep(ms);
         catch (InterruptedException e) 
            System.out.printf("Exception caught");
            


【讨论】:

以上是关于java.lang.IllegalStateException:远程端点处于 [TEXT_PARTIAL_WRITING] 状态,这是被调用方法的无效状态的主要内容,如果未能解决你的问题,请参考以下文章