本文整理汇总了Java中org.jboss.netty.bootstrap.ClientBootstrap类的典型用法代码示例。如果您正苦于以下问题:Java ClientBootstrap类的具体用法?Java ClientBootstrap怎么用?Java ClientBootstrap使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ClientBootstrap类属于org.jboss.netty.bootstrap包,在下文中一共展示了ClientBootstrap类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
public void start(String ip, int port) {
// Configure the client.
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("java.net.preferIPv6Addresses", "false");
bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
// Set up the event pipeline factory.
// bootstrap.setPipelineFactory(new MessageServerPipelineFactory());
bootstrap.getPipeline().addLast("decoder", new PackageDecoder());
bootstrap.getPipeline().addLast("encoder", new PackageEncoder());
bootstrap.getPipeline().addLast("handler", new ClientHandler());
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
bootstrap.setOption("child.linger", 1);
// Start the connection attempt.
channelFuture = bootstrap.connect(new InetSocketAddress(ip, port));
// Wait until the connection is closed or the connection attempt fails.
channelFuture.awaitUninterruptibly();
channel = channelFuture.awaitUninterruptibly().getChannel();
}
示例2: doOpen
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
@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;
}
});
}
示例3: run
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
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();
}
}
示例4: connect
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
/**
* 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);
}
示例5: connectFrom
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
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;
}
示例6: startClients
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
/**
* 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);
}
示例7: init
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
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);
}
示例8: startUp
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
@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;
}
示例9: start
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
private static TCPClient start(String host, int port, final boolean nio,final ChannelUpstreamHandler clientHandler, final ChannelPipelineFactory factory ) {
TCPClient client = new TCPClient(host, port);
client.clientHandler= clientHandler;
// Configure the client.
client.bootstrap = new ClientBootstrap( getClientSocketChannelFactory(nio));
client.bootstrap.setPipelineFactory( nonSharableChannelPipelineFactory( clientHandler, new Encoder()));
// Start the connection attempt.
client.future = client.bootstrap.connect(new InetSocketAddress(host, port));
if (client.getFuture().awaitUninterruptibly(TIME_OUT) && client.getFuture().getChannel().isConnected())
return client ; //logger.info("wait ok");
else {
client.future.getChannel().close();
client.bootstrap.releaseExternalResources();
throw new ConnectionException("not able to connect "+ client.toString());
}
}
示例10: doOpen
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
@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());
//下面才是正确的
//bootstrap.setOption("connectTimeoutMillis", getConnectTimeout());
//netty handler
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;
}
});
}
示例11: doOpen
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
@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(() -> {
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;
});
}
示例12: ServiceClient
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
private ServiceClient(String host, int port) {
this.host = host;
this.port = port;
// Configure the client.
bootstrap = new ClientBootstrap(channelFactory);
bootstrap.setOption("tcpNoDelay", Boolean.parseBoolean(AppProperties
.get("rpc_client_tcpNoDelay", "true")));
bootstrap.setOption("keepAlive", Boolean.parseBoolean(AppProperties
.get("rpc_client_keepAlive", "true")));
bootstrap.setOption("connectTimeoutMillis", AppProperties.getAsInt(
"rpc_client_connectTimeoutMillis", 1000 * 30));
bootstrap.setOption("receiveBufferSize", AppProperties.getAsInt(
"rpc_client_receiveBufferSize", 1024 * 1024));
bootstrap.setOption("soLinger",
AppProperties.getAsInt("rpc_client_soLinger", -1));
bootstrap.setPipelineFactory(new RpcClientPipelineFactory());
connAmout = AppProperties.getAsInt("rpc_client_connectionAmount", 1);
this.channelMap = new ConcurrentHashMap<Integer, Channel>();
initConns();
}
示例13: doOpen
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
@Override
public void doOpen() throws Throwable {
bootstrap = new ClientBootstrap(channelFactory);
bootstrap.setOption("keepAlive", true);
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("connectTimeoutMillis", getConnectTimeout());
final NettyHandler nettyHandler = new NettyHandler(getConf(), this);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
NettyCodecAdapter adapter = new NettyCodecAdapter(getConf(),getCodec(), NettyClient.this);
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", adapter.getDecoder());
pipeline.addLast("encoder", adapter.getEncoder());
pipeline.addLast("handler", nettyHandler);
return pipeline;
}
});
}
示例14: doOpen
import org.jboss.netty.bootstrap.ClientBootstrap; //导入依赖的package包/类
@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;
}
});
}