List of usage examples for io.netty.channel ChannelOption CONNECT_TIMEOUT_MILLIS
ChannelOption CONNECT_TIMEOUT_MILLIS
To view the source code for io.netty.channel ChannelOption CONNECT_TIMEOUT_MILLIS.
Click Source Link
From source file:net.kuujo.copycat.netty.protocol.impl.TcpProtocolClient.java
License:Apache License
@Override public CompletableFuture<Void> connect() { final CompletableFuture<Void> future = new CompletableFuture<>(); if (channel != null) { future.complete(null);/*w ww.j a v a 2 s.co m*/ return future; } final SslContext sslContext; if (protocol.isSsl()) { try { sslContext = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE); } catch (SSLException e) { future.completeExceptionally(e); return future; } } else { sslContext = null; } final EventLoopGroup group = new NioEventLoopGroup(protocol.getThreads()); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); if (sslContext != null) { pipeline.addLast( sslContext.newHandler(channel.alloc(), protocol.getHost(), protocol.getPort())); } pipeline.addLast(new ObjectEncoder(), new ObjectDecoder( ClassResolvers.softCachingConcurrentResolver(getClass().getClassLoader())), new TcpProtocolClientHandler(TcpProtocolClient.this)); } }); if (protocol.getSendBufferSize() > -1) { bootstrap.option(ChannelOption.SO_SNDBUF, protocol.getSendBufferSize()); } if (protocol.getReceiveBufferSize() > -1) { bootstrap.option(ChannelOption.SO_RCVBUF, protocol.getReceiveBufferSize()); } if (protocol.getTrafficClass() > -1) { bootstrap.option(ChannelOption.IP_TOS, protocol.getTrafficClass()); } bootstrap.option(ChannelOption.TCP_NODELAY, protocol.isNoDelay()); bootstrap.option(ChannelOption.SO_LINGER, protocol.getSoLinger()); bootstrap.option(ChannelOption.SO_KEEPALIVE, protocol.isKeepAlive()); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, protocol.getConnectTimeout()); bootstrap.connect(protocol.getHost(), protocol.getPort()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { if (channelFuture.isSuccess()) { channel = channelFuture.channel(); future.complete(null); } else { future.completeExceptionally(channelFuture.cause()); } } }); return future; }
From source file:net.tomp2p.connection.ChannelCreator.java
License:Apache License
/** * Creates a channel to the given address. This will setup the TCP * connection// w w w . j a va 2s . c o m * * @param socketAddress * The address to send future messages * @param connectionTimeoutMillis * The timeout for establishing a TCP connection * @param channelHandlers * The handlers to set * @param futureResponse * the futureResponse * @return The channel future object or null if we are shut down. */ public ChannelFuture createTCP(final SocketAddress socketAddress, final int connectionTimeoutMillis, final Map<String, Pair<EventExecutorGroup, ChannelHandler>> channelHandlers, final FutureResponse futureResponse) { readTCP.lock(); try { if (shutdownTCP) { return null; } if (!semaphoreTCP.tryAcquire()) { LOG.error("Tried to acquire more resources (TCP) than announced!"); throw new RuntimeException("Tried to acquire more resources (TCP) than announced!"); } Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeoutMillis); b.option(ChannelOption.TCP_NODELAY, true); b.option(ChannelOption.SO_LINGER, 0); b.option(ChannelOption.SO_REUSEADDR, true); Map<String, Pair<EventExecutorGroup, ChannelHandler>> channelHandlers2 = channelClientConfiguration .pipelineFilter().filter(channelHandlers, true, true); addHandlers(b, channelHandlers2); ChannelFuture channelFuture = b.connect(socketAddress, new InetSocketAddress(channelClientConfiguration.senderTCP(), 0)); recipients.add(channelFuture.channel()); setupCloseListener(channelFuture, semaphoreTCP, futureResponse); return channelFuture; } finally { readTCP.unlock(); } }
From source file:net.tomp2p.connection2.ChannelCreator.java
License:Apache License
/** * Creates a channel to the given address. This will setup the TCP connection * /*from www . j a v a 2 s . c om*/ * @param socketAddress * The address to send future messages * @param connectionTimeoutMillis * The timeout for establishing a TCP connection * @param channelHandlers * The handlers to set * @return The channel future object or null if we are shut down. */ public ChannelFuture createTCP(final SocketAddress socketAddress, final int connectionTimeoutMillis, final Map<String, ChannelHandler> channelHandlers) { readTCP.lock(); try { if (shutdownTCP) { return null; } if (!semaphoreTCP.tryAcquire()) { LOG.error("Tried to acquire more resources (TCP) than announced!"); throw new RuntimeException("Tried to acquire more resources (TCP) than announced!"); } Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeoutMillis); b.option(ChannelOption.TCP_NODELAY, true); b.option(ChannelOption.SO_LINGER, 0); b.option(ChannelOption.SO_REUSEADDR, true); // b.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE); channelClientConfiguration.pipelineFilter().filter(channelHandlers, true, true); addHandlers(b, channelHandlers); ChannelFuture channelFuture = b.connect(socketAddress); setupCloseListener(channelFuture, semaphoreTCP); CREATED_TCP_CONNECTIONS.incrementAndGet(); return channelFuture; } finally { readTCP.unlock(); } }
From source file:nettyTest.http.HttpHelloWorldClient.java
License:Apache License
public static void main(String[] args) throws Exception { URI uri = new URI(URL); String scheme = uri.getScheme() == null ? "http" : uri.getScheme(); String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost(); int port = uri.getPort(); if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) { System.err.println("Only HTTP(S) is supported."); return;/* w ww.j a va2 s.co m*/ } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000) .handler(new HttpHelloWorldClientInitializer()); // Make the connection attempt. Channel ch = b.connect(host, port).sync().channel(); // Prepare the HTTP request. HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath()); request.headers().set(HttpHeaders.Names.HOST, host); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); // Set some example cookies. request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX .encode(new DefaultCookie("my-cookie", "foo"), new DefaultCookie("another-cookie", "bar"))); // Send the HTTP request. ch.writeAndFlush(request); // Wait for the server to close the connection. ch.closeFuture().sync(); } finally { // Shut down executor threads to exit. group.shutdownGracefully(); } }
From source file:object.client.ext.ObjectClient.java
public ObjectClient(int id, String remotehost, int port, ClassLoader classLoader) { this.id = id; this.remotehost = remotehost; this.port = port; this.classLoader = classLoader; //GROUP = new NioEventLoopGroup(); bootstrap = new Bootstrap(); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 6000); // At client side option is tcpNoDelay and at server child.tcpNoDelay //monitor= new Monitor (); }
From source file:object.client.ext.ObjectNewClient.java
public ObjectNewClient(String remotehost, int port, ClassLoader classLoader, ConnectionFeedBack connectionFeedBack) { this.classLoader = classLoader; this.connectionFeedBack = connectionFeedBack; this.remotehost = remotehost; this.port = port; bootstrap = new Bootstrap(); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000); }
From source file:org.aaron.sms.api.SMSTCPConnection.java
License:Open Source License
@Override protected ChannelFuture doBootstrapConnection(ChannelInitializer<Channel> channelInitializer) { return new Bootstrap().group(getEventLoopGroup()) .channel(TCPEventLoopGroupContainer.getClientChannelClass()).handler(channelInitializer) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT_MS) .connect(brokerAddress, brokerPort); }
From source file:org.acmsl.katas.antlr4netty.InterpreterServer.java
License:Open Source License
/** * Launches the server to accept incoming requests on given port. * @param port the port.// w ww .j ava 2 s .c o m * @return the {@link ChannelFuture} when the server stops accepting connections. */ @NotNull public ChannelFuture listen(final int port) { @NotNull final ChannelFuture result; @NotNull final NioEventLoopGroup bossGroup = new NioEventLoopGroup(); @NotNull final NioEventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { /** * {@inheritDoc} */ @Override protected void initChannel(@NotNull final SocketChannel ch) throws Exception { ch.pipeline().addLast(new InterpreterServerChannelHandler()); } }).option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) .childOption(ChannelOption.SO_KEEPALIVE, true); result = wrap(bootstrap.bind(port), bossGroup, workerGroup); return result; }
From source file:org.acmsl.queryj.debugging.netty.NettyServerDebuggingService.java
License:Open Source License
/** * Launches the server.//from ww w. j a va 2 s. c o m * @param port the port. * @param handler the {@link ChannelHandlerAdapter handler} to handle incoming connections. * @return the {@link ChannelFuture}. * @throws InterruptedException if the server gets interrupted. * @throws IOException if the socket cannot be bound. */ @NotNull protected ChannelFuture launchServer(final int port, @NotNull final ChannelHandlerAdapter handler) throws InterruptedException, IOException { @NotNull final ChannelFuture result; @Nullable ChannelFuture aux = null; @NotNull final EventLoopGroup bossGroup = new NioEventLoopGroup(); setEventLoopGroup(bossGroup); @NotNull final EventLoopGroup workerGroup = new NioEventLoopGroup(); try { @NotNull final ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { // (4) /** * {@inheritDoc} */ @Override public void initChannel(@NotNull final SocketChannel ch) throws Exception { ch.pipeline().addLast(handler); } }).option(ChannelOption.SO_BACKLOG, 128) // (5) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) .childOption(ChannelOption.SO_KEEPALIVE, true); // (6) // Bind and start to accept incoming connections. aux = b.bind(port).sync(); // (7) // Wait until the server socket is closed. // In this example, this does not happen, but you can do that to gracefully // shut down your server. // result.channel().closeFuture().sync(); } catch (@NotNull final Throwable throwable) { LogFactory.getLog(NettyServerDebuggingService.class).fatal("Cannot run the template debugging server", throwable); workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } if (aux == null) { throw new RuntimeException("Cannot run server"); } else { result = aux; } return result; }
From source file:org.anhonesteffort.p25.chnlzr.ChnlzrConnectionFactory.java
License:Open Source License
public ListenableFuture<ChnlzrConnectionHandler> create(HostId chnlzrHost) { SettableFuture<ChnlzrConnectionHandler> future = SettableFuture.create(); ChnlzrConnectionHandler connection = new ChnlzrConnectionHandler(future); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workerGroup).channel(channel).option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.connectionTimeoutMs()) .option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, config.bufferHighWaterMark()) .option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, config.bufferLowWaterMark()) .handler(new ChannelInitializer<SocketChannel>() { @Override/* w w w.j a va 2s . com*/ public void initChannel(SocketChannel ch) { ch.pipeline().addLast("idle state", new IdleStateHandler(0, 0, config.idleStateThresholdMs(), TimeUnit.MILLISECONDS)); ch.pipeline().addLast("heartbeat", IdleStateHeartbeatWriter.INSTANCE); ch.pipeline().addLast("encoder", BaseMessageEncoder.INSTANCE); ch.pipeline().addLast("decoder", new BaseMessageDecoder()); ch.pipeline().addLast("connector", connection); } }); bootstrap.connect(chnlzrHost.getHostname(), chnlzrHost.getPort()).addListener(connect -> { if (!connect.isSuccess()) future.setException(new ConnectException("failed to connect to chnlzr")); }); return future; }