本文整理汇总了Java中io.netty.bootstrap.ServerBootstrap.channel方法的典型用法代码示例。如果您正苦于以下问题:Java ServerBootstrap.channel方法的具体用法?Java ServerBootstrap.channel怎么用?Java ServerBootstrap.channel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.bootstrap.ServerBootstrap
的用法示例。
在下文中一共展示了ServerBootstrap.channel方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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());
}
示例2: AbstractNettyServer
import io.netty.bootstrap.ServerBootstrap; //导入方法依赖的package包/类
protected AbstractNettyServer(String serverName) {
this.serverName = Objects.requireNonNull(serverName, "server name");
bootstrap = new ServerBootstrap();
if (Epoll.isAvailable()) {
bootstrap.option(ChannelOption.SO_BACKLOG, 1024).channel(EpollServerSocketChannel.class)
.childOption(ChannelOption.SO_LINGER, 0).childOption(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true);
log.info(serverName + " epoll init");
} else {
bootstrap.channel(NioServerSocketChannel.class);
log.info(serverName + " nio init");
}
bootstrap.group(bossGroup, workerGroup).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.childOption(ChannelOption.TCP_NODELAY, true).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
initPipeline(ch.pipeline());
}
});
}
示例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;
}
}