简体   繁体   中英

Java Socket selector doesnt switch to write state

We have implemented solution based on selector

like

    @Override
public void run() {
    super.run();
    while (session.isConnectionAlive()) {
        try {
            // Wait for an event
            selector.select();
        } catch (IOException e) {
            log.error("Selector error: {}", e.toString());
            log.debug("Stacktrace: ", e);
            session.closeConnection();
            break;
        }
        handleSelectorkeys(selector.selectedKeys());
    }
    executorService.shutdown();
    log.debug("Ucp worker stopped");
}

private void handleSelectorkeys(Set<SelectionKey> selectedKeys) {
    Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
    while (keys.hasNext()) {
        SelectionKey selKey = keys.next();
        selector.selectedKeys().remove(selKey);
        try {
            processSelectionKey(selKey);
        } catch (IOException e) {
            // Handle error with channel and unregister
            selKey.cancel();
            log.error("Selector error: {}", e.toString());
            log.debug("Stacktrace: ", e);
        }
    }
}

public void processSelectionKey(SelectionKey selKey) throws IOException {

    // Since the ready operations are cumulative,
    // need to check readiness for each operation
    if (selKey.isValid() && selKey.isConnectable()) {
        log.debug("connectable");
        // Get channel with connection request
        SocketChannel sChannel = (SocketChannel) selKey.channel();

        boolean success = sChannel.finishConnect();
        if (!success) {
            // An error occurred; handle it
            log.error("Error on finish");
            // Unregister the channel with this selector
            selKey.cancel();
        }
    }

    if (selKey.isValid() && selKey.isReadable()) {
        readMessage(selKey);
    }

    if (selKey.isValid() && selKey.isWritable()) {
        writeMessage(selKey);
    }

    if (selKey.isValid() && selKey.isAcceptable()) {
    }

}

It works fine till we start sending around 100 messages per sec and receive also around 100 response and 100 income messages and send 100 our responses (around 400 messages per sec in both ways) With such load from time to time due to unknown issue on other side our partner cut connection. We reestablish connection but for some reason selector doesn't switch to write state only read. We receive a lot messages on read but cannot send anything.
Any ideas? Is it OS issue issue on our side with connection. How does selector works? Switch from read to write by some logic or spontaneously?

  1. There is no such thing as 'switch [from read] to write state'. A socket can be readable and writable at the same time, and a Selector doesn't 'switch': it merely reports which states exist on the socket.

  2. If the write event never triggers, it is because the socket send buffer is full, which indicates that the peer isn't reading from the connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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