简体   繁体   English

Netty客户端相对于I / O客户端的好处?

[英]Benefits of Netty Client over I/O Client?

I want to know if I can save my application threads by implementing Netty Client. 我想知道是否可以通过实施Netty Client保存我的应用程序线程。

I wrote a demo client please find the below code. 我编写了一个演示客户端,请找到以下代码。 Expecting that a single thread can connect to different port handle them efficiently but i was wrong. 期望单个线程可以连接到不同的端口可以有效地处理它们,但是我错了。 Netty creates per thread connection. Netty为每个线程创建连接。

public class NettyClient {
    public static void main(String[] args) {
    Runnable runA = new Runnable() {
        public void run() {
            Connect(5544);
        }
    };

    Thread threadA = new Thread(runA, "threadA");
    threadA.start();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException x) {
    }

    Runnable runB = new Runnable() {
        public void run() {
            Connect(5544);
        }
    };

    Thread threadB = new Thread(runB, "threadB");
    threadB.start();

}

static ClientBootstrap bootstrap = null;
static NettyClient ins = new NettyClient();
public NettyClient() {

    bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(
            Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    /*
     * ClientBootstrap A helper class which creates a new client-side
     * Channel and makes a connection attempt.
     * 
     * NioClientSocketChannelFactory A ClientSocketChannelFactory which
     * creates a client-side NIO-based SocketChannel. It utilizes the
     * non-blocking I/O mode which was introduced with NIO to serve many
     * number of concurrent connections efficiently
     * 
     * There are two types of threads :Boss thread Worker threads Boss
     * Thread passes control to worker thread.
     */
    // Configure the client.

    ChannelGroup channelGroup = new DefaultChannelGroup(NettyClient.class.getName());
    // Only 1 thread configured but still aceepts threadA and Thread B
    // connection
    OrderedMemoryAwareThreadPoolExecutor pipelineExecutor = new OrderedMemoryAwareThreadPoolExecutor(
            1, 1048576, 1073741824, 1, TimeUnit.MILLISECONDS,
            new NioDataSizeEstimator(), new NioThreadFactory("NioPipeline"));

    bootstrap.setPipelineFactory(new NioCommPipelineFactory(channelGroup,
            pipelineExecutor));

    // bootstrap.setPipelineFactory(new
    // BackfillClientSocketChannelFactory());
    bootstrap.setOption("child.tcpNoDelay", true);

    bootstrap.setOption("child.keepAlive", true);
    bootstrap.setOption("child.reuseAddress", true);
    bootstrap.setOption("readWriteFair", true);
}

public static NettyClient getins() {
    return ins;
}

public static void Connect(int port) {
    ChannelFuture future = bootstrap
            .connect(new InetSocketAddress("localhost", port));

    Channel channel = future.awaitUninterruptibly().getChannel();
    System.out.println(channel.getId());
    channel.getCloseFuture().awaitUninterruptibly();
}

} }

Now I want to know what are the benefits of using Netty client? 现在我想知道使用Netty客户端有什么好处? Does it save Threads? 是否保存线程?

Netty saves threads. Netty保存线程。 Your NettyClient wastes threads when waiting synchronously for opening and closing of the connections (calling awaitUninterruptibly()). 当同步等待打开和关闭连接(调用awaitUninterruptible())时,您的NettyClient浪费线程。

BTW how many connections will your client have? 顺便说一句,您的客户有多少个连接? Maybe using classic synchronous one-thread-per-connection approach would suffice? 也许使用经典的同步每连接一个线程方法就足够了? Usually we have to save threads on a server side. 通常我们必须将线程保存在服务器端。

Netty allows you to handle thousands of connections with a handful of threads . Netty允许您使用少量线程处理数千个连接 When used in a client application, it allows a handful of threads to make thousands of concurrent connections to server. 在客户端应用程序中使用时,它允许少数线程与服务器建立数千个并发连接。

You have put sleep() in your thread. 您已将sleep()放入线程中。 We must never block the Netty worker/boss threads. 我们绝不能阻止Netty工人/老板线程。 Even if there is a need to perform any one-off blocking operation, it must be off-loaded to another executor. 即使需要执行一次一次性阻止操作,也必须将其卸载给另一位执行者。 Netty uses NIO, and the same thread can be used for creating a new connection, while the earlier connection gets some data in its input buffer. Netty使用NIO,同一线程可用于创建新连接,而较早的连接在其输入缓冲区中获取一些数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM