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 ...
随机推荐
- 赋值SQL语句
UPDATE TAB_DEV_MS SET DT_DETECTION_STARTTIME = TO_DATE ( '2017-01-01 00:00:00', 'YYYY-MM-DD HH24:MI: ...
- (转)Boyer-Moore算法
转自:Boyer-Moore算法 一.简述 在当前用于查找子字符串的算法中,BM(Boyer-Moore)算法是当前有效且应用比较广的一中算法,各种文本编辑器的“查找”功能(Ctrl+F),大多采用B ...
- Android基础知识 -- Fragment
Fragment是android3.0后提供的API(所以android:minSdkVersion="11"以上版本),主要针对平板UI.有自己的生命周期,但是必须依附在Acti ...
- python之路面向对象2
一.利用反射查看面向对象成员的归属 二.利用反射导入模块.查找类.创建对象.查找对象中的字段 三.静态字段 静态字段存在类中,把对象每个都有的存在类中就行了,只存一份 四.静态方法 静态方法中没有se ...
- 故障解决 | win10没声音及找不到Realtek高清音频管理器
重装 win10 系统后,电脑没声音,更新驱动以及万不得已下载驱动精灵都没有解决. 后来发现在“硬件和声音”中没有Realtek高清音频管理器,之后找到解决办法如下: 1. 找到Realtek高清音频 ...
- php设计模式之多态实例代码
<?php header("Content-type:text/html;charset=utf-8"); /** * 虎 */ abstract class Tiger { ...
- Go_json
package main import ( "encoding/json" "fmt" ) // 结构体与json // 1.序列化: 把Go语言中的结构体变量 ...
- (转)Java中的String与常量池
Java中的String与常量池 转自:http://developer.51cto.com/art/201106/266454.htm string是java中的字符串.String类是不可变的,对 ...
- nmon+python 基于AIX系统数据分析
https://sourceforge.net/projects/pynmongraph/ github :https://github.com/madmaze/pyNmonAnalyzer nmon ...
- Bugku-CTF之sql注入2 (全都tm过滤了绝望吗?)
Day 38 sql注入2 200 http://123.206.87.240:8007/web2/ 全都tm过滤了绝望吗? 提示 !,!=,=,+,-,^,%