- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 PLC4X 项目中,我们使用 Netty 作为客户端连接到充当服务器的 PLC。有时,由于用户错误或 PLC 错误,连接不被接受而是被拒绝。如果我们多次重试尽快建立连接,我们会遇到错误消息 Too many open files
.
我尝试清理代码中的所有内容,因此我假设没有可能泄漏的文件描述符:
try {
final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workerGroup);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.option(ChannelOption.TCP_NODELAY, true);
// TODO we should use an explicit (configurable?) timeout here
// bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000);
bootstrap.handler(channelHandler);
// Start the client.
final ChannelFuture f = bootstrap.connect(address, port);
f.addListener(new GenericFutureListener<Future<? super Void>>() {
@Override public void operationComplete(Future<? super Void> future) throws Exception {
if (!future.isSuccess()) {
logger.info("Unable to connect, shutting down worker thread.");
workerGroup.shutdownGracefully();
}
}
});
// Wait for sync
f.sync();
f.awaitUninterruptibly(); // jf: unsure if we need that
// Wait till the session is finished initializing.
return f.channel();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new PlcConnectionException("Error creating channel.", e);
} catch (Exception e) {
throw new PlcConnectionException("Error creating channel.", e);
}
ulimit
事情,因为 Netty(在 macOS 上)只需要一些 fd 才能使用?还是我漏了什么东西?
最佳答案
我找到了解决方案,有点像我自己。
原始实现中有 2 个问题(甚至可能是 3 个),它们与 Mac OS X 没有真正的关系:
workerGroup.shutdownGracefully()
在另一个线程中触发,因此主(被调用)线程已经完成 try {
final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workerGroup);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.option(ChannelOption.TCP_NODELAY, true);
// TODO we should use an explicit (configurable?) timeout here
// bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000);
bootstrap.handler(channelHandler);
// Start the client.
logger.trace("Starting connection attempt on tcp layer to {}:{}", address.getHostAddress(), port);
final ChannelFuture f = bootstrap.connect(address, port);
// Wait for sync
try {
f.sync();
} catch (Exception e) {
// Shutdown worker group here and wait for it
logger.info("Unable to connect, shutting down worker thread.");
workerGroup.shutdownGracefully().awaitUninterruptibly();
logger.debug("Worker Group is shutdown successfully.");
throw new PlcConnectionException("Unable to Connect on TCP Layer to " + address.getHostAddress() + ":" + port, e);
}
// Wait till the session is finished initializing.
return f.channel();
}
catch (Exception e) {
throw new PlcConnectionException("Error creating channel.", e);
}
关于java - Nettys FileDescriptor 在 OS X 上的使用情况如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60182502/
我希望在我的应用程序中的某些点写入文件后强制同步到磁盘。由于它在 Linux 上运行,我可以运行 Runtime.getRuntime().exec("sync"); 但是,我宁愿不介绍 Linux
我有一个或多个非阻塞 FileDescriptor 对象(已经创建并从某个子系统传递给我)。我想等待,然后使用某种 select() 读取它们。我如何在 Java (Android) 中执行此操作?我
FileDescriptor Android 中的 API 说: Instances of the file descriptor class serve as an opaque handle to
我的应用需要执行以下操作: 打开一个FileInputStream,获取底层的FileDescriptor(通过getFd()) 根据上述FileDescriptor创建新的FileInputStre
我通过 FileOutputStream 写入文件,该文件通过其构造函数采用 FileDescriptor 打开。 我想要的行为:当我写入文件时,我希望它是唯一的内容。例如。写入“Hello”应该导致
想知道以下场景(简化的 Java 代码): //Thread A: FileOutputStream fos = new FileOutputStream(PATH); OutputStreamWri
这个问题已经有答案了: Why is java.io.FileDescriptor's constructor public? (2 个回答) 已关闭 8 年前。 我正在浏览 Java 的 API,想
我是 android 和 java 的初学者。 但是,我想使用 MediaRecorder 来录制语音并将数据记录为 byte[],我编写了以下代码: ParcelFileDescriptor[] f
给定一个 FileDescriptor 对象,是否可以通过某种方式从中创建一个 File? 谢谢! 最佳答案 不容易,如果有的话。来自documentation : Instances of the
在 Java 中,我们可以使用 Scanner 类来获取输入,但它不如 IO Package 的 BufferedReader 效率高。在初始化 Scanner 类的对象或 BufferedReade
假设我有一个有效的 Java FileDescriptor,它是通过这种方式获得的: FileInputStream is = new FileInputStream("/some/path/to/f
我有一些基于 asyncore lib 的 python 脚本,具有超过 3k 的传出套接字连接。由于连接限制 (1024),我无法使用 select(..),但 poll(..) 也无法正常工作:
在 Android 中,是否可以直接从字节数组生成 FileDescriptor 而无需先打开文件? 在 Android 2.2 中,我即时生成一个 MIDI 文件,然后使用 MediaPlayer
我正在使用 Intent 调用我的应用程序外部的文件选择器。一些文件选择器应用程序返回方案“内容”的 Uri。 我需要获取所选对象的最后修改日期。当方案是“内容”时我该怎么做?我没有找到合适的 API
关于我的问题的一些快速背景: 我正在编写一个将域类型强制规范转换为 Java 安全管理器代码的编译器。简而言之,DTE 定义“类型”(对象),为这些类型分配路径;然后定义“域”(主题),并定义域对各种
来自官方API : 您知道以下构造函数实际上有用的真实情况吗? 文件读取器 public FileReader(FileDescriptor fd) Creates a new FileReader,
我有一个FileDescriptor并且想打开它以使用单个 channel 进行读写。我可以使用像这样的流来获取一个用于读取的 channel 和一个用于写入的 channel ,但我更喜欢单个 ch
可能,这是一个复杂的问题,但我会尝试拍摄。 我使用 Monodroid(Android 的 Mono),并尝试将媒体流实现到 UDP 套接字。 Best way to stream audio and
我尝试使用带有 FileDescriptor 的 SoundPool 的加载函数,因为我希望能够设置偏移量和长度。该文件不存储在资源中,而是存储卡上的文件。即使 SoundPool 的加载和播放功能都
java.io.FileDescriptor.FileDescriptor() 的 JavaDoc说: Constructs an (invalid) FileDescriptor object. 如
我是一名优秀的程序员,十分优秀!