io.netty.channel.oio.OioEventLoopGroup Java Examples

The following examples show how to use io.netty.channel.oio.OioEventLoopGroup. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RxtxClient.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventLoopGroup group = new OioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group)
         .channel(RxtxChannel.class)
         .handler(new ChannelInitializer<RxtxChannel>() {
             @Override
             public void initChannel(RxtxChannel ch) throws Exception {
                 ch.pipeline().addLast(
                     new LineBasedFrameDecoder(32768),
                     new StringEncoder(),
                     new StringDecoder(),
                     new RxtxClientHandler()
                 );
             }
         });

        ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();

        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}
 
Example #2
Source File: NettyOioServer.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void server(int port) throws Exception {
	final ByteBuf buf = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!\r\n", Charset.forName("UTF-8")));
	EventLoopGroup group = new OioEventLoopGroup();
	try {
		ServerBootstrap b = new ServerBootstrap();
		b.group(group).channel(OioServerSocketChannel.class).localAddress(new InetSocketAddress(port))
				.childHandler(new ChannelInitializer<SocketChannel>() {
					@Override
					public void initChannel(SocketChannel ch) throws Exception {
						ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
							@Override
							public void channelActive(ChannelHandlerContext ctx) throws Exception {
								ctx.writeAndFlush(buf.duplicate()).addListener(ChannelFutureListener.CLOSE);
							}
						});
					}
				});
		ChannelFuture f = b.bind().sync();
		f.channel().closeFuture().sync();
	} finally {
		group.shutdownGracefully().sync();
	}
}
 
Example #3
Source File: RxtxClient.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventLoopGroup group = new OioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group)
         .channel(RxtxChannel.class)
         .handler(new ChannelInitializer<RxtxChannel>() {
             @Override
             public void initChannel(RxtxChannel ch) throws Exception {
                 ch.pipeline().addLast(
                     new LineBasedFrameDecoder(32768),
                     new StringEncoder(),
                     new StringDecoder(),
                     new RxtxClientHandler()
                 );
             }
         });

        ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();

        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}
 
Example #4
Source File: BlockingEchoServer.java    From netty.book.kor with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventLoopGroup bossGroup = new OioEventLoopGroup(1);
    EventLoopGroup workerGroup = new OioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
        .channel(OioServerSocketChannel.class)
        .childHandler(new ChannelInitializer<SocketChannel>() {
           @Override
           public void initChannel(SocketChannel ch) {
               ChannelPipeline p = ch.pipeline();
               p.addLast(new EchoServerHandler());
           }
        });

        ChannelFuture f = b.bind(8888).sync();

        f.channel().closeFuture().sync();
    }
    finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}
 
Example #5
Source File: BuiltInServerManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private Future<?> startServerInPooledThread() {
  if (!started.compareAndSet(false, true)) {
    return null;
  }

  return myApplication.executeOnPooledThread(() -> {
    try {
      ImportantFolderLocker locker = StartupUtil.getLocker();

      BuiltInServer mainServer = locker instanceof ImportantFolderLockerViaBuiltInServer ? ((ImportantFolderLockerViaBuiltInServer)locker).getServer() : null;
      if (mainServer == null || mainServer.getEventLoopGroup() instanceof OioEventLoopGroup) {
        server = BuiltInServer.start(1, getDefaultPort(), PORTS_COUNT, false, null);
      }
      else {
        server = BuiltInServer.start(mainServer.getEventLoopGroup(), false, getDefaultPort(), PORTS_COUNT, true, null);
      }
      bindCustomPorts(server);
    }
    catch (Throwable e) {
      LOG.info(e);
      NOTIFICATION_GROUP.getValue().createNotification("Cannot start internal HTTP server. Git integration, Some plugins may operate with errors. " +
                                                       "Please check your firewall settings and restart " + ApplicationNamesInfo.getInstance().getFullProductName(),
                                                       NotificationType.ERROR).notify(null);
      return;
    }

    LOG.info("built-in server started, port " + server.getPort());

    Disposer.register(myApplication, server);
  });
}
 
Example #6
Source File: NettyOioServer.java    From code with Apache License 2.0 4 votes vote down vote up
public void server(int port)
        throws Exception {
    final ByteBuf buf =
            Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!\r\n", StandardCharsets.UTF_8));
    EventLoopGroup group = new OioEventLoopGroup();
    try {
        //创建 ServerBootstrap
        ServerBootstrap b = new ServerBootstrap();
        b.group(group)
                //使用 OioEventLoopGroup以允许阻塞模式(旧的I/O)
                .channel(OioServerSocketChannel.class)
                .localAddress(new InetSocketAddress(port))
                //指定 ChannelInitializer,对于每个已接受的连接都调用它
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch)
                            throws Exception {
                        ch.pipeline().addLast(
                                //添加一个 ChannelInboundHandlerAdapter以拦截和处理事件
                                new ChannelInboundHandlerAdapter() {
                                    @Override
                                    public void channelActive(
                                            ChannelHandlerContext ctx)
                                            throws Exception {
                                        ctx.writeAndFlush(buf.duplicate())
                                                .addListener(
                                                        //将消息写到客户端,并添加 ChannelFutureListener,
                                                        //以便消息一被写完就关闭连接
                                                        ChannelFutureListener.CLOSE);
                                    }
                                });
                    }
                });
        //绑定服务器以接受连接
        ChannelFuture f = b.bind().sync();
        f.channel().closeFuture().sync();
    } finally {
        //释放所有的资源
        group.shutdownGracefully().sync();
    }
}
 
Example #7
Source File: DefaultChannelPipelineTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 3000)
public void testAddInListenerOio() throws Throwable {
    testAddInListener(new OioSocketChannel(), new OioEventLoopGroup(1));
}
 
Example #8
Source File: OioSctpLimitStreamsTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected EventLoopGroup newEventLoopGroup() {
    return new OioEventLoopGroup();
}
 
Example #9
Source File: RpcClient.java    From TakinRPC with Apache License 2.0 4 votes vote down vote up
public RpcClient(OioEventLoopGroup eventLoopGroup, EventExecutorGroup eventExecutor) {
    this(eventLoopGroup, eventExecutor, OioSocketChannel.class);
}
 
Example #10
Source File: RpcServer.java    From TakinRPC with Apache License 2.0 4 votes vote down vote up
public RpcServer(OioEventLoopGroup eventLoopGroup, EventExecutorGroup eventExecutor, @Assisted SocketAddress address) {
    this(eventLoopGroup, eventExecutor, OioServerSocketChannel.class, address);
}
 
Example #11
Source File: SocketChannelResolverTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void worksWithOioEventLoopGroupFactory() {
    assertThat(resolveSocketChannelFactory(new OioEventLoopGroup()).newChannel()).isInstanceOf(OioSocketChannel.class);
}
 
Example #12
Source File: NettyKt.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static Bootstrap oioClientBootstrap() {
  Bootstrap bootstrap = new Bootstrap().group(new OioEventLoopGroup(1, PooledThreadExecutor.INSTANCE)).channel(OioSocketChannel.class);
  bootstrap.option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true);
  return bootstrap;
}