hibernationSettings.get().setHibernationAllowed(false); factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); ClientBootstrap bootstrap = new ClientBootstrap(factory); bootstrap.setPipelineFactory(new TerasologyClientPipelineFactory(this)); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("keepAlive", true); ChannelFuture connectCheck = bootstrap.connect(new InetSocketAddress(address, port)); try { connectCheck.await(); } catch (InterruptedException e) { connectCheck.cancel(); connectCheck.getChannel().getCloseFuture().awaitUninterruptibly(); factory.releaseExternalResources(); throw e; if (!connectCheck.isSuccess()) { logger.warn("Failed to connect to server", connectCheck.getCause()); connectCheck.getChannel().getCloseFuture().awaitUninterruptibly(); factory.releaseExternalResources(); return new JoinStatusImpl("Failed to connect to server - " + connectCheck.getCause().getMessage()); } else { allChannels.add(connectCheck.getChannel()); ClientConnectionHandler connectionHandler = connectCheck.getChannel().getPipeline().get(ClientConnectionHandler.class); if (connectionHandler == null) { JoinStatusImpl status = new JoinStatusImpl();
@Override public void stop() { bootstrap.releaseExternalResources(); } }
public void start() { bootstrap = new ClientBootstrap(clientChannelFactory); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("reuseAddress", true); bootstrap.setOption("sendBufferSize", bufferSize); bootstrap.setOption("keepAlive", true); // Set up the pipeline factory. bootstrap.setPipelineFactory(new StormClientPipelineFactory(this, stormConf)); reconnect(); }
public ServerInfoService() { pool = Executors.newCachedThreadPool(); factory = new NioClientSocketChannelFactory(pool, pool, 1, 1); bootstrap = new ClientBootstrap(factory); bootstrap.setPipelineFactory(new InfoRequestPipelineFactory()); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("keepAlive", true); }
/** * Attempts a new connection with the specified {@code remoteAddress} and * the current {@code "localAddress"} option. If the {@code "localAddress"} * option is not set, the local address of a new channel is determined * automatically. This method is identical with the following code: * * <pre> * {@link ClientBootstrap} b = ...; * b.connect(remoteAddress, b.getOption("localAddress")); * </pre> * * @return a future object which notifies when this connection attempt * succeeds or fails * * @throws ClassCastException * if {@code "localAddress"} option's value is * neither a {@link SocketAddress} nor {@code null} * @throws ChannelPipelineException * if this bootstrap's {@link #setPipelineFactory(ChannelPipelineFactory) pipelineFactory} * failed to create a new {@link ChannelPipeline} */ public ChannelFuture connect(SocketAddress remoteAddress) { if (remoteAddress == null) { throw new NullPointerException("remoteAddress"); } SocketAddress localAddress = (SocketAddress) getOption("localAddress"); return connect(remoteAddress, localAddress); }
/** * Create a new client that connects to the supplied host and port. Connection * attempts and non-blocking commands will {@link #setDefaultTimeout timeout} * after 60 seconds. * * @param host Server hostname. * @param port Server port. */ public RedisClient(String host, int port) { ExecutorService connectors = Executors.newFixedThreadPool(1); ExecutorService workers = Executors.newCachedThreadPool(); ClientSocketChannelFactory factory = new NioClientSocketChannelFactory(connectors, workers); InetSocketAddress addr = new InetSocketAddress(host, port); bootstrap = new ClientBootstrap(factory); bootstrap.setOption("remoteAddress", addr); setDefaultTimeout(60, TimeUnit.SECONDS); channels = new DefaultChannelGroup(); timer = new HashedWheelTimer(); }
/** * Connect to app master for status RPC report. */ private void initRPCClient() { this.rpcClient = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newSingleThreadExecutor(), Executors.newSingleThreadExecutor())); // Set up the pipeline factory. this.rpcClient.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())), new ClientHandler()); } }); // Start the connection attempt. ChannelFuture future = this.rpcClient.connect(new InetSocketAddress(this.rpcHostName, this.rpcPort)); LOG.info("Connect to {}:{}", this.rpcHostName, this.rpcPort); this.rpcClientChannel = future.awaitUninterruptibly().getChannel(); }
public void run() { // Configure the client. ChannelFactory factory = new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1); ClientBootstrap bootstrap = new ClientBootstrap(factory); // Set up the pipeline factory. bootstrap.setPipelineFactory(setPipelineFactory()); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("keepAlive", true); // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); if (oneShot) { // Wait until the connection is closed or the connection attempt fails. future.getChannel().getCloseFuture().awaitUninterruptibly(); // Shut down thread pools to exit. bootstrap.releaseExternalResources(); } } }
@Override public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) { try{ Thread.sleep(1000); } catch(Exception ex) { } bootstrap.connect(new InetSocketAddress(host, port)); }
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(); }
private ChannelFuture connect(Request request, Uri uri, ProxyServer proxy, boolean useProxy, ClientBootstrap bootstrap, AsyncHandler<?> asyncHandler) throws UnknownHostException { InetSocketAddress remoteAddress = remoteAddress(request, uri, proxy, useProxy); if (asyncHandler instanceof AsyncHandlerExtensions) AsyncHandlerExtensions.class.cast(asyncHandler).onDnsResolved(remoteAddress.getAddress()); if (request.getLocalAddress() != null) return bootstrap.connect(remoteAddress, new InetSocketAddress(request.getLocalAddress(), 0)); else return bootstrap.connect(remoteAddress); }
public void connect(String host, int port) { bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); handler = new MqttMessageHandler(); handler.setListener(listener); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new MqttMessageEncoder(), new MqttMessageDecoder(), handler); } }); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("keepAlive", true); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); channel = future.awaitUninterruptibly().getChannel(); if (!future.isSuccess()) { future.getCause().printStackTrace(); bootstrap.releaseExternalResources(); return; } channel.write(new ConnectMessage(id, true, 30)); // TODO: Should probably wait for the ConnAck message }
public void connect() { clientBootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); clientBootstrap.setOption("tcpNoDelay", true); clientBootstrap.setPipelineFactory(new ClientPipelineFactory()); while (true) { ChannelFuture future = clientBootstrap.connect(new InetSocketAddress(ip, Integer.parseInt(port))); future.awaitUninterruptibly(5000); if (future.isDone()) { channel = future.getChannel(); if (channel != null && channel.isConnected()) { break; } } } }
public void startClient() { ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); try { bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { ChannelPipeline p = Channels.pipeline(); handler = new NettyClientHandler(); p.addLast("handler", handler); return p; } }); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("receiveBufferSize", 1048576); bootstrap.setOption("sendBufferSize", 1048576); // Start the connection attempt. LOG.info("EventClient: Connecting " + host + "," + port); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); LOG.info("EventClient: Connected " + host + "," + port); allChannels = new DefaultChannelGroup(); // Wait until the connection is closed or the connection attempt fails. allChannels.add(future.getChannel()); LOG.info("EventClient: Added to Channels "); } catch (Exception e) { e.printStackTrace(); } }
@Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) { final Channel inboundChannel = e.getChannel(); RtmpProxy.ALL_CHANNELS.add(inboundChannel); inboundChannel.setReadable(false); ClientBootstrap cb = new ClientBootstrap(cf); cb.getPipeline().addLast("handshaker", new ProxyHandshakeHandler()); cb.getPipeline().addLast("handler", new OutboundHandler(e.getChannel())); ChannelFuture f = cb.connect(new InetSocketAddress(remoteHost, remotePort)); outboundChannel = f.getChannel(); f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { logger.info("connected to remote host: {}, port: {}", remoteHost, remotePort); inboundChannel.setReadable(true); } else { inboundChannel.close(); } } }); }
public static void main(String args[]) { // Client服务启动器 ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // 设置一个处理服务端消息和各种消息事件的类(Handler) bootstrap.setPipelineFactory(() -> Channels.pipeline(new HelloClientHandler())); // 连接到本地的8000端口的服务端 bootstrap.connect(new InetSocketAddress("127.0.0.1", 8000)); }
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; }); }
// Configure the client. ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); ChannelFuture future = null; while (true) { future = bootstrap.connect(new InetSocketAddress("127.0.0.1", 80)); future.awaitUninterruptibly(); if (future.isSuccess()) { break; } }
channelFuture = nettyClient.getBootstrap().connect( new InetSocketAddress(nettyClient.getUrl().getHost(), nettyClient.getUrl().getPort())); boolean result = channelFuture.awaitUninterruptibly(timeout, TimeUnit.MILLISECONDS); boolean success = channelFuture.isSuccess(); channel = channelFuture.getChannel(); if (channel.getLocalAddress() != null && channel.getLocalAddress() instanceof InetSocketAddress) { localAddress = (InetSocketAddress) channel.getLocalAddress();
final int port = url.getPort() == -1 ? url.getDefaultPort() : url.getPort(); final ChannelFuture retVal; final ChannelFuture connectFuture = bootstrap.connect(new InetSocketAddress(host, port)); final ChannelPipeline pipeline = connectFuture.getChannel().getPipeline(); pipeline.addFirst("ssl", sslHandler); final ChannelFuture handshakeFuture = Channels.future(connectFuture.getChannel()); pipeline.addLast("connectionErrorHandler", new SimpleChannelUpstreamHandler() connectFuture.addListener( new ChannelFutureListener()