List of usage examples for java.nio.channels ServerSocketChannel setOption
public abstract <T> ServerSocketChannel setOption(SocketOption<T> name, T value) throws IOException;
From source file:com.byteatebit.nbserver.simple.tcp.TcpConnectorFactory.java
@Override public IConnector create(ISelectorRegistrarBalancer selectorRegistrarBalancer, IComputeTaskScheduler taskScheduler, SocketAddress listenAddress) throws IOException { Preconditions.checkNotNull(selectorRegistrarBalancer, "selectorRegistrarBalancer cannot be null"); Preconditions.checkNotNull(taskScheduler, "taskScheduler cannot be null"); Preconditions.checkNotNull(listenAddress, "listenAddress cannot be null"); // open the server socketChannel ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); IOTimeoutTask timeoutTask = (selectionKey, ops) -> { LOG.error("selectionKey timeout"); selectionKey.cancel();//from w ww .j a v a 2 s . c om IOUtils.closeQuietly(selectionKey.channel()); }; for (SocketOptionValue socketOptionValue : socketOptionValues) serverSocketChannel.setOption(socketOptionValue.getOption(), socketOptionValue.getValue()); SelectionKey serverSocketSelectionKey = selectorRegistrarBalancer.getSelectorRegistrar().register( serverSocketChannel, SelectionKey.OP_ACCEPT, selectionKey -> connectHandler.accept( new NbContext(selectorRegistrarBalancer.getSelectorRegistrar(), taskScheduler), selectionKey, serverSocketChannel), timeoutTask, -1); serverSocketChannel.socket().bind(listenAddress, maxServerSocketBacklog); return new TcpConnector(serverSocketSelectionKey, serverSocketChannel); }
From source file:org.apache.nifi.io.nio.ChannelListener.java
/** * Adds a server socket channel for listening to connections. * * @param nicIPAddress - if null binds to wildcard address * @param port - port to bind to/* ww w. j av a2 s. c om*/ * @param receiveBufferSize - size of OS receive buffer to request. If less * than 0 then will not be set and OS default will win. * @throws IOException if unable to add socket */ public void addServerSocket(final InetAddress nicIPAddress, final int port, final int receiveBufferSize) throws IOException { final ServerSocketChannel ssChannel = ServerSocketChannel.open(); ssChannel.configureBlocking(false); if (receiveBufferSize > 0) { ssChannel.setOption(StandardSocketOptions.SO_RCVBUF, receiveBufferSize); final int actualReceiveBufSize = ssChannel.getOption(StandardSocketOptions.SO_RCVBUF); if (actualReceiveBufSize < receiveBufferSize) { LOGGER.warn(this + " attempted to set TCP Receive Buffer Size to " + receiveBufferSize + " bytes but could only set to " + actualReceiveBufSize + "bytes. You may want to consider changing the Operating System's " + "maximum receive buffer"); } } ssChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); ssChannel.bind(new InetSocketAddress(nicIPAddress, port)); ssChannel.register(serverSocketSelector, SelectionKey.OP_ACCEPT); }
From source file:org.apache.nifi.processor.util.listen.dispatcher.SocketChannelDispatcher.java
@Override public void open(final InetAddress nicAddress, final int port, final int maxBufferSize) throws IOException { stopped = false;//from w w w. j a v a 2s. c om executor = Executors.newFixedThreadPool(maxConnections); final ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); if (maxBufferSize > 0) { serverSocketChannel.setOption(StandardSocketOptions.SO_RCVBUF, maxBufferSize); final int actualReceiveBufSize = serverSocketChannel.getOption(StandardSocketOptions.SO_RCVBUF); if (actualReceiveBufSize < maxBufferSize) { logger.warn("Attempted to set Socket Buffer Size to " + maxBufferSize + " bytes but could only set to " + actualReceiveBufSize + "bytes. You may want to consider changing the Operating System's " + "maximum receive buffer"); } } serverSocketChannel.socket().bind(new InetSocketAddress(nicAddress, port)); selector = Selector.open(); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); }