本文整理汇总了Java中io.netty.bootstrap.ServerBootstrap.childHandler方法的典型用法代码示例。如果您正苦于以下问题:Java ServerBootstrap.childHandler方法的具体用法?Java ServerBootstrap.childHandler怎么用?Java ServerBootstrap.childHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.bootstrap.ServerBootstrap
的用法示例。
在下文中一共展示了ServerBootstrap.childHandler方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
@Override
public void start() throws EmbeddedServletContainerException {
ServerBootstrap b = new ServerBootstrap();
groups(b);
servletExecutor = new DefaultEventExecutorGroup(50);
b.childHandler(new NettyEmbeddedServletInitializer(servletExecutor, context));
// Don't yet need the complexity of lifecycle state, listeners etc, so tell the context it's initialised here
context.setInitialised(true);
ChannelFuture future = b.bind(address).awaitUninterruptibly();
//noinspection ThrowableResultOfMethodCallIgnored
Throwable cause = future.cause();
if (null != cause) {
throw new EmbeddedServletContainerException("Could not start Netty server", cause);
}
logger.info(context.getServerInfo() + " started on port: " + getPort());
}
示例2: openServer
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
@Override
public void openServer(URL url) throws Exception{
EventLoopGroup eventLoop = new NioEventLoopGroup();
EventLoopGroup workLoop = new NioEventLoopGroup();
serverBootstrap = new ServerBootstrap();
serverBootstrap.group(eventLoop, workLoop);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>(){
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
.addLast("decoder", new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader()))) // in 1
.addLast("handler", new ServerHandler()) // in 2
.addLast("encoder", new ObjectEncoder()); // out 3
}
});
serverChannel = serverBootstrap.bind(url.getPort()).sync().sync().channel();
logger.info("start server at:" + url.getPort());
}
示例3: doOpen
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
public void doOpen() throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try{
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workerGroup);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new ObjectDecoder(1024*1024, ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
pipeline.addLast(new ObjectEncoder());
pipeline.addLast((SimpleChannelInboundHandler)handler);
}
});
serverBootstrap.option(ChannelOption.SO_BACKLOG,1024);
serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE,true);
ChannelFuture future = serverBootstrap.bind(address,port).sync();
//future.channel().closeFuture().sync();
}finally{
//workerGroup.shutdownGracefully();
//bossGroup.shutdownGracefully();
}
}
示例4: startAcceptingConnections
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
private void startAcceptingConnections() throws InterruptedException {
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
b.option(ChannelOption.SO_RCVBUF, 1048576);
b.option(ChannelOption.TCP_NODELAY, true);
b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
b.group(serverGroup, clientGroup);
b.channel(serverChannelClass);
if (enableNettyTls) {
b.childHandler(new SslServerCommunicationChannelInitializer());
} else {
b.childHandler(new OnosCommunicationChannelInitializer());
}
b.option(ChannelOption.SO_BACKLOG, 128);
b.childOption(ChannelOption.SO_KEEPALIVE, true);
// Bind and start to accept incoming connections.
b.bind(localEp.port()).sync().addListener(future -> {
if (future.isSuccess()) {
log.info("{} accepting incoming connections on port {}", localEp.host(), localEp.port());
} else {
log.warn("{} failed to bind to port {}", localEp.host(), localEp.port(), future.cause());
}
});
}
示例5: start
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
public void start(){
logger.debug( "--Socket Server will start------------" ) ;
boss = new NioEventLoopGroup() ;
work = new NioEventLoopGroup() ;
int port = CommonConfig.getInteger( SOCKET_PORT1 );
try {
logger.info( "Netty Server[" + port + "] started..." ) ;
ServerBootstrap b = new ServerBootstrap() ;
b.group( boss , work ) ;
b.channel( NioServerSocketChannel.class ) ;
b.childHandler( nettyInitializer ) ;
b.bind( port ).sync().channel().closeFuture().sync() ;
} catch ( Exception e ) {
String err_string = e.toString();
if( err_string.indexOf( "childHandler" ) != -1 ){
logger.error( "Netty Server[" + port + "] NettyInitializer can't find." ) ;
}else{
logger.error( "Netty Server[" + port + "] onload err:" + e.toString() , e ) ;
}
} finally {
logger.error( "Netty Server[" + port + "] will be unload..." ) ;
unload();
}
}
示例6: start
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
public void start(int port) {
// TODO review listening use
listening = true;
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
EthereumChannelInitializer ethereumChannelInitializer = ethereumChannelInitializerFactory.newInstance("");
ethereumListener.trace("Listening on port " + port);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT);
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout());
b.handler(new LoggingHandler());
b.childHandler(ethereumChannelInitializer);
// Start the client.
logger.info("Listening for incoming connections, port: [{}] ", port);
logger.info("NodeId: [{}] ", Hex.toHexString(config.nodeId()));
ChannelFuture f = b.bind(port).sync();
// Wait until the connection is closed.
f.channel().closeFuture().sync();
logger.debug("Connection is closed");
// TODO review listening use
listening = false;
} catch (Exception e) {
logger.debug("Exception: {} ({})", e.getMessage(), e.getClass().getName());
throw new Error("Server Disconnected");
} finally {
workerGroup.shutdownGracefully();
}
}
示例7: startSocket
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
public void startSocket() throws InterruptedException {
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
try {
ServerBootstrap boot = new ServerBootstrap();
boot.group(boss,worker);
boot.channel(NioServerSocketChannel.class);
boot.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024*1024,0,4,-4,0,false));
ch.pipeline().addLast(new ByteToPacketCodec());
//ch.pipeline().addLast(new LoginChannelHandler(listener));
ch.pipeline().addLast(new PacketChannelHandler(listener));
}
});
boot.option(ChannelOption.SO_BACKLOG,128);
boot.childOption(ChannelOption.SO_KEEPALIVE,true);
channelFuture = boot.bind(port).sync();
System.out.println("服务器"+port+"开启成功...");
channelFuture.channel().closeFuture().sync();
}finally {
boss.shutdownGracefully().sync();
worker.shutdownGracefully().sync();
channelFuture = null;
System.out.println("服务器关闭成功...");
}
}
示例8: start
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
public void start() throws Exception {
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boss, worker);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.handler(new LoggingHandler(LogLevel.INFO));
bootstrap.childHandler(ProxyServerChannelInitializerFactory.getChannelInitializer(definition));
bootstrap.childOption(ChannelOption.AUTO_READ, false);
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
server = bootstrap.bind(port).sync().channel();
log.info("Started proxy server at port: " + port);
} catch (Exception e) {
log.error(String.format("Failed starting proxy server at port: %d", port), e);
}
}
示例9: start
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
/**
* Starts the network for a {@link Server}.
*
* @param server The {@link Server} to use for building the network.
* @return <True> If the network started successfully.
*/
public static void start() {
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boss, worker);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("decoder", new ConnectionDecoder());
pipeline.addLast("encoder", new ConnectionEncoder());
pipeline.addLast("adapter", new NetworkMessageHandler());
}
});
bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
try {
bootstrap.bind(Constants.HOST_NAME, Constants.HOST_PORT).sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Server successfully bootstrapped on port " + Constants.HOST_PORT + " and address " + Constants.HOST_NAME + ".");
}
示例10: run
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
public void run() {
ServerBootstrap b = new ServerBootstrap();// 引导辅助程序
bossGroup = new NioEventLoopGroup(BIZGROUPSIZE);
workerGroup = new NioEventLoopGroup(BIZTHREADSIZE);
try {
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);// 设置nio类型的channel
b.childHandler(new ChannelInitializer<SocketChannel>() {// 有连接到达时会创建一个channel
protected void initChannel(SocketChannel ch) throws Exception {
logger.debug("客户端:{} 初始化", ch.remoteAddress());
// pipeline管理channel中的Handler,在channel队列中添加一个handler来处理业务
ch.pipeline().addLast("frameDecoder",
new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
ch.pipeline().addLast("frameEncoder", new LengthFieldPrepender(4));
ch.pipeline().addLast("decoder", msgPackDecode);
ch.pipeline().addLast("encoder", msgPackEncode);
ch.pipeline().addLast(serverHandler);
}
});
b.option(ChannelOption.SO_BACKLOG, 128);
b.childOption(ChannelOption.SO_KEEPALIVE, true);
logger.info("server start : {}", port);
ChannelFuture f = b.bind(port).sync();// 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功
channel = f.channel();
f.channel().closeFuture().sync();// 应用程序会一直等待,直到channel关闭
} catch (Exception e) {
e.printStackTrace();
}
}
示例11: doStart
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
@Override
protected void doStart() {
boolean success = false;
try {
this.serverOpenChannels = new Netty4OpenChannelsHandler(logger);
serverBootstrap = new ServerBootstrap();
serverBootstrap.group(new NioEventLoopGroup(workerCount, daemonThreadFactory(settings,
HTTP_SERVER_WORKER_THREAD_NAME_PREFIX)));
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childHandler(configureServerChannelHandler());
serverBootstrap.childOption(ChannelOption.TCP_NODELAY, SETTING_HTTP_TCP_NO_DELAY.get(settings));
serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, SETTING_HTTP_TCP_KEEP_ALIVE.get(settings));
final ByteSizeValue tcpSendBufferSize = SETTING_HTTP_TCP_SEND_BUFFER_SIZE.get(settings);
if (tcpSendBufferSize.getBytes() > 0) {
serverBootstrap.childOption(ChannelOption.SO_SNDBUF, Math.toIntExact(tcpSendBufferSize.getBytes()));
}
final ByteSizeValue tcpReceiveBufferSize = SETTING_HTTP_TCP_RECEIVE_BUFFER_SIZE.get(settings);
if (tcpReceiveBufferSize.getBytes() > 0) {
serverBootstrap.childOption(ChannelOption.SO_RCVBUF, Math.toIntExact(tcpReceiveBufferSize.getBytes()));
}
serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, recvByteBufAllocator);
serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, recvByteBufAllocator);
final boolean reuseAddress = SETTING_HTTP_TCP_REUSE_ADDRESS.get(settings);
serverBootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, reuseAddress);
this.boundAddress = createBoundHttpAddress();
if (logger.isInfoEnabled()) {
logger.info("{}", boundAddress);
}
success = true;
} finally {
if (success == false) {
doStop(); // otherwise we leak threads since we never moved to started
}
}
}
示例12: createServerBootstrap
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
private void createServerBootstrap(String name, Settings settings) {
if (logger.isDebugEnabled()) {
logger.debug("using profile[{}], worker_count[{}], port[{}], bind_host[{}], publish_host[{}], compress[{}], "
+ "connect_timeout[{}], connections_per_node[{}/{}/{}/{}/{}], receive_predictor[{}->{}]",
name, workerCount, settings.get("port"), settings.get("bind_host"), settings.get("publish_host"), compress,
defaultConnectionProfile.getConnectTimeout(),
defaultConnectionProfile.getNumConnectionsPerType(TransportRequestOptions.Type.RECOVERY),
defaultConnectionProfile.getNumConnectionsPerType(TransportRequestOptions.Type.BULK),
defaultConnectionProfile.getNumConnectionsPerType(TransportRequestOptions.Type.REG),
defaultConnectionProfile.getNumConnectionsPerType(TransportRequestOptions.Type.STATE),
defaultConnectionProfile.getNumConnectionsPerType(TransportRequestOptions.Type.PING),
receivePredictorMin, receivePredictorMax);
}
final ThreadFactory workerFactory = daemonThreadFactory(this.settings, TRANSPORT_SERVER_WORKER_THREAD_NAME_PREFIX, name);
final ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(new NioEventLoopGroup(workerCount, workerFactory));
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childHandler(getServerChannelInitializer(name, settings));
serverBootstrap.childOption(ChannelOption.TCP_NODELAY, TCP_NO_DELAY.get(settings));
serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, TCP_KEEP_ALIVE.get(settings));
final ByteSizeValue tcpSendBufferSize = TCP_SEND_BUFFER_SIZE.getDefault(settings);
if (tcpSendBufferSize != null && tcpSendBufferSize.getBytes() > 0) {
serverBootstrap.childOption(ChannelOption.SO_SNDBUF, Math.toIntExact(tcpSendBufferSize.getBytes()));
}
final ByteSizeValue tcpReceiveBufferSize = TCP_RECEIVE_BUFFER_SIZE.getDefault(settings);
if (tcpReceiveBufferSize != null && tcpReceiveBufferSize.getBytes() > 0) {
serverBootstrap.childOption(ChannelOption.SO_RCVBUF, Math.toIntExact(tcpReceiveBufferSize.bytesAsInt()));
}
serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, recvByteBufAllocator);
serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, recvByteBufAllocator);
final boolean reuseAddress = TCP_REUSE_ADDRESS.get(settings);
serverBootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, reuseAddress);
serverBootstrap.validate();
serverBootstraps.put(name, serverBootstrap);
}
示例13: start
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
public void start(int port) {
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
ethereumChannelInitializer = ctx.getBean(EthereumChannelInitializer.class, "");
ethereumListener.trace("Listening on port " + port);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT);
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout());
b.handler(new LoggingHandler());
b.childHandler(ethereumChannelInitializer);
// Start the client.
logger.info("Listening for incoming connections, port: [{}] ", port);
logger.info("NodeId: [{}] ", Hex.toHexString(config.nodeId()));
channelFuture = b.bind(port).sync();
listening = true;
// Wait until the connection is closed.
channelFuture.channel().closeFuture().sync();
logger.debug("Connection is closed");
} catch (Exception e) {
logger.debug("Exception: {} ({})", e.getMessage(), e.getClass().getName());
throw new Error("Server Disconnected");
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
listening = false;
}
}
示例14: bootstrapNetty
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
/**
* Bootstraps netty, the server that handles all openflow connections
*/
public void bootstrapNetty() {
try {
bossGroup = new NioEventLoopGroup();
workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_REUSEADDR, true)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_SNDBUF, Controller.SEND_BUFFER_SIZE);
OFChannelInitializer initializer = new OFChannelInitializer(
this,
this,
debugCounterService,
floodlightProvider.getTimer(),
ofBitmaps,
defaultFactory,
keyStore,
keyStorePassword);
bootstrap.childHandler(initializer);
cg = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
Set<InetSocketAddress> addrs = new HashSet<InetSocketAddress>();
if (floodlightProvider.getOFAddresses().isEmpty()) {
cg.add(bootstrap.bind(new InetSocketAddress(InetAddress.getByAddress(IPv4Address.NONE.getBytes()), floodlightProvider.getOFPort().getPort())).channel());
} else {
for (IPv4Address ip : floodlightProvider.getOFAddresses()) {
addrs.add(new InetSocketAddress(InetAddress.getByAddress(ip.getBytes()), floodlightProvider.getOFPort().getPort()));
}
}
for (InetSocketAddress sa : addrs) {
cg.add(bootstrap.bind(sa).channel());
log.info("Listening for switch connections on {}", sa);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}