Java Code Examples for java.nio.channels.SelectableChannel#configureBlocking()
The following examples show how to use
java.nio.channels.SelectableChannel#configureBlocking() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SelectorTest.java From incubator-tuweni with Apache License 2.0 | 6 votes |
@Test void selectorRemovesKeysOnChannelCloseWhenSelecting() throws Exception { Pipe pipe = Pipe.open(); Selector selector = Selector.open(); SelectableChannel source = pipe.source(); source.configureBlocking(false); SelectionKey key = source.register(selector, OP_READ); assertTrue(selector.keys().contains(key)); source.close(); assertTrue(selector.keys().contains(key)); selector.selectNow(); assertFalse(selector.keys().contains(key)); }
Example 2
Source File: SelectorTest.java From cava with Apache License 2.0 | 6 votes |
@Test void cancelledKeyRemovedFromChannel() throws Exception { Pipe pipe = Pipe.open(); Selector selector = Selector.open(); SelectableChannel source = pipe.source(); source.configureBlocking(false); for (int i = 0; i < 1000; ++i) { assertNull(source.keyFor(selector)); SelectionKey key = source.register(selector, OP_READ); selector.selectedKeys().clear(); selector.selectNow(); key.cancel(); selector.wakeup(); selector.selectedKeys().clear(); selector.selectNow(); } }
Example 3
Source File: SelectorTest.java From cava with Apache License 2.0 | 6 votes |
@Test void selectorRemovesKeysOnCancelWhenSelecting() throws Exception { Pipe pipe = Pipe.open(); Selector selector = Selector.open(); SelectableChannel source = pipe.source(); source.configureBlocking(false); SelectionKey key = source.register(selector, OP_READ); assertTrue(selector.keys().contains(key)); key.cancel(); assertTrue(selector.keys().contains(key)); assertSame(key, source.keyFor(selector)); selector.selectNow(); assertFalse(selector.keys().contains(key)); assertNull(source.keyFor(selector)); }
Example 4
Source File: SelectorTest.java From cava with Apache License 2.0 | 6 votes |
@Test void selectorRemovesKeysOnChannelCloseWhenSelecting() throws Exception { Pipe pipe = Pipe.open(); Selector selector = Selector.open(); SelectableChannel source = pipe.source(); source.configureBlocking(false); SelectionKey key = source.register(selector, OP_READ); assertTrue(selector.keys().contains(key)); source.close(); assertTrue(selector.keys().contains(key)); selector.selectNow(); assertFalse(selector.keys().contains(key)); }
Example 5
Source File: Server.java From TarsJava with BSD 3-Clause "New" or "Revised" License | 6 votes |
protected void startNIOServer() throws Exception { SelectableChannel server = null; int interestKey; //1. Start reactor service selectorManager.start(); //2. Start server on the specified port if (this.udpMode) { server = DatagramChannel.open(); ((DatagramChannel) server).socket().bind(new InetSocketAddress(host, port)); interestKey = SelectionKey.OP_READ; } else { server = ServerSocketChannel.open(); ((ServerSocketChannel) server).socket().bind(new InetSocketAddress(host, port), 1024); interestKey = SelectionKey.OP_ACCEPT; } server.configureBlocking(false); //3. Choose one reactor to handle NIO event selectorManager.getReactor(0).registerChannel(server, interestKey); System.out.println("INFO: NAMI Server started on port " + String.valueOf(port) + "..."); }
Example 6
Source File: SelectorTest.java From incubator-tuweni with Apache License 2.0 | 6 votes |
@Test void cancelledKeyRemovedFromChannel() throws Exception { Pipe pipe = Pipe.open(); Selector selector = Selector.open(); SelectableChannel source = pipe.source(); source.configureBlocking(false); for (int i = 0; i < 1000; ++i) { assertNull(source.keyFor(selector)); SelectionKey key = source.register(selector, OP_READ); selector.selectedKeys().clear(); selector.selectNow(); key.cancel(); selector.wakeup(); selector.selectedKeys().clear(); selector.selectNow(); } }
Example 7
Source File: SelectorTest.java From incubator-tuweni with Apache License 2.0 | 6 votes |
@Test void selectorRemovesKeysOnCancelWhenSelecting() throws Exception { Pipe pipe = Pipe.open(); Selector selector = Selector.open(); SelectableChannel source = pipe.source(); source.configureBlocking(false); SelectionKey key = source.register(selector, OP_READ); assertTrue(selector.keys().contains(key)); key.cancel(); assertTrue(selector.keys().contains(key)); assertSame(key, source.keyFor(selector)); selector.selectNow(); assertFalse(selector.keys().contains(key)); assertNull(source.keyFor(selector)); }
Example 8
Source File: AbstractNioChannel.java From netty4.0.27Learn with Apache License 2.0 | 6 votes |
/** * Create a new instance * * @param parent the parent {@link Channel} by which this instance was created. May be {@code null} * @param ch the underlying {@link SelectableChannel} on which it operates * @param readInterestOp the ops to set to receive data from the {@link SelectableChannel} */ protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) { super(parent); this.ch = ch; this.readInterestOp = readInterestOp; try { ch.configureBlocking(false); } catch (IOException e) { try { ch.close(); } catch (IOException e2) { if (logger.isWarnEnabled()) { logger.warn( "Failed to close a partially initialized socket.", e2); } } throw new ChannelException("Failed to enter non-blocking mode.", e); } }
Example 9
Source File: SocketIOWithTimeout.java From RDFS with Apache License 2.0 | 5 votes |
SocketIOWithTimeout(SelectableChannel channel, long timeout) throws IOException { checkChannelValidity(channel); this.channel = channel; this.timeout = timeout; // Set non-blocking channel.configureBlocking(false); }
Example 10
Source File: NioReceiver.java From tomcatsrc with Apache License 2.0 | 5 votes |
/** * Register the given channel with the given selector for * the given operations of interest */ protected void registerChannel(Selector selector, SelectableChannel channel, int ops, Object attach) throws Exception { if (channel == null)return; // could happen // set the new channel non-blocking channel.configureBlocking(false); // register it with the selector channel.register(selector, ops, attach); }
Example 11
Source File: SelectorTest.java From cava with Apache License 2.0 | 5 votes |
@Test void selectorRemovesKeysOnChannelCloseWhileSelecting() throws Exception { Pipe pipe = Pipe.open(); Selector selector = Selector.open(); SelectableChannel source = pipe.source(); source.configureBlocking(false); SelectionKey key = source.register(selector, OP_READ); assertTrue(selector.keys().contains(key)); CountDownLatch latch = new CountDownLatch(1); Future<?> job = executor.submit(() -> { latch.countDown(); try { selector.select(); } catch (IOException e) { throw new UncheckedIOException(e); } }); latch.await(); Thread.sleep(100); source.close(); selector.wakeup(); job.get(); assertFalse(selector.keys().contains(key)); }
Example 12
Source File: SelectorTest.java From incubator-tuweni with Apache License 2.0 | 5 votes |
@Test void selectorRemovesKeysOnChannelCloseWhileSelecting() throws Exception { Pipe pipe = Pipe.open(); Selector selector = Selector.open(); SelectableChannel source = pipe.source(); source.configureBlocking(false); SelectionKey key = source.register(selector, OP_READ); assertTrue(selector.keys().contains(key)); CountDownLatch latch = new CountDownLatch(1); Future<?> job = executor.submit(() -> { latch.countDown(); try { selector.select(); } catch (IOException e) { throw new UncheckedIOException(e); } }); latch.await(); Thread.sleep(100); source.close(); selector.wakeup(); job.get(); assertFalse(selector.keys().contains(key)); }
Example 13
Source File: SocketIOWithTimeout.java From stratosphere with Apache License 2.0 | 5 votes |
SocketIOWithTimeout(SelectableChannel channel, long timeout) throws IOException { checkChannelValidity(channel); this.channel = channel; this.timeout = timeout; // Set non-blocking channel.configureBlocking(false); }
Example 14
Source File: NioReceiver.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
/** * Register the given channel with the given selector for * the given operations of interest */ protected void registerChannel(Selector selector, SelectableChannel channel, int ops, Object attach) throws Exception { if (channel == null)return; // could happen // set the new channel non-blocking channel.configureBlocking(false); // register it with the selector channel.register(selector, ops, attach); }
Example 15
Source File: SocketIOWithTimeout.java From hadoop with Apache License 2.0 | 5 votes |
SocketIOWithTimeout(SelectableChannel channel, long timeout) throws IOException { checkChannelValidity(channel); this.channel = channel; this.timeout = timeout; // Set non-blocking channel.configureBlocking(false); }
Example 16
Source File: ServerConnection.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Switch this guy to blocking mode so we can use oldIO to read and write msgs. */ public void makeBlocking() throws IOException { //logger.info("DEBUG: makeBlocking " + this); // if (this.sKey != null) { // this.sKey = null; // } SelectableChannel c = this.theSocket.getChannel(); c.configureBlocking(true); }
Example 17
Source File: SocketIOWithTimeout.java From hadoop-gpu with Apache License 2.0 | 5 votes |
SocketIOWithTimeout(SelectableChannel channel, long timeout) throws IOException { checkChannelValidity(channel); this.channel = channel; this.timeout = timeout; // Set non-blocking channel.configureBlocking(false); }
Example 18
Source File: Client.java From TarsJava with BSD 3-Clause "New" or "Revised" License | 4 votes |
protected synchronized void reConnect() throws IOException { //0. Don't send connect request if it is connecting. if (isNotConnected()) { SocketAddress server = new InetSocketAddress(this.host, this.port); SelectableChannel channel = null; Session session2 = null; int event; if (this.udpMode) { //1. Create socket channel channel = DatagramChannel.open(); channel.configureBlocking(false); //2. Create NioSession for each client connection session2 = new UDPSession(this.selectorManager); ((UDPSession) session2).setBufferSize(bufferSize); ((UDPSession) session2).setTarget(server); event = SelectionKey.OP_READ; session2.setStatus(SessionStatus.CLIENT_CONNECTED); } else { //1. Create socket channel channel = SocketChannel.open(); channel.configureBlocking(false); try { if (this.tc != INVALID_TRAFFIC_CLASS_VALUE) ((SocketChannel) channel).socket().setTrafficClass(this.tc); } catch (Exception ex) { ex.printStackTrace(); } ((SocketChannel) channel).connect(server); //2. Create NioSession for each client connection session2 = new TCPSession(this.selectorManager); ((TCPSession) session2).setTcpNoDelay(this.tcpNoDelay); event = SelectionKey.OP_CONNECT; } session2.setChannel(channel); session2.setKeepAlive(selectorManager.isKeepAlive()); //3. Register event this.selectorManager.nextReactor().registerChannel(channel, event, session2); if (!this.udpMode) { //4. Wait to connect if (!session2.waitToConnect(this.connectTimeout)) { session2.asyncClose(); throw new IOException("connect timed out to " + this.host + ":" + this.port); } //5. Handle exception if (session2.getStatus() == SessionStatus.NOT_CONNECTED) { session2.asyncClose(); throw new IOException("connect failed to " + this.host + ":" + this.port); } else if (session2.getStatus() == SessionStatus.CLOSED) { //Already closed throw new IOException("connect failed to " + this.host + ":" + this.port); //Please see stderr.log for more details. } } this.session = session2; } }
Example 19
Source File: ServantClient.java From TarsJava with BSD 3-Clause "New" or "Revised" License | 4 votes |
protected synchronized void reConnect() throws IOException { if (isNotConnected()) { SocketAddress server = new InetSocketAddress(this.host, this.port); SelectableChannel channel = null; Session temp = null; int event; if (this.udpMode) { channel = DatagramChannel.open(); channel.configureBlocking(false); temp = new UDPSession(this.selectorManager); ((UDPSession) temp).setBufferSize(bufferSize); ((UDPSession) temp).setTarget(server); event = SelectionKey.OP_READ; temp.setStatus(SessionStatus.CLIENT_CONNECTED); } else { channel = SocketChannel.open(); channel.configureBlocking(false); try { if (this.tc != INVALID_TRAFFIC_CLASS_VALUE) { ((SocketChannel) channel).socket().setTrafficClass(this.tc); } } catch (Exception ex) { logger.error(ex.getLocalizedMessage()); } ((SocketChannel) channel).connect(server); temp = new TCPSession(this.selectorManager); ((TCPSession) temp).setTcpNoDelay(this.tcpNoDelay); event = SelectionKey.OP_CONNECT; } temp.setChannel(channel); temp.setKeepAlive(selectorManager.isKeepAlive()); this.selectorManager.nextReactor().registerChannel(channel, event, temp); if (!this.udpMode) { if (!temp.waitToConnect(this.connectTimeout)) { temp.asyncClose(); throw new TimeoutException("connect " + this.connectTimeout + "ms timed out to " + this.getAddress()); } if (temp.getStatus() == SessionStatus.NOT_CONNECTED) { temp.asyncClose(); throw new NotConnectedException("connect failed to " + this.getAddress()); } else if (temp.getStatus() == SessionStatus.CLOSED) { throw new NotConnectedException("connect failed to " + this.getAddress()); } } this.session = temp; } }
Example 20
Source File: NioProcessor.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override protected void init(NioSession session) throws Exception { SelectableChannel ch = (SelectableChannel) session.getChannel(); ch.configureBlocking(false); session.setSelectionKey(ch.register(selector, SelectionKey.OP_READ, session)); }