netty笔记-:EpollEventLoopGroup:Caused by: java.lang.ExceptionInInitializerError:Caused by: java.lang.IllegalStateException: Only supported on Linux
今天在翻看netty的源码的时候发现netty对EventLoopGroup的实现有不止常用的NIOEventLoopGroup ,一共有以下几种。
- EpollEventLoopGroup
- NioEventLoopGroup
- KQueueEventLoopGroup
其中NioEventLoopGroup则是我们比较常用的,这个使用了java NIO中的SelectorProvider.provider()来选择系统默认的selectorProvider(即多路复用IO的支持)。
public NioEventLoopGroup(ThreadFactory threadFactory) {
this(0, threadFactory, SelectorProvider.provider());
}
而JDK则会根据自己的版本来返回默认的。
public static SelectorProvider provider() {
synchronized (lock) {
if (provider != null)
return provider;
return AccessController.doPrivileged(
new PrivilegedAction<SelectorProvider>() {
public SelectorProvider run() {
if (loadProviderFromProperty())
return provider;
if (loadProviderAsService())
return provider;
provider = sun.nio.ch.DefaultSelectorProvider.create();
return provider;
}
});
}
}
如果我们没有在参数中指定,那么provider = sun.nio.ch.DefaultSelectorProvider.create();这句代码则会找到当前jdk版本默认的selectorProvider。 不同的系统对应的默认selectorProvider不一样。
oracle jdk也会根据发行的不同操作系统的版本来提供不同的selectorProvider实现 例如windows的
则是WindowsSelectorProvider (注意本文是以oracle jdk为例,open jdk这儿的实现是不一样的)
public class DefaultSelectorProvider {
private DefaultSelectorProvider() {
}
public static SelectorProvider create() {
return new WindowsSelectorProvider();
}
}
而BSD/MAC OS则是KQueue->KQueueSelectorProvider ,linux则是Epoll->EPollSelectorProvider(注意linux 2.5.44以后才提供,之前均为select/poll) 。而java NIO会根据jvm的版本来选择合适的selectorProvider,使用这个是确保不会出现系统
切换的问题,即java跨平台的特性还是得到保证的。那netty为何还要多此一举的为EPollSelectorProvider和KQueueSelectorProvider单独提供对应的EventLoopGroup即EpollEventLoopGroup和KQueueEventLoopGroup呢。要知道手动指定会有操作系统不匹配的风险,例如如下代码
public class Server {
public static void main(String[] args) throws InterruptedException {
ServerBootstrap serverBootstrap = new ServerBootstrap();
ChannelFuture channelFuture = serverBootstrap.group(new EpollEventLoopGroup(1) , new EpollEventLoopGroup(10))
.channel(EpollServerSocketChannel.class)
.handler(new LoggingHandler())
.childHandler(new InitialierHandler())
.bind(8080)
.sync();
channelFuture.channel().closeFuture().sync();
}
}
如果这段代码在windows或者mac os上运行则会报本文标题上的错 (将其中EpollEnventLoopGroup换为NIOEventLoopGroup,EpollServerSocketChannel 换为NIOsERVERSocketChannel则可以平台通用)
Exception in thread "main" java.lang.UnsatisfiedLinkError: failed to load the required native library
at io.netty.channel.epoll.Epoll.ensureAvailability(Epoll.java:80)
at io.netty.channel.epoll.EpollEventLoop.<clinit>(EpollEventLoop.java:51)
at io.netty.channel.epoll.EpollEventLoopGroup.newChild(EpollEventLoopGroup.java:150)
at io.netty.channel.epoll.EpollEventLoopGroup.newChild(EpollEventLoopGroup.java:35)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:84)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:58)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:47)
at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:59)
at io.netty.channel.epoll.EpollEventLoopGroup.<init>(EpollEventLoopGroup.java:112)
at io.netty.channel.epoll.EpollEventLoopGroup.<init>(EpollEventLoopGroup.java:99)
at io.netty.channel.epoll.EpollEventLoopGroup.<init>(EpollEventLoopGroup.java:76)
at io.netty.channel.epoll.EpollEventLoopGroup.<init>(EpollEventLoopGroup.java:52)
at netty.Server.main(Server.java:22)
Caused by: java.lang.ExceptionInInitializerError
at io.netty.channel.epoll.Epoll.<clinit>(Epoll.java:39)
... 12 more
Caused by: java.lang.IllegalStateException: Only supported on Linux
at io.netty.channel.epoll.Native.loadNativeLibrary(Native.java:225)
at io.netty.channel.epoll.Native.<clinit>(Native.java:58)
... 13 more
俗话说,有风险,肯定就有收益。netty单独提供的epoll/KQueue实现肯定是有性能上的优化的。这儿可以引用一下官方文档,地址:https://netty.io/wiki/native-transports.html
其中大概的解释了下原因。

即相比于java NIO,提供了更多的特定平台的功能,产生更少的垃圾,总体上提高了性能。java JDK因为要保证跨平台所以在功能上必须做出妥协

所以我们在构建项目的时候可以根据项目指定环境的不同来指定不同的EnventLoopGroup提升性能。例如开发环境 windows可以使用java的NIO ,mac可以使用netty定制的KqueueSelector.
生产环境一般为linux则可以使用netty定制的EpollSelector来提高负载性能
netty笔记-:EpollEventLoopGroup:Caused by: java.lang.ExceptionInInitializerError:Caused by: java.lang.IllegalStateException: Only supported on Linux的更多相关文章
- java.lang.ExceptionInInitializerError Caused by: org.hibernate.InvalidMappingException: Unable to read XML
此错误是说无法读取你的xml文档,于是我们就该去更改xml文档,因为我是自动生成的,所以我找了一份之前手写的,发现是dtd错了,把之前的dtd拷贝过来之后程序就测试通过了
- java.lang.ExceptionInInitializerError异常
今天在开发的过程中,遇到java.lang.ExceptionInInitializerError异常,百度查了一下,顺便学习学习,做个笔记 静态初始化程序中发生意外异常的信号,抛出Exception ...
- Exception in thread "main" java.lang.ExceptionInInitializerError
Exception in thread "main" java.lang.ExceptionInInitializerErrorCaused by: java.util.Missi ...
- springboot下jar包方式运行Caused by: java.lang.ExceptionInInitializerError: null
idea调试过程中不会出现此问题,异常如下 org.springframework.beans.factory.BeanCreationException: Error creating bean w ...
- java.lang.ExceptionInInitializerError /NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
java.lang.ExceptionInInitializerError at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Nati ...
- java.lang.ExceptionInInitializerError
java.lang.ExceptionInInitializerError at com.csdhsm.compiler.test.DevTest.testReadInput(DevTest.java ...
- android java.lang.ExceptionInInitializerError
11-08 13:36:05.108: E/AndroidRuntime(5318): java.lang.ExceptionInInitializerError 11-08 13:36:05.108 ...
- Java java.lang.ExceptionInInitializerError 错误解决方案
引起 java.lang.ExceptionInInitializerError 错误的原因是:在类的初始化时,出错.也就是说,在加载类时,执行static的属性.方法块时,出错了. 比如 publi ...
- java含有静态代码块新建的时候报错java.lang.ExceptionInInitializerError
问题描述 最近在写一些单元测试用例,为了避免连接外界服务,所有选择mock了数据库Dao层,计划将数据库所需要的数据存在List中,在类加载的时候初始化List并且填充数据.代码如下: public ...
随机推荐
- [CF]Round511
这场比赛我及时的参加了,但是打的时候状态实在是太烂了,只做出来了Div2的AB题. A Little C loves 3 I 直接构造就行. B Cover Points 应该很容易就看出来这个等腰三 ...
- java 中使用MD5加密 , 入库时对密码进行加密
import lombok.extern.slf4j.Slf4j; import java.security.MessageDigest; @Slf4j public class MD5Util { ...
- codeforces 1282B2. K for the Price of One (Hard Version) (dp)
链接 https://codeforces.com/contest/1282/problem/B2 题意: 商店买东西,商店有n个物品,每个物品有自己的价格,商店有个优惠活动,当你买恰好k个东西时可以 ...
- Mybatis学习笔记——输入参数parameterType、Mybatis调用存储过程
输入参数:parameterType(两种取值符号) 1.类型为简单类型 区别: (1) #{可以为任意值} ${vaue}--->标识符只能是value (2) ...
- SuperSocket与SuperSocket.ClientEngine实现Protobuf协议
参考资料说明 SuperSocket文档 http://docs.supersocket.net/ Protobuf语言参考 https://developers.google.com/protoco ...
- 大数据-es(elasticsearch)
elasticsearch elasticsearch是lucene作为核心的实时分布式检索,底层使用倒排索引实现. 倒排索引原理 索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址.由于不 ...
- Virtual Judge HDU 1241 Oil Deposits
八方向 深搜 #include <iostream> #include<cstdio> #include<cstdlib> #include<algori ...
- 通过FormData对象可以组装一组用 [XMLHttpRequest]发送请求的键/值对,它可以更灵活方便的发送表单数据。
工作记录用 1 大概页面,点击选择按钮,选择文件,填写备注并可以上传前预览,然后点击上传按钮开始上传 2 html+js代码 <h2>Test</h2> <div id= ...
- 【14】 DFS 机器人活动范围 (static插曲)
题目 地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] .一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左.右.上.下移动一格(不能移动到方格外),也不能进入行坐 ...
- sudo用户找不到环境变量 sudo找不到/usr/local/bin 下的执行文件,
出于安全方面的考虑,使用sudo执行命令将在一个最小化的环境中执行,环境变量都重置成默认状态. 所以PATH这个变量不包括用户自定义设置的内容,如找不到/usr/local/bin/下面的命令在sud ...