@Override public boolean add(Channel channel) { ConcurrentMap<Integer, Channel> map = channel instanceof ServerChannel? serverChannels : nonServerChannels; boolean added = map.putIfAbsent(channel.getId(), channel) == null; if (added) { channel.getCloseFuture().addListener(remover); } return added; }
/** * Sets the value of this variable only when no value was set. * * @return {@code null} if the specified value was set. * An existing value if failed to set. */ public T setIfAbsent(Channel channel, T value) { if (value == null) { return get(channel); } else { if (channel == null) { throw new NullPointerException("channel"); } T mapping = map.putIfAbsent(channel, value); if (removeOnClose && mapping == null) { channel.getCloseFuture().addListener(remover); } return mapping; } }
@Override public void stop() { logger.info("Netcat UDP Source stopping..."); logger.info("Metrics:{}", counterGroup); if (nettyChannel != null) { nettyChannel.close(); try { nettyChannel.getCloseFuture().await(60, TimeUnit.SECONDS); } catch (InterruptedException e) { logger.warn("netty server stop interrupted", e); } finally { nettyChannel = null; } } super.stop(); }
/** * Removes the variable and returns the removed value. If no value was set, * this method returns the return value of {@link #initialValue(Channel)}, * which is {@code null} by default. * * @return the removed value. * {@linkplain #initialValue(Channel) an initial value} (by default * {@code null}) if no value was set. */ public T remove(Channel channel) { if (channel == null) { throw new NullPointerException("channel"); } T removed = map.remove(channel); if (removed == null) { return initialValue(channel); } else { if (removeOnClose) { channel.getCloseFuture().removeListener(remover); } return removed; } }
/** * Sets the value of this variable. * * @return the old value. {@code null} if there was no old value. */ public T set(Channel channel, T value) { if (value == null) { return remove(channel); } else { if (channel == null) { throw new NullPointerException("channel"); } T old = map.put(channel, value); if (removeOnClose) { channel.getCloseFuture().addListener(remover); } return old; } }
@Override public boolean remove(Object o) { Channel c = null; if (o instanceof Integer) { c = nonServerChannels.remove(o); if (c == null) { c = serverChannels.remove(o); } } else if (o instanceof Channel) { c = (Channel) o; if (c instanceof ServerChannel) { c = serverChannels.remove(c.getId()); } else { c = nonServerChannels.remove(c.getId()); } } if (c == null) { return false; } c.getCloseFuture().removeListener(remover); return true; }
@Override public void stop() { logger.info("Syslog UDP Source stopping..."); logger.info("Metrics: {}", sourceCounter); if (nettyChannel != null) { nettyChannel.close(); try { nettyChannel.getCloseFuture().await(60, TimeUnit.SECONDS); } catch (InterruptedException e) { logger.warn("netty server stop interrupted", e); } finally { nettyChannel = null; } } sourceCounter.stop(); super.stop(); }
@Override public void stop() { logger.info("Syslog TCP Source stopping..."); logger.info("Metrics: {}", sourceCounter); if (nettyChannel != null) { nettyChannel.close(); try { nettyChannel.getCloseFuture().await(60, TimeUnit.SECONDS); } catch (InterruptedException e) { logger.warn("netty server stop interrupted", e); } finally { nettyChannel = null; } } sourceCounter.stop(); super.stop(); }
public Future<ServerInfoMessage> requestInfo(final String address, final int port) { return pool.submit(() -> { InetSocketAddress remoteAddress = new InetSocketAddress(address, port); ChannelFuture connectCheck = bootstrap.connect(remoteAddress); connectCheck.syncUninterruptibly(); Channel channel = connectCheck.getChannel(); channel.getCloseFuture().syncUninterruptibly(); ServerInfoRequestHandler handler = channel.getPipeline().get(ServerInfoRequestHandler.class); ServerInfoMessage serverInfo = handler.getServerInfo(); return serverInfo; }); }
/** * Sends a {@code "close"} request to the last * {@link ChannelDownstreamHandler} in the {@link ChannelPipeline} of * the specified {@link Channel}. * * @param channel the channel to close * * @return the {@link ChannelFuture} which will be notified on closure */ public static ChannelFuture close(Channel channel) { ChannelFuture future = channel.getCloseFuture(); channel.getPipeline().sendDownstream(new DownstreamChannelStateEvent( channel, future, ChannelState.OPEN, Boolean.FALSE)); return future; }
@Override protected Executor getChildExecutor(ChannelEvent e) { final Object key = getChildExecutorKey(e); Executor executor = childExecutors.get(key); if (executor == null) { executor = new ChildExecutor(); Executor oldExecutor = childExecutors.putIfAbsent(key, executor); if (oldExecutor != null) { executor = oldExecutor; } else { // register a listener so that the ChildExecutor will get removed once the channel was closed e.getChannel().getCloseFuture().addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { removeChildExecutor(key); } }); } } return executor; } }
} catch (InterruptedException e) { connectCheck.cancel(); connectCheck.getChannel().getCloseFuture().awaitUninterruptibly(); factory.releaseExternalResources(); throw e; connectCheck.getChannel().getCloseFuture().awaitUninterruptibly(); factory.releaseExternalResources(); return new JoinStatusImpl("Failed to connect to server - " + connectCheck.getCause().getMessage());
event.getChannel().getCloseFuture().addListener(new ChannelFutureListener() {
e.getChannel().getCloseFuture().addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { context.sendDownstream(e);
@Override public WebServer call() throws Exception { if (channel != null) { channel.close(); } if (connectionTrackingHandler != null) { connectionTrackingHandler.closeAllConnections(); connectionTrackingHandler = null; } if (bootstrap != null) { bootstrap.releaseExternalResources(); } // shut down all services & give them a chance to terminate for (ExecutorService executorService : executorServices) { shutdownAndAwaitTermination(executorService); } bootstrap = null; if (channel != null) { channel.getCloseFuture().await(); } return NettyWebServer.this; } });
public synchronized void awaitDisconnect() { if (channel == null) { throw new IllegalStateException("The connect() method were not invoked."); } ChannelFuture closeFuture = channel.getCloseFuture(); closeFuture.awaitUninterruptibly(); }
private static ChannelGroupFuture getChannelGroupCloseFuture(ChannelGroup g) { Collection<ChannelFuture> futures = new ArrayList<ChannelFuture>(g.size()); for (Channel c: g) { futures.add(c.getCloseFuture()); } return new DefaultChannelGroupFuture(g, futures); }
@Override public void run(Timer.TimeoutHandle timeoutHandle) { try { if (!channel.isConnected() && !channel.getCloseFuture().isDone()) { LOGGER.warn("{}: connect timeout to {} triggered", selfId, serverId); channel.close(); } } catch (Exception e) { LOGGER.warn("{}: fail close unconnected channel to {}", selfId, serverId, e); } } }, connectTimeout, timeUnit);
@Override public WebSocketClient call() throws Exception { try { channel.getCloseFuture().awaitUninterruptibly(); bootstrap.releaseExternalResources(); webSocketHandler.onClose(null); } catch (Throwable e) { exceptionHandler.uncaughtException(Thread.currentThread(), WebbitException.fromException(e, channel)); } return WebSocketClient.this; } });
public static void connect(final ClientOptions options) { final ClientBootstrap bootstrap = getBootstrap(Executors.newCachedThreadPool(), options); final ChannelFuture future = bootstrap.connect(new InetSocketAddress(options.getHost(), options.getPort())); future.awaitUninterruptibly(); if(!future.isSuccess()) { // future.getCause().printStackTrace(); logger.error("error creating client connection: {}", future.getCause().getMessage()); } future.getChannel().getCloseFuture().awaitUninterruptibly(); bootstrap.getFactory().releaseExternalResources(); }