本文整理汇总了Java中io.netty.channel.nio.NioEventLoopGroup类的典型用法代码示例。如果您正苦于以下问题:Java NioEventLoopGroup类的具体用法?Java NioEventLoopGroup怎么用?Java NioEventLoopGroup使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NioEventLoopGroup类属于io.netty.channel.nio包,在下文中一共展示了NioEventLoopGroup类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: NettyServer
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的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.nio.NioEventLoopGroup; //导入依赖的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();
}
}
示例3: start
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的package包/类
public void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(this.host, this.port))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("connected server...");
ch.pipeline().addLast(new ByteArrayEncoder());
ch.pipeline().addLast(new ByteArrayDecoder());
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture cf = b.connect().sync();
cf.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
示例4: start
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的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();
}
}
示例5: start
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的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();
}
}
示例6: start
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的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();
}
}
示例7: init
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的package包/类
public void init() throws SyncException {
cg = new DefaultChannelGroup("Cluster Bootstrap", GlobalEventExecutor.INSTANCE);
workerExecutor = new NioEventLoopGroup();
timer = new HashedWheelTimer();
bootstrap = new Bootstrap()
.group(workerExecutor)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_REUSEADDR, true)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_SNDBUF, RPCService.SEND_BUFFER_SIZE)
.option(ChannelOption.SO_RCVBUF, RPCService.SEND_BUFFER_SIZE)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, RPCService.CONNECT_TIMEOUT);
pipelineFactory = new BootstrapChannelInitializer(timer, this);
bootstrap.handler(pipelineFactory);
}
示例8: main
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try{
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new HttpServer());
ChannelFuture channelFuture = serverBootstrap
.bind(new InetSocketAddress("0.0.0.0", PORT))
.sync();
channelFuture.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
示例9: start
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的package包/类
public void start(String ip, int port) throws Exception {
// Configure SSL.
final SslContext sslCtx;
if (SSL) {
sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
} else {
sslCtx = null;
}
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).handler(new FileClientInitializer(sslCtx));
Channel ch = b.connect(ip, port).sync().channel();
ConfigurationContext.propMap.putIfAbsent(SOCKET_CHANNEL, ch);
}catch(Exception e){
e.printStackTrace();
}
}
示例10: startAsync
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的package包/类
public Future startAsync() {
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new SocksServerInitializer())
.childAttr(OPTION_ATTRIBUTE_KEY, option);
return bootstrap.bind(option.getLocalHost(), option.getLocalPort()).addListener(
new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (infoEnable) {
if (future.isSuccess()) {
logger.info("Listening on local port {}", option.getLocalPort());
} else {
logger.info("Shadowsocks client startup failed", future.cause());
}
}
}
});
}
示例11: RemotingNettyClient
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的package包/类
private RemotingNettyClient(final NettyClientConfig nettyClientConfig) {
super(nettyClientConfig.getOnewaySemaphoreValue(), nettyClientConfig.getAsyncSemaphoreValue());
int publicThreadNums = nettyClientConfig.getCallbackExecutorThreads();
if (publicThreadNums <= 0) {
publicThreadNums = 4;
}
this.publicExecutor = Executors.newFixedThreadPool(publicThreadNums, new ThreadFactory() {
private AtomicInteger threadIndex = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "NettyClientPublicExecutor_" + this.threadIndex.incrementAndGet());
}
});
group = new NioEventLoopGroup(nettyClientConfig.getWorkerThreads(), new CustomThreadFactory("client"));
start();
}
示例12: UDPServerSocket
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的package包/类
public UDPServerSocket(ThreadedLogger logger, int port, String interfaz) {
this.logger = logger;
try {
bootstrap = new Bootstrap();
group = new NioEventLoopGroup();
bootstrap
.group(group)
.channel(NioDatagramChannel.class)
.handler(this);
channel = bootstrap.bind(interfaz, port).sync().channel();
} catch (Exception e) {
this.logger.critical("**** FAILED TO BIND TO " + interfaz + ":" + port + "!");
this.logger.critical("Perhaps a server is already running on that port?");
System.exit(1);
}
}
示例13: run
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的package包/类
/**
* Start the server
*
* @param port The port on which the server listen to
*/
public void run(final int port) {
final EventLoopGroup bossGroup = new NioEventLoopGroup();
final EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
final ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ServerInitializer())
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.TCP_NODELAY, true);
final ChannelFuture f = bootstrap.bind(port).sync();
LOGGER.info("NettyServer: running on port {}", port);
f.channel().closeFuture().sync();
} catch (final InterruptedException e) {
LOGGER.error("NettyServer: an error occurred while running: {}", e.getMessage());
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
示例14: run
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的package包/类
public void run() {
workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
// b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(65536, 0, 4, 0, 4));
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast("decoder", new MsgPackDecode());
pipeline.addLast("encoder", new MsgPackEncode());
pipeline.addLast(new ClientHandler());
}
});
channel = b.connect(clientProperties.getServerHost(), clientProperties.getServerPort()).sync().channel();
status = Status.START;
channel.closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
}
status = Status.STOP;
}
示例15: start
import io.netty.channel.nio.NioEventLoopGroup; //导入依赖的package包/类
public void start(String hostName, int port) {
Executors.newSingleThreadExecutor().submit(() -> {
group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new KyChannelInitializer());
if (hostName != null && !hostName.equals(""))
bootstrap.remoteAddress(new InetSocketAddress(hostName, port));
else
bootstrap.remoteAddress(new InetSocketAddress(port));
ChannelFuture channelFuture = null;
try {
channelFuture = bootstrap.connect().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
startListenerHandle(channelFuture, launchListener);
});
}