private void startReceiver()
    {
        if (receiver != null)
        {
            return;
        }

        receiver = receiverExecutor.submit(new Runnable()
        {
            @Override
            public void run()
            {
                while (true)
                {
                    try
                    {
                        if (readSelector.select() != 1 || Thread.interrupted())
                        {
                            break;
                        }

                        readSelector.selectedKeys().clear();

                        for (String value : packet.read(socket))
                        {
                            LOGGER.trace("Received message: {}", value);

                            if (interceptor != null && interceptor.intercept(value))
                            {
                                continue;
                            }

                            /*
                             * If the client stops waiting for a response, it
                             * will increment this value. To keep things in
                             * sync, those skipped responses need to be thrown
                             * away when they arrive.
                             */
                            if (skippedResponses.get() > 0)
                            {
                                LOGGER.trace("Discarding skipped message: {}", value);

                                skippedResponses.decrementAndGet();
                                continue;
                            }

                            queue.add(value);
                        }
                    }
                    catch (InterruptedIOException e)
                    {
                        LOGGER.info("Receiver interrupted");
                        break;
                    }
                    catch (ClosedByInterruptException e)
                    {
                        LOGGER.info("Receiver closed by interrupt");
                        break;
                    }
                    catch (IOException e)
                    {
                        LOGGER.error("Connection error", e);

                        disconnect();
                        break;
                    }
                }
            }
        });
    }
private void startReceiver ( ) { if ( receiver != null ) { return ; } receiver = receiverExecutor . submit ( new Runnable ( ) { @ Override public void run ( ) { while ( true ) { try { if ( readSelector . select ( ) != 1 || Thread . interrupted ( ) ) { break ; } readSelector . selectedKeys ( ) . clear ( ) ; for ( String value : packet . read ( socket ) ) { LOGGER . trace ( "Received message: {}" , value ) ; if ( interceptor != null && interceptor . intercept ( value ) ) { continue ; } if ( skippedResponses . get ( ) > 0 ) { LOGGER . trace ( "Discarding skipped message: {}" , value ) ; skippedResponses . decrementAndGet ( ) ; continue ; } queue . add ( value ) ; } } catch ( InterruptedIOException e ) { LOGGER . info ( "Receiver interrupted" ) ; break ; } catch ( ClosedByInterruptException e ) { LOGGER . info ( "Receiver closed by interrupt" ) ; break ; } catch ( IOException e ) { LOGGER . error ( "Connection error" , e ) ; disconnect ( ) ; break ; } } } } ) ; }
Start the receiver thread. This thread will wait for data to arrive from
the connected server and deal with it (as either a response or an
unsolicited message). The receiver must be started before communication
can proceed, but it should be {@link #stopReceiver() stopped} before
redirecting the channel to prevent corruption.