The background thread that adds sockets to the Poller, checks the poller for triggered events and hands the associated socket off to an appropriate processor as events occur.
()
| 895 | * associated socket off to an appropriate processor as events occur. |
| 896 | */ |
| 897 | @Override |
| 898 | public void run() { |
| 899 | // Loop until destroy() is called |
| 900 | while (true) { |
| 901 | |
| 902 | boolean hasEvents = false; |
| 903 | |
| 904 | try { |
| 905 | if (!close) { |
| 906 | hasEvents = events(); |
| 907 | if (wakeupCounter.getAndSet(-1) > 0) { |
| 908 | // If we are here, means we have other stuff to do |
| 909 | // Do a non-blocking select |
| 910 | keyCount = selector.selectNow(); |
| 911 | } else { |
| 912 | keyCount = selector.select(selectorTimeout); |
| 913 | } |
| 914 | wakeupCounter.set(0); |
| 915 | } |
| 916 | if (close) { |
| 917 | events(); |
| 918 | timeout(0, false); |
| 919 | try { |
| 920 | selector.close(); |
| 921 | } catch (IOException ioe) { |
| 922 | log.error(sm.getString("endpoint.nio.selectorCloseFail"), ioe); |
| 923 | } |
| 924 | break; |
| 925 | } |
| 926 | // Either we timed out or we woke up, process events first |
| 927 | if (keyCount == 0) { |
| 928 | // Non-shorrt-circuit OR since events() always needs to run here. |
| 929 | hasEvents = (hasEvents | events()); |
| 930 | } |
| 931 | } catch (Throwable x) { |
| 932 | ExceptionUtils.handleThrowable(x); |
| 933 | log.error(sm.getString("endpoint.nio.selectorLoopError"), x); |
| 934 | continue; |
| 935 | } |
| 936 | |
| 937 | Iterator<SelectionKey> iterator = keyCount > 0 ? selector.selectedKeys().iterator() : null; |
| 938 | // Walk through the collection of ready keys and dispatch |
| 939 | // any active event. |
| 940 | while (iterator != null && iterator.hasNext()) { |
| 941 | SelectionKey sk = iterator.next(); |
| 942 | iterator.remove(); |
| 943 | NioSocketWrapper socketWrapper = (NioSocketWrapper) sk.attachment(); |
| 944 | // Attachment may be null if another thread has called |
| 945 | // cancelledKey() |
| 946 | if (socketWrapper != null) { |
| 947 | processKey(sk, socketWrapper); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | // Process timeouts |
| 952 | timeout(keyCount, hasEvents); |
| 953 | } |
| 954 |
nothing calls this directly
no test coverage detected