Java Code Examples for org.jboss.netty.bootstrap.ClientBootstrap#setPipelineFactory()
The following examples show how to use
org.jboss.netty.bootstrap.ClientBootstrap#setPipelineFactory() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Fetcher.java From incubator-tajo with Apache License 2.0 | 6 votes |
public Fetcher(URI uri, File file, ClientSocketChannelFactory factory) { this.uri = uri; this.file = file; String scheme = uri.getScheme() == null ? "http" : uri.getScheme(); this.host = uri.getHost() == null ? "localhost" : uri.getHost(); this.port = uri.getPort(); if (port == -1) { if (scheme.equalsIgnoreCase("http")) { this.port = 80; } else if (scheme.equalsIgnoreCase("https")) { this.port = 443; } } bootstrap = new ClientBootstrap(factory); bootstrap.setOption("connectTimeoutMillis", 5000L); // set 5 sec bootstrap.setOption("receiveBufferSize", 1048576); // set 1M bootstrap.setOption("tcpNoDelay", true); ChannelPipelineFactory pipelineFactory = new HttpClientPipelineFactory(file); bootstrap.setPipelineFactory(pipelineFactory); }
Example 2
Source File: NettyClient.java From dubbox with Apache License 2.0 | 6 votes |
@Override protected void doOpen() throws Throwable { NettyHelper.setNettyLoggerFactory(); bootstrap = new ClientBootstrap(channelFactory); // config // @see org.jboss.netty.channel.socket.SocketChannelConfig bootstrap.setOption("keepAlive", true); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("connectTimeoutMillis", getTimeout()); final NettyHandler nettyHandler = new NettyHandler(getUrl(), this); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", adapter.getDecoder()); pipeline.addLast("encoder", adapter.getEncoder()); pipeline.addLast("handler", nettyHandler); return pipeline; } }); }
Example 3
Source File: NettyClient.java From dubbox with Apache License 2.0 | 6 votes |
@Override protected void doOpen() throws Throwable { NettyHelper.setNettyLoggerFactory(); bootstrap = new ClientBootstrap(channelFactory); // config // @see org.jboss.netty.channel.socket.SocketChannelConfig bootstrap.setOption("keepAlive", true); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("connectTimeoutMillis", getTimeout()); final NettyHandler nettyHandler = new NettyHandler(getUrl(), this); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", adapter.getDecoder()); pipeline.addLast("encoder", adapter.getEncoder()); pipeline.addLast("handler", nettyHandler); return pipeline; } }); }
Example 4
Source File: NettyClient.java From dubbox-hystrix with Apache License 2.0 | 6 votes |
@Override protected void doOpen() throws Throwable { NettyHelper.setNettyLoggerFactory(); bootstrap = new ClientBootstrap(channelFactory); // config // @see org.jboss.netty.channel.socket.SocketChannelConfig bootstrap.setOption("keepAlive", true); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("connectTimeoutMillis", getTimeout()); final NettyHandler nettyHandler = new NettyHandler(getUrl(), this); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", adapter.getDecoder()); pipeline.addLast("encoder", adapter.getEncoder()); pipeline.addLast("handler", nettyHandler); return pipeline; } }); }
Example 5
Source File: SimpleTcpClient.java From hadoop with Apache License 2.0 | 6 votes |
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(); } }
Example 6
Source File: RemoteSyncManager.java From floodlight_with_topoguard with Apache License 2.0 | 6 votes |
@Override public void startUp(FloodlightModuleContext context) throws FloodlightModuleException { shutdown = false; bossExecutor = Executors.newCachedThreadPool(); workerExecutor = Executors.newCachedThreadPool(); final ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory(bossExecutor, workerExecutor)); bootstrap.setOption("child.reuseAddr", true); bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.sendBufferSize", RPCService.SEND_BUFFER_SIZE); bootstrap.setOption("child.receiveBufferSize", RPCService.SEND_BUFFER_SIZE); bootstrap.setOption("child.connectTimeoutMillis", RPCService.CONNECT_TIMEOUT); pipelineFactory = new RemoteSyncPipelineFactory(this); bootstrap.setPipelineFactory(pipelineFactory); clientBootstrap = bootstrap; }
Example 7
Source File: BgpControllerImplTest.java From onos with Apache License 2.0 | 6 votes |
private Channel connectFrom(InetSocketAddress connectToSocket, SocketAddress localAddress) throws InterruptedException { ChannelFactory channelFactory = new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); ChannelPipelineFactory pipelineFactory = () -> { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("BgpPeerFrameDecoderTest", peerFrameDecoder); pipeline.addLast("BgpPeerChannelHandlerTest", peerChannelHandler); return pipeline; }; peerBootstrap = new ClientBootstrap(channelFactory); peerBootstrap.setOption("child.keepAlive", true); peerBootstrap.setOption("child.tcpNoDelay", true); peerBootstrap.setPipelineFactory(pipelineFactory); Channel channel = peerBootstrap.connect(connectToSocket, localAddress).getChannel(); return channel; }
Example 8
Source File: BgpControllerImplTest.java From onos with Apache License 2.0 | 6 votes |
/** * Starts the BGP peer. * * @param connectToSocket the socket to connect to */ private void connect(InetSocketAddress connectToSocket) throws InterruptedException { ChannelFactory channelFactory = new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); ChannelPipelineFactory pipelineFactory = () -> { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("BgpPeerFrameDecoderTest", peerFrameDecoder); pipeline.addLast("BgpPeerChannelHandlerTest", peerChannelHandler); return pipeline; }; peerBootstrap = new ClientBootstrap(channelFactory); peerBootstrap.setOption("child.keepAlive", true); peerBootstrap.setOption("child.tcpNoDelay", true); peerBootstrap.setPipelineFactory(pipelineFactory); peerBootstrap.connect(connectToSocket); }
Example 9
Source File: NettyClient.java From dubbo-2.6.5 with Apache License 2.0 | 6 votes |
@Override protected void doOpen() throws Throwable { NettyHelper.setNettyLoggerFactory(); bootstrap = new ClientBootstrap(channelFactory); // config // @see org.jboss.netty.channel.socket.SocketChannelConfig bootstrap.setOption("keepAlive", true); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("connectTimeoutMillis", getConnectTimeout()); final NettyHandler nettyHandler = new NettyHandler(getUrl(), this); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() { NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", adapter.getDecoder()); pipeline.addLast("encoder", adapter.getEncoder()); pipeline.addLast("handler", nettyHandler); return pipeline; } }); }
Example 10
Source File: Bootstrap.java From floodlight_with_topoguard with Apache License 2.0 | 6 votes |
public void init() throws SyncException { cg = new DefaultChannelGroup("Cluster Bootstrap"); bossExecutor = Executors.newCachedThreadPool(); workerExecutor = Executors.newCachedThreadPool(); bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(bossExecutor, workerExecutor)); bootstrap.setOption("child.reuseAddr", true); bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.sendBufferSize", RPCService.SEND_BUFFER_SIZE); bootstrap.setOption("child.receiveBufferSize", RPCService.SEND_BUFFER_SIZE); bootstrap.setOption("child.connectTimeoutMillis", RPCService.CONNECT_TIMEOUT); pipelineFactory = new BootstrapPipelineFactory(this); bootstrap.setPipelineFactory(pipelineFactory); }
Example 11
Source File: RPCService.java From floodlight_with_topoguard with Apache License 2.0 | 6 votes |
/** * Connect to remote servers. We'll initiate the connection to * any nodes with a lower ID so that there will be a single connection * between each pair of nodes which we'll use symmetrically */ protected void startClients(ChannelPipelineFactory pipelineFactory) { final ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory(bossExecutor, workerExecutor)); bootstrap.setOption("child.reuseAddr", true); bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.sendBufferSize", SEND_BUFFER_SIZE); bootstrap.setOption("child.connectTimeoutMillis", CONNECT_TIMEOUT); bootstrap.setPipelineFactory(pipelineFactory); clientBootstrap = bootstrap; ScheduledExecutorService ses = syncManager.getThreadPool().getScheduledExecutor(); reconnectTask = new SingletonTask(ses, new ConnectTask()); reconnectTask.reschedule(0, TimeUnit.SECONDS); }
Example 12
Source File: NettySend.java From jlogstash-input-plugin with Apache License 2.0 | 5 votes |
public void connect(){ bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); bootstrap.setOption("tcpNoDelay", false); bootstrap.setOption("keepAlive", true); final NettyClientHandler handler = new NettyClientHandler(this, timer); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("handler", handler); pipeline.addLast("encoder", new StringEncoder()); return pipeline; } }); bootstrap.setOption("remoteAddress", new InetSocketAddress(host, port)); try { ChannelFuture future = bootstrap.connect().sync(); channel = future.getChannel(); } catch (Exception e) { logger.error(ExceptionUtil.getErrorMessage(e)); bootstrap.releaseExternalResources(); System.exit(-1);//第一次连接出现异常直接退出,不走重连 } }
Example 13
Source File: NettyClientFactory.java From migration-tool with Apache License 2.0 | 5 votes |
public Client createClient(String targetIP, int targetPort, int connectTimeout) throws Exception { ClientBootstrap bootstrap = new ClientBootstrap(nioClient); bootstrap.setOption("tcpNoDelay", Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.nodelay", "true"))); bootstrap.setOption("reuseAddress", Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.reuseaddress", "true"))); if (connectTimeout < 1000) { bootstrap.setOption("connectTimeoutMillis", 1000); } else { bootstrap.setOption("connectTimeoutMillis", connectTimeout); } NettyClientHandler handler = new NettyClientHandler(this); bootstrap.setPipelineFactory(new NettyClientPipelineFactory(handler)); ChannelFuture future = bootstrap.connect(new InetSocketAddress(targetIP, targetPort)); future.awaitUninterruptibly(connectTimeout); if (!future.isDone()) { log.error("Create connection to " + targetIP + ":" + targetPort + " timeout!"); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " timeout!"); } if (future.isCancelled()) { log.error("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!"); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!"); } if (!future.isSuccess()) { log.error("Create connection to " + targetIP + ":" + targetPort + " error", future.getCause()); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " error", future.getCause()); } NettyClient client = new NettyClient(future, connectTimeout); handler.setClient(client); return client; }
Example 14
Source File: NettyTcpClientTransport.java From msgpack-rpc-java with Apache License 2.0 | 5 votes |
NettyTcpClientTransport(TcpClientConfig config, Session session, NettyEventLoop loop) { // TODO check session.getAddress() instanceof IPAddress super(config, session); RpcMessageHandler handler = new RpcMessageHandler(session); bootstrap = new ClientBootstrap(loop.getClientFactory()); bootstrap.setPipelineFactory(new StreamPipelineFactory(loop.getMessagePack(), handler)); Map<String, Object> options = config.getOptions(); setIfNotPresent(options, TCP_NO_DELAY, Boolean.TRUE, bootstrap); bootstrap.setOptions(options); }
Example 15
Source File: Connection.java From nfs-client-java with Apache License 2.0 | 5 votes |
/** * @param remoteHost A unique name for the host to which the connection is being made. * @param port The remote host port being used for the connection. * @param usePrivilegedPort * <ul> * <li>If <code>true</code>, use a privileged port (below 1024) * for RPC communication.</li> * <li>If <code>false</code>, use any non-privileged port for RPC * communication.</li> * </ul> */ public Connection(String remoteHost, int port, boolean usePrivilegedPort) { _remoteHost = remoteHost; _port = port; _usePrivilegedPort = usePrivilegedPort; _clientBootstrap = new ClientBootstrap(NetMgr.getInstance().getFactory()); // Configure the client. _clientBootstrap.setOption(REMOTE_ADDRESS_OPTION, new InetSocketAddress(_remoteHost, _port)); _clientBootstrap.setOption("connectTimeoutMillis", CONNECT_TIMEOUT); // set // connection // timeout // value // to // 10 // seconds _clientBootstrap.setOption("tcpNoDelay", true); _clientBootstrap.setOption("keepAlive", true); _clientBootstrap.setOption(CONNECTION_OPTION, this); // Configure the pipeline factory. _clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() { /** * Netty helper instance. */ private final ChannelHandler ioHandler = new ClientIOHandler(_clientBootstrap); /* (non-Javadoc) * @see org.jboss.netty.channel.ChannelPipelineFactory#getPipeline() */ public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new RPCRecordDecoder(), ioHandler); } }); }
Example 16
Source File: EventClient.java From hadoop-arch-book with Apache License 2.0 | 5 votes |
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(); } }
Example 17
Source File: NettyClient.java From jstorm with Apache License 2.0 | 5 votes |
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(); }
Example 18
Source File: NettyRpcConnection.java From voyage with Apache License 2.0 | 5 votes |
/** * @param connectStatus 心跳检测状态是否正常 * @throws Throwable */ public void open(boolean connectStatus) throws Throwable { logger.info("open start,"+getConnStr()); bootstrap = new ClientBootstrap(factory); // timer = new HashedWheelTimer(); { bootstrap.setOption("tcpNoDelay", Boolean.parseBoolean(clientConfig.getTcpNoDelay())); bootstrap.setOption("reuseAddress", Boolean.parseBoolean(clientConfig.getReuseAddress())); bootstrap.setOption("SO_RCVBUF",1024*128); bootstrap.setOption("SO_SNDBUF",1024*128); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { ChannelPipeline pipeline = Channels.pipeline(); // int readTimeout = clientConfig.getReadTimeout(); // if (readTimeout > 0) { // pipeline.addLast("timeout", new ReadTimeoutHandler(timer, // readTimeout, TimeUnit.MILLISECONDS)); // } pipeline.addLast("encoder", new RpcRequestEncode()); pipeline.addLast("decoder", new RpcResponseDecode()); pipeline.addLast("handler", NettyRpcConnection.this); return pipeline; } }); } connected.set(connectStatus); logger.info("open finish,"+getConnStr()); }
Example 19
Source File: TcpProviderPoc.java From parallec with Apache License 2.0 | 4 votes |
public void request() throws Exception { // Parse options. String host = "localhost"; int port = 10081; int connectTimeoutMillis = 2000; // Configure the client. ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); final TelnetClientHandler handler = new TelnetClientHandler(); // Configure the pipeline factory. bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("framer", new DelimiterBasedFrameDecoder(1024, Delimiters.lineDelimiter())); pipeline.addLast("stringDecoder", stringDecoder); pipeline.addLast("stringEncoder", stringEncoder); pipeline.addLast("handler", handler); return pipeline; } }); bootstrap.setOption("connectTimeoutMillis",connectTimeoutMillis); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("keepAlive", true); // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection attempt succeeds or fails. Channel channel = future.awaitUninterruptibly().getChannel(); // Read commands from the stdin. ChannelFuture lastWriteFuture = null; String command = "hadoopMonitorFromClient"; // Sends the line to server. lastWriteFuture = channel.write(command + "\r\n"); // Wait until all messages are flushed before closing the channel. if (lastWriteFuture != null) { lastWriteFuture.await(); } // note that we need to wait for the response before // wait time before close the channel too early that msg will not be // received. while (!handler.channelCompleted) { Thread.sleep(1l); } // Close the connection. Make sure the close operation ends because // all I/O operations are asynchronous in Netty. channel.close().awaitUninterruptibly(); // Shut down all thread pools to exit. bootstrap.releaseExternalResources(); }
Example 20
Source File: BgpSessionManagerTest.java From onos with Apache License 2.0 | 4 votes |
/** * Starts up the BGP peer and connects it to the tested BgpSessionManager * instance. * * @param connectToSocket the socket to connect to */ private void connect(InetSocketAddress connectToSocket) throws InterruptedException { // // Setup the BGP Peer, i.e., the "remote" BGP router that will // initiate the BGP connection, send BGP UPDATE messages, etc. // ChannelFactory channelFactory = new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); ChannelPipelineFactory pipelineFactory = () -> { // Setup the transmitting pipeline ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("TestBgpPeerFrameDecoder", peerFrameDecoder); pipeline.addLast("TestBgpPeerChannelHandler", peerChannelHandler); return pipeline; }; peerBootstrap = new ClientBootstrap(channelFactory); peerBootstrap.setOption("child.keepAlive", true); peerBootstrap.setOption("child.tcpNoDelay", true); peerBootstrap.setPipelineFactory(pipelineFactory); peerBootstrap.connect(connectToSocket); boolean result; // Wait until the OPEN message is received result = peerFrameDecoder.receivedOpenMessageLatch.await( MESSAGE_TIMEOUT_MS, TimeUnit.MILLISECONDS); assertThat(result, is(true)); // Wait until the KEEPALIVE message is received result = peerFrameDecoder.receivedKeepaliveMessageLatch.await( MESSAGE_TIMEOUT_MS, TimeUnit.MILLISECONDS); assertThat(result, is(true)); for (BgpSession bgpSession : bgpSessionManager.getBgpSessions()) { if (bgpSession.remoteInfo().bgpId().equals(BGP_PEER1_ID)) { bgpSession1 = bgpSession; } if (bgpSession.remoteInfo().bgpId().equals(BGP_PEER2_ID)) { bgpSession2 = bgpSession; } if (bgpSession.remoteInfo().bgpId().equals(BGP_PEER3_ID)) { bgpSession3 = bgpSession; } } }