本文整理汇总了Java中io.netty.channel.socket.nio.NioServerSocketChannel类的典型用法代码示例。如果您正苦于以下问题:Java NioServerSocketChannel类的具体用法?Java NioServerSocketChannel怎么用?Java NioServerSocketChannel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NioServerSocketChannel类属于io.netty.channel.socket.nio包,在下文中一共展示了NioServerSocketChannel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: NettyServer
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
private NettyServer(){
pGroup = new NioEventLoopGroup();
cGroup = new NioEventLoopGroup();
serverBootstrap = new ServerBootstrap();
serverBootstrap.group(pGroup, cGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
//设置日志
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
sc.pipeline().addLast(new ReadTimeoutHandler(60));
sc.pipeline().addLast(new NettyServerHandler());
}
});
}
示例2: start
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
public void start(SlaveNode slaveNode) {
if(slaveNode==null){
throw new IllegalArgumentException("slaveNode is null");
}
EventLoopGroup bossGroup = new NioEventLoopGroup(CommonConstants.BOSS_GROUP_SIZE, new DefaultThreadFactory("boss", true));
EventLoopGroup workerGroup = new NioEventLoopGroup(CommonConstants.WORKER_GROUP_SIZE, new DefaultThreadFactory("worker", true));
try {
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new SlaveServerInitializer());
ChannelFuture future = b.bind(slaveNode.getPort()).sync();
LOGGER.info("SlaveServer Startup at port:{}",slaveNode.getPort());
// 等待服务端Socket关闭
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
LOGGER.error("InterruptedException:",e);
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
示例3: start
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
public void start() {
EventLoopGroup bossGroup = new NioEventLoopGroup(CommonConstants.BOSS_GROUP_SIZE, new DefaultThreadFactory("boss", true));
EventLoopGroup workerGroup = new NioEventLoopGroup(CommonConstants.WORKER_GROUP_SIZE, new DefaultThreadFactory("worker", true));
try {
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new MasterServerInitializer());
ChannelFuture future = b.bind(CommonConstants.SERVER_PORT).sync();
LOGGER.info("MasterServer Startup at port:{}",CommonConstants.SERVER_PORT);
// 等待服务端Socket关闭
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
LOGGER.error("InterruptedException:",e);
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
示例4: start
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
public void start() {
EventLoopGroup bossGroup = new NioEventLoopGroup(CommonConstants.BOSS_GROUP_SIZE, new DefaultThreadFactory("boss", true));
EventLoopGroup workerGroup = new NioEventLoopGroup(CommonConstants.WORKER_GROUP_SIZE, new DefaultThreadFactory("worker", true));
try {
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ServerInitializer());
ChannelFuture future = b.bind(CommonConstants.SERVER_PORT).sync();
logger.info("NettyServer Startup at port:{}",CommonConstants.SERVER_PORT);
// 等待服务端Socket关闭
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
logger.error("InterruptedException:",e);
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
示例5: bind
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
@Override
public void bind(int port) {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(8888))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new Encoder(serializer), new Decoder(serializer), new ProviderHandler());
}
});
bootstrap.bind(port);
}
示例6: start
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
@Override
public void start() throws Exception {
try {
ServerBootstrap b = new ServerBootstrap()
.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.childHandler(serverInitializer);
logger.info("Starting TcpChatServer... Port: " + port);
channelFuture = b.bind(port).sync();
} finally {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
shutdown();
}
});
}
}
示例7: start
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
@Override
public void start() throws Exception {
try {
ServerBootstrap b = new ServerBootstrap()
.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.childHandler(serverInitializer);
logger.info("Starting WebSocketChatServer... Port: " + port);
channelFuture = b.bind(port).sync();
} finally {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
shutdown();
}
});
}
}
示例8: main
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_REUSEADDR, true)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast(new SimpleServerHandler());
}
});
b.bind(8090).sync().channel().closeFuture().sync();
}
示例9: AbstractNettyServer
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的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());
}
});
}
示例10: start
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
@Override
public void start(final int port) {
bossGroup = new NioEventLoopGroup(bossGroupThreads);
workerGroup = new NioEventLoopGroup(workerGroupThreads);
ServerBootstrap serverBootstrap = new ServerBootstrap();
try {
serverBootstrap
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, backlogSize)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.TCP_NODELAY, true)
.childHandler(serializeType.getServerChannelInitializer().newInstance());
channel = serverBootstrap.bind(port).sync().channel();
} catch (final Exception ex) {
throw new ServerException(Server.SYSTEM_MESSAGE_ID, ex);
}
}
示例11: doOpen
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的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();
}
}
示例12: start
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
/**
* 启动服务
*
* @throws Exception 异常
*/
public void start() throws Exception {
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(channelInitializer)
.option(ChannelOption.SO_BACKLOG, aceServerConfig.getBackSize())
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
ChannelFuture future = bootstrap.bind(aceServerConfig.getPort()).sync();
System.out.println("ace server starter on port : " + aceServerConfig.getPort());
future.channel().closeFuture().sync();
} finally {
close();
}
}
示例13: Receiver
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
public Receiver ( final ReceiverHandlerFactory factory, final SocketAddress addr )
{
this.factory = factory;
this.bossGroup = new NioEventLoopGroup ();
this.workerGroup = new NioEventLoopGroup ();
this.bootstrap = new ServerBootstrap ();
this.bootstrap.group ( this.bossGroup, this.workerGroup );
this.bootstrap.channel ( NioServerSocketChannel.class );
this.bootstrap.option ( ChannelOption.SO_BACKLOG, 5 );
this.bootstrap.option ( ChannelOption.SO_REUSEADDR, true );
this.bootstrap.childHandler ( new ChannelInitializer<SocketChannel> () {
@Override
protected void initChannel ( final SocketChannel ch ) throws Exception
{
handleInitChannel ( ch );
}
} );
this.channel = this.bootstrap.bind ( addr ).channel ();
logger.info ( "Receiver running ..." );
}
示例14: start
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
@Override
public synchronized void start() {
bossGroup = new NioEventLoopGroup(); // (1)
workerGroup = new NioEventLoopGroup();
try {
b = new ServerBootstrap(); // (2)
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new SocketServerChannelInitializer(heartTime,socketService,applicationContext));
// Bind and start to accept incoming connections.
b.bind(port);
logger.info("socket: "+port+" starting....");
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
} catch (Exception e) {
e.printStackTrace();
}
}
示例15: bindToPlainSocket
import io.netty.channel.socket.nio.NioServerSocketChannel; //导入依赖的package包/类
private ChannelFuture bindToPlainSocket() throws InterruptedException {
String hostname = configuration.getHostName();
int port = Integer.parseInt(configuration.getPlain().getPort());
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new SocketChannelInitializer(ioExecutors))
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
// Bind and start to accept incoming connections.
ChannelFuture future = b.bind(hostname, port).sync();
LOGGER.info("Listening AMQP on " + hostname + ":" + port);
return future;
}