Search in sources :

Example 86 with Selector

use of java.nio.channels.Selector in project Aeron by real-logic.

the class ClusterNetworkTopologyTest method shouldGetEchoFromCluster.

@ParameterizedTest
@MethodSource("provideTopologyConfigurations")
@InterruptAfter(60)
void shouldGetEchoFromCluster(final List<String> hostnames, final List<String> internalHostnames, final String ingressChannel, final String logChannel) throws Exception {
    assertNotNull(hostnames);
    assertEquals(3, hostnames.size());
    setupDataCollection(3);
    final String ingressEndpoints = ingressChannel.contains("endpoint") ? null : BasicAuctionClusterClient.ingressEndpoints(hostnames);
    try (RemoteLaunchClient remote0 = RemoteLaunchClient.connect(hostnames.get(0), REMOTE_LAUNCH_PORT);
        RemoteLaunchClient remote1 = RemoteLaunchClient.connect(hostnames.get(1), REMOTE_LAUNCH_PORT);
        RemoteLaunchClient remote2 = RemoteLaunchClient.connect(hostnames.get(2), REMOTE_LAUNCH_PORT)) {
        final Selector selector = Selector.open();
        launchNode(hostnames, internalHostnames, ingressChannel, logChannel, remote0, selector, 0);
        launchNode(hostnames, internalHostnames, ingressChannel, logChannel, remote1, selector, 1);
        launchNode(hostnames, internalHostnames, ingressChannel, logChannel, remote2, selector, 2);
        connectAndSendMessages(ingressChannel, ingressEndpoints, selector, 1);
    }
}
Also used : RemoteLaunchClient(io.aeron.test.launcher.RemoteLaunchClient) Selector(java.nio.channels.Selector) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 87 with Selector

use of java.nio.channels.Selector in project ignite by apache.

the class GridNioServer method createSelector.

/**
 * Creates selector and binds server socket to a given address and port. If address is null
 * then will not bind any address and just creates a selector.
 *
 * @param addr Local address to listen on.
 * @return Created selector.
 * @throws IgniteCheckedException If selector could not be created or port is already in use.
 */
private Selector createSelector(@Nullable SocketAddress addr) throws IgniteCheckedException {
    Selector selector = null;
    ServerSocketChannel srvrCh = null;
    try {
        // Create a new selector
        selector = SelectorProvider.provider().openSelector();
        if (addr != null) {
            // Create a new non-blocking server socket channel
            srvrCh = ServerSocketChannel.open();
            srvrCh.configureBlocking(false);
            if (sockRcvBuf > 0)
                srvrCh.socket().setReceiveBufferSize(sockRcvBuf);
            // Bind the server socket to the specified address and port
            srvrCh.socket().bind(addr);
            // Register the server socket channel, indicating an interest in
            // accepting new connections
            srvrCh.register(selector, SelectionKey.OP_ACCEPT);
        }
        return selector;
    } catch (Throwable e) {
        U.close(srvrCh, log);
        U.close(selector, log);
        if (e instanceof Error)
            throw (Error) e;
        throw new IgniteCheckedException("Failed to initialize NIO selector.", e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 88 with Selector

use of java.nio.channels.Selector in project rabbitmq-java-client by rabbitmq.

the class NioLoop method run.

@Override
public void run() {
    final SelectorHolder selectorState = context.readSelectorState;
    final Selector selector = selectorState.selector;
    final Set<SocketChannelRegistration> registrations = selectorState.registrations;
    final ByteBuffer buffer = context.readBuffer;
    final SelectorHolder writeSelectorState = context.writeSelectorState;
    final Selector writeSelector = writeSelectorState.selector;
    final Set<SocketChannelRegistration> writeRegistrations = writeSelectorState.registrations;
    // whether there have been write registrations in the previous loop
    // registrations are done after Selector.select(), to work on clean keys
    // thus, any write operation is performed in the next loop
    // we don't want to wait in the read Selector.select() if there are
    // pending writes
    boolean writeRegistered = false;
    try {
        while (!Thread.currentThread().isInterrupted()) {
            for (SelectionKey selectionKey : selector.keys()) {
                SocketChannelFrameHandlerState state = (SocketChannelFrameHandlerState) selectionKey.attachment();
                if (state.getConnection() != null && state.getConnection().getHeartbeat() > 0) {
                    long now = System.currentTimeMillis();
                    if ((now - state.getLastActivity()) > state.getConnection().getHeartbeat() * 1000 * 2) {
                        try {
                            handleHeartbeatFailure(state);
                        } catch (Exception e) {
                            LOGGER.warn("Error after heartbeat failure of connection {}", state.getConnection());
                        } finally {
                            selectionKey.cancel();
                        }
                    }
                }
            }
            int select;
            if (!writeRegistered && registrations.isEmpty() && writeRegistrations.isEmpty()) {
                // we can block, registrations will call Selector.wakeup()
                select = selector.select(1000);
                if (selector.keys().size() == 0) {
                    // we haven't been doing anything for a while, shutdown state
                    boolean clean = context.cleanUp();
                    if (clean) {
                        // we stop this thread
                        return;
                    }
                // there may be incoming connections, keep going
                }
            } else {
                // we don't have to block, we need to select and clean cancelled keys before registration
                select = selector.selectNow();
            }
            writeRegistered = false;
            // registrations should be done after select,
            // once the cancelled keys have been actually removed
            SocketChannelRegistration registration;
            Iterator<SocketChannelRegistration> registrationIterator = registrations.iterator();
            while (registrationIterator.hasNext()) {
                registration = registrationIterator.next();
                registrationIterator.remove();
                int operations = registration.operations;
                try {
                    if (registration.state.getChannel().isOpen()) {
                        registration.state.getChannel().register(selector, operations, registration.state);
                    }
                } catch (Exception e) {
                    // can happen if the channel has been closed since the operation has been enqueued
                    LOGGER.info("Error while registering socket channel for read: {}", e.getMessage());
                }
            }
            if (select > 0) {
                Set<SelectionKey> readyKeys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = readyKeys.iterator();
                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    iterator.remove();
                    if (!key.isValid()) {
                        continue;
                    }
                    if (key.isReadable()) {
                        final SocketChannelFrameHandlerState state = (SocketChannelFrameHandlerState) key.attachment();
                        try {
                            if (!state.getChannel().isOpen()) {
                                key.cancel();
                                continue;
                            }
                            if (state.getConnection() == null) {
                                // let's wait a bit more
                                continue;
                            }
                            state.prepareForReadSequence();
                            while (state.continueReading()) {
                                final Frame frame = state.frameBuilder.readFrame();
                                if (frame != null) {
                                    try {
                                        boolean noProblem = state.getConnection().handleReadFrame(frame);
                                        if (noProblem && (!state.getConnection().isRunning() || state.getConnection().hasBrokerInitiatedShutdown())) {
                                            // looks like the frame was Close-Ok or Close
                                            dispatchShutdownToConnection(state);
                                            key.cancel();
                                            break;
                                        }
                                    } catch (Throwable ex) {
                                        // problem during frame processing, tell connection, and
                                        // we can stop for this channel
                                        handleIoError(state, ex);
                                        key.cancel();
                                        break;
                                    }
                                }
                            }
                            state.setLastActivity(System.currentTimeMillis());
                        } catch (final Exception e) {
                            LOGGER.warn("Error during reading frames", e);
                            handleIoError(state, e);
                            key.cancel();
                        } finally {
                            buffer.clear();
                        }
                    }
                }
            }
            // write loop
            select = writeSelector.selectNow();
            // registrations should be done after select,
            // once the cancelled keys have been actually removed
            SocketChannelRegistration writeRegistration;
            Iterator<SocketChannelRegistration> writeRegistrationIterator = writeRegistrations.iterator();
            while (writeRegistrationIterator.hasNext()) {
                writeRegistration = writeRegistrationIterator.next();
                writeRegistrationIterator.remove();
                int operations = writeRegistration.operations;
                try {
                    if (writeRegistration.state.getChannel().isOpen()) {
                        writeRegistration.state.getChannel().register(writeSelector, operations, writeRegistration.state);
                        writeRegistered = true;
                    }
                } catch (Exception e) {
                    // can happen if the channel has been closed since the operation has been enqueued
                    LOGGER.info("Error while registering socket channel for write: {}", e.getMessage());
                }
            }
            if (select > 0) {
                Set<SelectionKey> readyKeys = writeSelector.selectedKeys();
                Iterator<SelectionKey> iterator = readyKeys.iterator();
                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    iterator.remove();
                    SocketChannelFrameHandlerState state = (SocketChannelFrameHandlerState) key.attachment();
                    if (!key.isValid()) {
                        continue;
                    }
                    if (key.isWritable()) {
                        boolean cancelKey = true;
                        try {
                            if (!state.getChannel().isOpen()) {
                                key.cancel();
                                continue;
                            }
                            state.prepareForWriteSequence();
                            int toBeWritten = state.getWriteQueue().size();
                            int written = 0;
                            DataOutputStream outputStream = state.outputStream;
                            WriteRequest request;
                            while (written <= toBeWritten && (request = state.getWriteQueue().poll()) != null) {
                                request.handle(outputStream);
                                written++;
                            }
                            outputStream.flush();
                            if (!state.getWriteQueue().isEmpty()) {
                                cancelKey = true;
                            }
                        } catch (Exception e) {
                            handleIoError(state, e);
                        } finally {
                            state.endWriteSequence();
                            if (cancelKey) {
                                key.cancel();
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("Error in NIO loop", e);
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) Frame(com.rabbitmq.client.impl.Frame) DataOutputStream(java.io.DataOutputStream) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) Selector(java.nio.channels.Selector)

Example 89 with Selector

use of java.nio.channels.Selector in project baseio by generallycloud.

the class JavaEventLoop method open_selector.

@SuppressWarnings("rawtypes")
private static Selector open_selector(final SelectionKeySet keySet) throws IOException {
    final SelectorProvider provider = SelectorProvider.provider();
    final Selector selector = provider.openSelector();
    Object res = AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override
        public Object run() {
            try {
                return Class.forName("sun.nio.ch.SelectorImpl");
            } catch (Throwable cause) {
                return cause;
            }
        }
    });
    if (res instanceof Throwable) {
        return selector;
    }
    final Class selectorImplClass = (Class) res;
    res = AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override
        public Object run() {
            try {
                Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
                Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");
                Throwable cause = Util.trySetAccessible(selectedKeysField);
                if (cause != null) {
                    return cause;
                }
                cause = Util.trySetAccessible(publicSelectedKeysField);
                if (cause != null) {
                    return cause;
                }
                selectedKeysField.set(selector, keySet);
                publicSelectedKeysField.set(selector, keySet);
                return null;
            } catch (Throwable e) {
                return e;
            }
        }
    });
    if (res instanceof Throwable) {
        return selector;
    }
    return selector;
}
Also used : Field(java.lang.reflect.Field) SelectorProvider(java.nio.channels.spi.SelectorProvider) PrivilegedAction(java.security.PrivilegedAction) Selector(java.nio.channels.Selector)

Example 90 with Selector

use of java.nio.channels.Selector in project baseio by generallycloud.

the class NIOClientDemo method main.

public static void main(String[] args) throws IOException {
    // 打开socket通道
    SocketChannel socketChannel = SocketChannel.open();
    // 设置为非阻塞方式
    socketChannel.configureBlocking(false);
    // 打开选择器
    Selector selector = Selector.open();
    // 注册连接服务端socket动作
    socketChannel.register(selector, SelectionKey.OP_CONNECT);
    // 连接
    socketChannel.connect(SERVER_ADDRESS);
    // 分配缓冲区大小内存
    Set<SelectionKey> selectionKeys;
    Iterator<SelectionKey> iterator;
    SelectionKey selectionKey;
    SocketChannel client;
    String receiveText;
    String sendText;
    int count = 0;
    while (true) {
        // 选择一组键,其相应的通道已为 I/O 操作准备就绪。
        // 此方法执行处于阻塞模式的选择操作。
        selector.select();
        // 返回此选择器的已选择键集。
        selectionKeys = selector.selectedKeys();
        // System.out.println(selectionKeys.size());
        iterator = selectionKeys.iterator();
        while (iterator.hasNext()) {
            selectionKey = iterator.next();
            if (selectionKey.isConnectable()) {
                System.out.println("client connect");
                client = (SocketChannel) selectionKey.channel();
                // 完成套接字通道的连接过程。
                if (client.isConnectionPending()) {
                    client.finishConnect();
                    System.out.println("完成连接!");
                    sendbuffer.clear();
                    sendbuffer.put("Hello,Server".getBytes());
                    sendbuffer.flip();
                    client.write(sendbuffer);
                }
                client.register(selector, SelectionKey.OP_READ);
            } else if (selectionKey.isReadable()) {
                client = (SocketChannel) selectionKey.channel();
                // 将缓冲区清空以备下次读取
                receivebuffer.clear();
                // 读取服务器发送来的数据到缓冲区中
                count = client.read(receivebuffer);
                if (count > 0) {
                    receiveText = new String(receivebuffer.array(), 0, count);
                    System.out.println("客户端接受服务器端数据--:" + receiveText);
                    client.register(selector, SelectionKey.OP_WRITE);
                }
            } else if (selectionKey.isWritable()) {
                sendbuffer.clear();
                client = (SocketChannel) selectionKey.channel();
                sendText = "message from client--" + (flag++);
                sendbuffer.put(sendText.getBytes());
                // 将缓冲区各标志复位,因为向里面put了数据标志被改变要想从中读取数据发向服务器,就要复位
                sendbuffer.flip();
                client.write(sendbuffer);
                System.out.println("客户端向服务器端发送数据--:" + sendText);
                client.register(selector, SelectionKey.OP_READ);
            }
        }
        selectionKeys.clear();
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) Selector(java.nio.channels.Selector)

Aggregations

Selector (java.nio.channels.Selector)189 SelectionKey (java.nio.channels.SelectionKey)85 IOException (java.io.IOException)71 SocketChannel (java.nio.channels.SocketChannel)52 InetSocketAddress (java.net.InetSocketAddress)38 ByteBuffer (java.nio.ByteBuffer)29 ServerSocketChannel (java.nio.channels.ServerSocketChannel)27 Test (org.junit.Test)14 DatagramChannel (java.nio.channels.DatagramChannel)13 ClosedChannelException (java.nio.channels.ClosedChannelException)12 CancelledKeyException (java.nio.channels.CancelledKeyException)11 NioEndpoint (org.apache.tomcat.util.net.NioEndpoint)10 ClosedSelectorException (java.nio.channels.ClosedSelectorException)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 EOFException (java.io.EOFException)7 ServerSocket (java.net.ServerSocket)7 SelectableChannel (java.nio.channels.SelectableChannel)7 RemoteLaunchClient (io.aeron.test.launcher.RemoteLaunchClient)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 MethodSource (org.junit.jupiter.params.provider.MethodSource)6