博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
netty jni epoll
阅读量:7167 次
发布时间:2019-06-29

本文共 4216 字,大约阅读时间需要 14 分钟。

hot3.png

netty4.0.17 提供了基于c的epoll   采用ET工作模式 下面直接分享 基于接口 使用这一特性  目前已经替换在内网测试服务器上面 运行良好   

以下是简单的替换:

public void runEpollProxy() throws Exception {        // Configure the bootstrap.        EpollEventLoopGroup BossEventLoopGroup=new EpollEventLoopGroup(0x1,new PriorityThreadFactory("@+监听连接线程",Thread.NORM_PRIORITY)); //mainReactor    1个线程        EpollEventLoopGroup WorkerEventLoopGroup=new EpollEventLoopGroup(Runtime.getRuntime().availableProcessors()+ 0x1,new PriorityThreadFactory("@+I/O线程",Thread.NORM_PRIORITY));   //subReactor       线程数量等价于cpu个数+1        try {            ServerBootstrap serverBootstrap = new ServerBootstrap();            serverBootstrap.group(BossEventLoopGroup, WorkerEventLoopGroup)                    .channel(NioServerSocketChannel.class)                    .childOption(ChannelOption.TCP_NODELAY, true)                    .childOption(ChannelOption.SO_KEEPALIVE, true)                    .childOption(ChannelOption.SO_REUSEADDR, true)     //重用地址                    .childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(false))// heap buf 's better                    .childOption(ChannelOption.SO_RCVBUF, 1048576)                    .childOption(ChannelOption.SO_SNDBUF, 1048576)                    .childHandler(new ProxyServerChannelInitializer(remoteHost, remotePort))  ;            ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress(localPort)).sync();            LOGGER.debug("proxy server监听端口:"+ localPort);            channelFuture.channel().closeFuture().sync();        } finally {            BossEventLoopGroup.shutdownGracefully();            WorkerEventLoopGroup.shutdownGracefully();        }    }

public void runProxy() throws Exception {        // Configure the bootstrap.        NioEventLoopGroup BossEventLoopGroup=new NioEventLoopGroup(0x1,new PriorityThreadFactory("@+监听连接线程",Thread.NORM_PRIORITY)); //mainReactor    1个线程        NioEventLoopGroup WorkerEventLoopGroup=new NioEventLoopGroup(Runtime.getRuntime().availableProcessors()+ 0x1,new PriorityThreadFactory("@+I/O线程",Thread.NORM_PRIORITY));   //subReactor       线程数量等价于cpu个数+1        try {            ServerBootstrap serverBootstrap = new ServerBootstrap();            serverBootstrap.group(BossEventLoopGroup, WorkerEventLoopGroup)                    .channel(NioServerSocketChannel.class)                    .childOption(ChannelOption.TCP_NODELAY, true)                    .childOption(ChannelOption.SO_KEEPALIVE, true)                    .childOption(ChannelOption.SO_REUSEADDR, true)     //重用地址                    .childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(false))// heap buf 's better                    .childOption(ChannelOption.SO_RCVBUF, 1048576)                    .childOption(ChannelOption.SO_SNDBUF, 1048576)                    .childHandler(new ProxyServerChannelInitializer(remoteHost, remotePort))  ;                ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress(localPort)).sync();                LOGGER.debug("proxy server监听端口:"+ localPort);               channelFuture.channel().closeFuture().sync();        } finally {            BossEventLoopGroup.shutdownGracefully();            WorkerEventLoopGroup.shutdownGracefully();        }    }

public class PriorityThreadFactory implements ThreadFactory {    private int _prio;    private String _name;    private AtomicInteger _threadNumber = new AtomicInteger(1);    private ThreadGroup _group;    /**     *     * @param name 线程池名     * @param priority   线程池优先级     */    public PriorityThreadFactory(String name, int priority){        _prio = priority;        _name = name;        _group = new ThreadGroup(_name);    }    @Override    public Thread newThread(Runnable r){        Thread t = new Thread(_group, r);        t.setName(_name + "-"+"#-" + _threadNumber.getAndIncrement());        t.setPriority(_prio);        return t;    }    public ThreadGroup getGroup(){        return _group;    }}

 只需要简单的替换就搞定  

 环境配置:

     ## RHEL/CentOS/Fedora:

     #sudo yum install autoconf automake libtool glibc-devel.i686 glibc-devel libgcc.i686 make
     ## Debian/Ubuntu:
     #sudo apt-get install autoconf automake libtool make gcc-multilib
就这样 就可以咯 有问题留言

转载于:https://my.oschina.net/chenleijava/blog/203005

你可能感兴趣的文章
c# 线程状态及转换
查看>>
windows批处理命令FOR的巧用
查看>>
Android Task 任务栈
查看>>
python实用程序-HTTP服务调用系统命令(带白名单)
查看>>
H3C S5500-28F-EI 清除配置
查看>>
load average
查看>>
浏览器标签栏logo添加
查看>>
setjmp()/longjmp()的使用方法
查看>>
SQL Server 2008R2的各种问题及解决方案
查看>>
为什么字符串正则里有好多的反斜杠
查看>>
A.Eugeny and Array
查看>>
rzchecktree实现单选以及隐藏选择框
查看>>
amazon 面经3
查看>>
hibernate主键详细介绍
查看>>
【整理】uclibc,eglibc,glibc之间的区别和联系
查看>>
Python Scrapy 爬虫(四):部署与运行
查看>>
bat 每天开机自动从git/svn服务器更新代码
查看>>
Poj 3669 Meteor Shower
查看>>
深度学习【二】机器学习的通用流程
查看>>
具有参考意义的博客园地址
查看>>