Refine search
@Override public void shutdown() { bootstrap.config().group().shutdownGracefully(); try { bootstrap.config().group().terminationFuture().sync(); } catch (InterruptedException e) { GlowServer.logger.log(Level.SEVERE, "Datagram server shutdown process interrupted!", e); } } }
@Override @SuppressWarnings("unchecked") void init(Channel channel) throws Exception { ChannelPipeline p = channel.pipeline(); p.addLast(config.handler()); final Map<ChannelOption<?>, Object> options = options0(); synchronized (options) { setChannelOptions(channel, options, logger); } final Map<AttributeKey<?>, Object> attrs = attrs0(); synchronized (attrs) { for (Entry<AttributeKey<?>, Object> e: attrs.entrySet()) { channel.attr((AttributeKey<Object>) e.getKey()).set(e.getValue()); } } }
/** * Connect a {@link Channel} to the remote peer. */ public ChannelFuture connect() { validate(); SocketAddress remoteAddress = this.remoteAddress; if (remoteAddress == null) { throw new IllegalStateException("remoteAddress not set"); } return doResolveAndConnect(remoteAddress, config.localAddress()); }
@Override public String toString() { StringBuilder buf = new StringBuilder(super.toString()); buf.setLength(buf.length() - 1); buf.append(", resolver: ").append(resolver()); SocketAddress remoteAddress = remoteAddress(); if (remoteAddress != null) { buf.append(", remoteAddress: ") .append(remoteAddress); } return buf.append(')').toString(); } }
AddressResolver<InetSocketAddress> resolver = (AddressResolver<InetSocketAddress>) bootstrap.config().resolver().getResolver(bootstrap.config().group().next()); Future<InetSocketAddress> resolveFuture = resolver.resolve(InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort())); resolveFuture.addListener(new FutureListener<InetSocketAddress>() {
/** * Binds a GS4 listener to the specified {@code hostname} and {@code port}. * * @param hostname the hostname to bind to * @param port the port to bind to */ public void queryBind(final String hostname, final int port) { InetSocketAddress address = new InetSocketAddress(hostname, port); final Bootstrap bootstrap = new Bootstrap() .channel(this.transportType.datagramChannelClass) .group(this.workerGroup) .handler(new GS4QueryHandler(this.server)) .localAddress(address); bootstrap.bind() .addListener((ChannelFutureListener) future -> { final Channel channel = future.channel(); if (future.isSuccess()) { this.endpoints.put(address, channel); LOGGER.info("Listening for GS4 query on {}", channel.localAddress()); } else { LOGGER.error("Can't bind to {}", bootstrap.config().localAddress(), future.cause()); } }); }
@Test public void shouldNotLogExceptionsWhenEvenLoopIsShuttingDown() throws Exception { AssertableLogProvider logProvider = new AssertableLogProvider(); BoltConnection connection = mock( BoltConnection.class ); HouseKeeper houseKeeper = new HouseKeeper( connection, logProvider.getLog( HouseKeeper.class ) ); Bootstrap bootstrap = newBootstrap( houseKeeper ); try ( ServerSocket serverSocket = new ServerSocket( 0 ) ) { ChannelFuture future = bootstrap.connect( "localhost", serverSocket.getLocalPort() ).sync(); Channel channel = future.channel(); // write some messages without flushing for ( int i = 0; i < 100; i++ ) { // use void promise which should redirect all write errors back to the pipeline and the HouseKeeper channel.write( writeUtf8( channel.alloc(), "Hello" ), channel.voidPromise() ); } // stop the even loop to make all pending writes fail bootstrap.config().group().shutdownGracefully(); // await for the channel to be closed by the HouseKeeper channel.closeFuture().sync(); } finally { // make sure event loop group is always terminated bootstrap.config().group().shutdownGracefully().sync(); } logProvider.assertNoLoggingOccurred(); }
/** * Remove a configuration given its unique name from the given {@link * Bootstrap} * * @param b a bootstrap * @param name a configuration name */ public static Bootstrap removeConfiguration(Bootstrap b, String name) { Objects.requireNonNull(b, "bootstrap"); Objects.requireNonNull(name, "name"); if (b.config().handler() != null) { b.handler(removeConfiguration(b.config().handler(), name)); } return b; }
public void shutdown() { if (bootstrap != null) { EventLoopGroup group = bootstrap.config().group(); group.shutdownGracefully(); } }
@Override public Mono<? extends Connection> acquire(Bootstrap b) { return Mono.create(sink -> { Bootstrap bootstrap = b.clone(); ChannelOperations.OnSetup factory = BootstrapHandlers.channelOperationFactory(bootstrap); ConnectionObserver obs = BootstrapHandlers.connectionObserver(bootstrap); if (bootstrap.config() .remoteAddress() != null) { convertLazyRemoteAddress(bootstrap); } BootstrapHandlers.finalizeHandler(bootstrap, factory, new NewConnectionObserver(sink, obs)); ChannelFuture f; if (bootstrap.config() .remoteAddress() != null) { f = bootstrap.connect(); } else { f = bootstrap.bind(); } DisposableConnect disposableConnect = new DisposableConnect(sink, f, bootstrap); f.addListener(disposableConnect); sink.onCancel(disposableConnect); }); }
private void tryReconnect(final RedisConnection connection, final int nextAttempt) { if (connection.isClosed() || bootstrap.config().group().isShuttingDown()) { return; bootstrap.connect(connection.getRedisClient().getAddr()).addListener(new ChannelFutureListener() {
@Override public void run() { final Bootstrap bootstrap = ApnsChannelFactory.this.bootstrapTemplate.clone() .channelFactory(new AugmentingReflectiveChannelFactory<>( ClientChannelClassUtil.getSocketChannelClass(ApnsChannelFactory.this.bootstrapTemplate.config().group()), CHANNEL_READY_PROMISE_ATTRIBUTE_KEY, channelReadyPromise)); final ChannelFuture connectFuture = bootstrap.connect(); connectFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) { if (!future.isSuccess()) { // This may seem spurious, but our goal here is to accurately report the cause of // connection failure; if we just wait for connection closure, we won't be able to // tell callers anything more specific about what went wrong. tryFailureAndLogRejectedCause(channelReadyPromise, future.cause()); } } }); connectFuture.channel().closeFuture().addListener(new GenericFutureListener<ChannelFuture> () { @Override public void operationComplete(final ChannelFuture future) { // We always want to try to fail the "channel ready" promise if the connection closes; if it has // already succeeded, this will have no effect. channelReadyPromise.tryFailure( new IllegalStateException("Channel closed before HTTP/2 preface completed.")); } }); } }, delay, TimeUnit.SECONDS);
@SuppressWarnings("unchecked") static void convertLazyRemoteAddress(Bootstrap b) { SocketAddress remote = b.config() .remoteAddress(); Objects.requireNonNull(remote, "Remote Address not configured"); if (remote instanceof Supplier) { Supplier<? extends SocketAddress> lazyRemote = (Supplier<? extends SocketAddress>) remote; b.remoteAddress(Objects.requireNonNull(lazyRemote.get(), "address supplier returned null")); } }
private void reconnect(final RedisConnection connection, final int attempts){ int timeout = 2 << attempts; if (bootstrap.config().group().isShuttingDown()) { return; } try { timer.newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { tryReconnect(connection, Math.min(BACKOFF_CAP, attempts + 1)); } }, timeout, TimeUnit.MILLISECONDS); } catch (IllegalStateException e) { // skip } }
@Override public Mono<Connection> acquire(Bootstrap b) { return Mono.create(sink -> { Bootstrap bootstrap = b.clone(); ChannelHandler handler = bootstrap.config().handler(); PoolKey holder = new PoolKey(bootstrap.config().remoteAddress(), handler != null ? handler.hashCode() : -1); name, bootstrap.config() .remoteAddress());
static int getPort(Bootstrap b) { if (b.config() .remoteAddress() instanceof InetSocketAddress) { return ((InetSocketAddress) b.config() .remoteAddress()).getPort(); } return DEFAULT_PORT; } }