/** * Release all the previous created channels. * This takes care of calling {@link LocalChannelRegistry#unregister(LocalAddress)} for each of them. */ public void releaseExternalResources() { group.close().awaitUninterruptibly(); }
void registerChannel(Channel channel) { allChannels.add(channel); }
/** * * @return The number of clients currently connected to this server. */ public int getNumActiveConnections() { //allChannels also contains the server channel, so exclude that from the //count. return allChannels.size() - 1; }
@Override public void close() { ChannelGroupFuture future = allChannels.close(); future.awaitUninterruptibly(); channelFactory.releaseExternalResources(); closed.countDown(); }
private CanalServerWithNetty(){ this.embeddedServer = CanalServerWithEmbedded.instance(); this.childGroups = new DefaultChannelGroup(); }
public LocalServerChannel newChannel(ChannelPipeline pipeline) { LocalServerChannel channel = new DefaultLocalServerChannel(this, pipeline, sink); group.add(channel); return channel; }
@Override public void run() { try { // await(5, TimeUnit.SECONDS) // sometimes allChannels.close() will block the exit thread allChannels.close().await(1, TimeUnit.SECONDS); LOG.info("Successfully close all channel"); factory.releaseExternalResources(); } catch (Exception ignored) { } allChannels = null; } }).start();
public void operationComplete(ChannelFuture future) throws Exception { remove(future.getChannel()); } };
public boolean await(long timeout, TimeUnit unit) throws InterruptedException { return await0(unit.toNanos(timeout), true); }
private void notifyListeners() { // This method doesn't need synchronization because: // 1) This method is always called after synchronized (this) block. // Hence any listener list modification happens-before this method. // 2) This method is called only when 'done' is true. Once 'done' // becomes true, the listener list is never modified - see add/removeListener() if (firstListener != null) { notifyListener(firstListener); firstListener = null; if (otherListeners != null) { for (ChannelGroupFutureListener l: otherListeners) { notifyListener(l); } otherListeners = null; } } }
boolean setDone() { synchronized (this) { // Allow only once. if (done) { return false; } done = true; if (waiters > 0) { notifyAll(); } } notifyListeners(); return true; }
/** * Closes the Netty Channel and releases all resources */ @Override protected void stopInner() { /* * TODO REST-Server Need to handle inflight operations. What happens to * the existing async operations when a channel.close() is issued in * Netty? */ if(this.nettyServerChannel != null) { this.nettyServerChannel.close(); } if(allChannels != null) { allChannels.close().awaitUninterruptibly(); } this.bootstrap.releaseExternalResources(); }
@Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { allChannels.add(e.getChannel()); super.channelOpen(ctx, e); }
public void stopServer() throws Exception { LOG.debug("In stopServer method of class: {}", this.getClass().getName()); ChannelGroupFuture future = ALL_CHANNELS.close(); try { future.await(); } catch (InterruptedException e) { LOG.error("Execption occurred while waiting for channels to close: {}",e); } super.stopServer(); }
public synchronized boolean add(Channel channel) { if (channel != null) { if (channel.getRemoteAddress() != null) { channelMap.put(channel.getRemoteAddress().toString(), channel); } return super.add(channel); } else { return false; } }
public boolean await(long timeoutMillis) throws InterruptedException { return await0(MILLISECONDS.toNanos(timeoutMillis), true); }
public void stop() { super.stop(); if (this.serverChannel != null) { this.serverChannel.close().awaitUninterruptibly(1000); } // close sockets explicitly to reduce socket channel hung in complicated // network environment. if (this.childGroups != null) { this.childGroups.close().awaitUninterruptibly(5000); } if (this.bootstrap != null) { this.bootstrap.releaseExternalResources(); } if (embeddedServer.isStart()) { embeddedServer.stop(); } }
@Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { NettyTCPClient.ALL_CHANNELS.add(e.getChannel()); super.channelConnected(ctx, e); }