public void reconnect() throws Exception { if (channel != null && channel.isActive()) { return; } //首先清除路由信息,下线 routeRequest.offLine(); LOGGER.info("开始重连。。"); start(); LOGGER.info("重连成功!!"); }
protected boolean isActive() { return channel.isActive(); }
@Override public boolean isConnected() { return ch.isActive(); }
/** * Is connection ready for another command. */ @Override public boolean isValid(ByteBuffer notUsed) { return (System.nanoTime() - lastUsed) <= maxSocketIdle && channel.isActive(); }
/** * Is connection ready for another command. */ @Override public boolean isValid(ByteBuffer notUsed) { return (System.nanoTime() - lastUsed) <= maxSocketIdle && channel.isActive(); }
if (socketChannel != null && socketChannel.isActive()) { return;
/** * Connection consumer wants to close connection. Initiate connection close * and complete. This is a blocking call that ensures that the connection is * closed before returning. As part of this call, the channel close handler * will be triggered which will call channelClosed() above. The latter will * happen in a separate thread while this method is blocking. */ @Override public void close() { try { if (channel.isActive()) { channel.close().get(); } } catch (final InterruptedException | ExecutionException e) { logger.warn("Caught exception while closing channel.", e); // Preserve evidence that the interruption occurred so that code higher up // on the call stack can learn of the // interruption and respond to it if it wants to. Thread.currentThread().interrupt(); } }
public boolean isActive() { return connection != null && connection.getChannel() != null && connection.getChannel().isActive(); }
public void testShutdownOutputAfterClosed(Bootstrap cb) throws Throwable { TestHandler h = new TestHandler(); ServerSocket ss = new ServerSocket(); Socket s = null; try { ss.bind(newSocketAddress()); SocketChannel ch = (SocketChannel) cb.handler(h).connect(ss.getLocalSocketAddress()).sync().channel(); assertTrue(ch.isActive()); s = ss.accept(); ch.close().syncUninterruptibly(); try { ch.shutdownInput().syncUninterruptibly(); fail(); } catch (Throwable cause) { checkThrowable(cause); } try { ch.shutdownOutput().syncUninterruptibly(); fail(); } catch (Throwable cause) { checkThrowable(cause); } } finally { if (s != null) { s.close(); } ss.close(); } }
public void testShutdownOutput(ServerBootstrap sb) throws Throwable { TestHandler h = new TestHandler(); Socket s = new Socket(); Channel sc = null; try { sc = sb.childHandler(h).childOption(ChannelOption.ALLOW_HALF_CLOSURE, true).bind().sync().channel(); SocketUtils.connect(s, sc.localAddress(), 10000); s.getOutputStream().write(1); assertEquals(1, (int) h.queue.take()); assertTrue(h.ch.isOpen()); assertTrue(h.ch.isActive()); assertFalse(h.ch.isInputShutdown()); assertFalse(h.ch.isOutputShutdown()); s.shutdownOutput(); h.halfClosure.await(); assertTrue(h.ch.isOpen()); assertTrue(h.ch.isActive()); assertTrue(h.ch.isInputShutdown()); assertFalse(h.ch.isOutputShutdown()); assertEquals(1, h.closure.getCount()); Thread.sleep(100); assertEquals(1, h.halfClosureCount.intValue()); } finally { if (sc != null) { sc.close(); } s.close(); } }
public void testShutdownOutputWithoutOption(ServerBootstrap sb) throws Throwable { TestHandler h = new TestHandler(); Socket s = new Socket(); Channel sc = null; try { sc = sb.childHandler(h).bind().sync().channel(); SocketUtils.connect(s, sc.localAddress(), 10000); s.getOutputStream().write(1); assertEquals(1, (int) h.queue.take()); assertTrue(h.ch.isOpen()); assertTrue(h.ch.isActive()); assertFalse(h.ch.isInputShutdown()); assertFalse(h.ch.isOutputShutdown()); s.shutdownOutput(); h.closure.await(); assertFalse(h.ch.isOpen()); assertFalse(h.ch.isActive()); assertTrue(h.ch.isInputShutdown()); assertTrue(h.ch.isOutputShutdown()); assertEquals(1, h.halfClosure.getCount()); Thread.sleep(100); assertEquals(0, h.halfClosureCount.intValue()); } finally { if (sc != null) { sc.close(); } s.close(); } }
ss.bind(newSocketAddress()); ch = (SocketChannel) cb.handler(h).connect(ss.getLocalSocketAddress()).sync().channel(); assertTrue(ch.isActive()); assertFalse(ch.isOutputShutdown()); assertTrue(h.ch.isActive()); assertFalse(h.ch.isInputShutdown()); assertFalse(h.ch.isOutputShutdown()); assertTrue(h.ch.isActive()); assertFalse(h.ch.isInputShutdown()); assertTrue(h.ch.isOutputShutdown());
ch = (SocketChannel) cb.handler(h).connect(ss.getLocalSocketAddress()).sync().channel(); assumeFalse(ch instanceof OioSocketChannel); assertTrue(ch.isActive()); assertFalse(ch.isOutputShutdown()); assertTrue(h.ch.isActive()); assertFalse(h.ch.isInputShutdown()); assertFalse(h.ch.isOutputShutdown()); assertTrue(h.ch.isActive()); assertFalse(h.ch.isInputShutdown()); assertTrue(h.ch.isOutputShutdown());
/** * schedules the handshake initiation after the connection attempt */ private void scheduleHandshake( SocketChannel ch, HandshakeClient handshakeClient, TimeoutStrategy.Timeout handshakeDelay ) { ch.eventLoop().schedule( () -> { if ( ch.isActive() ) { initiateHandshake( ch, handshakeClient ); } else if ( ch.isOpen() ) { handshakeDelay.increment(); scheduleHandshake( ch, handshakeClient, handshakeDelay ); } else { handshakeClient.failIfNotDone( "Channel closed" ); } }, handshakeDelay.getMillis(), MILLISECONDS ); }