nio

Uniquely identify file in Java

南笙酒味 提交于 2021-01-26 11:01:13
问题 Im on Linux and my Java application is not intended to be portable. I'm looking for a way to identify a file uniquely in Java. I can make use of statfs syscall since the pair (f_fsid, ino) uniquely identifies a file (not only across a file system) as specified here: http://man7.org/linux/man-pages/man2/statfs.2.html The question is if it is possible extract fsid from Java directly so I can avoid writing JNI function? inode can be extracted with NIO , but how about fsid? inode and fsid comes

How safe is it to use Java FileLock?

跟風遠走 提交于 2021-01-24 07:53:05
问题 How safe is it to use java.nio.channels.FileLock for locking files among processes? It is stated that other processes can not access the file if we have an exclusive lock. However, the below answer on another SO question states other processes have to check for the filelock too in order for our process to be safe. (a) Are you aware that locking the file won't keep other processes from touching it unless they also use locks? So I tested my code and tried to change, a file which I have the lock

Java阻塞IO与非阻塞IO

别等时光非礼了梦想. 提交于 2021-01-06 05:22:09
IO: IO 是主存和外部设备 ( 硬盘、终端和网络等 ) 拷贝数据的过程。 IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成。 阻塞与非阻塞: 一辆从 A 开往 B 的公共汽车上,路上有很多点可能会有人下车。司机不知道哪些点会有哪些人会下车,对于需要下车的人,如何处理更好? 司机过程中定时询问每个乘客是否到达目的地,若有人说到了,那么司机停车,乘客下车。 ( 类似阻塞式 ) 每个人告诉售票员自己的目的地,然后睡觉,司机只和售票员交互,到了某个点由售票员通知乘客下车。 ( 类似非阻塞 ) 很显然,每个人要到达某个目的地可以认为是一个线程,司机可以认为是 CPU 。在阻塞式里面,每个线程需要不断的轮询,上下文切换,以达到找到目的地的结果。而在非阻塞方式里,每个乘客 ( 线程 ) 都在睡觉 ( 休眠 ) ,只在真正外部环境准备好了才唤醒,这样的唤醒肯定不会阻塞。 阻塞式I/O:(传统的IO) 以网络应用为例,在传统IO方式(阻塞IO)中需要监听一个ServerSocket,接受请求的连接为其提供服务(服务通常包括了处理请求并发送响应)下图是服务器的生命周期图,其中标有粗黑线条的部分表明会发生I/O阻塞。 此方式在遇到多请求时,只能等待前面的请求完成后才能处理新的请求,所以通常在Java中处理阻塞I/O要用到线程(大量的线程)。代码如下 public class

java nio-3

a 夏天 提交于 2021-01-05 08:17:44
到目前为止,我们只是使用缓冲区将数据从一个通道转移到另一个通道。然而,程序经常需要直接处理数据。例如,您可能需要将用户数据保存到磁盘。在这种情况下,您必须将这些数据直接放入缓冲区,然后用通道将缓冲区写入磁盘。 或者,您可能想要从磁盘读取用户数据。在这种情况下,您要将数据从通道读到缓冲区中,然后检查缓冲区中的数据。 实际上,每一个基本类型的缓冲区都为我们提供了直接访问缓冲区中数据的方法,我们以ByteBuffer为例,分析如何使用其提供的get()和put()方法直接访问缓冲区中的数据。 The get () methods: ByteBuffer类中有四个get()方法: byte get(); ByteBuffer get( byte dst[] ); ByteBuffer get( byte dst[], int offset, int length ); byte get( int index ); 第一个方法获取单个字节。第二和第三个方法将一组字节读到一个数组中。第四个方法从缓冲区中的特定位置获取字节。那些返回ByteBuffer的方法只是返回调用它们的缓冲区的this值。 此外,我们认为前三个get()方法是相对的,而最后一个方法是绝对的。“相对”意味着get()操作服从limit和position值,更明确地说,字节是从当前position读取的

How can we combine readLockCheckInterval and maxMessagesPerPoll in camel file component configuration?

老子叫甜甜 提交于 2020-12-11 08:58:22
问题 We are facing problem that camel file component readLockCheckInterval allow single file to be processed at a time and for next file camel lock, it will wait for readLockCheckInterval time. We have 10000 or more files which we want to process in parallel. I want to use maxMessagesPerPoll attribute for accessing multiple file per poll but with readLockCheckInterval because camel release the file lock if the file is still being copied. It will be great help if there will be any other way to

How can we combine readLockCheckInterval and maxMessagesPerPoll in camel file component configuration?

北战南征 提交于 2020-12-11 08:56:38
问题 We are facing problem that camel file component readLockCheckInterval allow single file to be processed at a time and for next file camel lock, it will wait for readLockCheckInterval time. We have 10000 or more files which we want to process in parallel. I want to use maxMessagesPerPoll attribute for accessing multiple file per poll but with readLockCheckInterval because camel release the file lock if the file is still being copied. It will be great help if there will be any other way to

基于NIO的消息路由的实现(五) 服务端连接管理

限于喜欢 提交于 2020-11-11 10:34:43
一、对客户端连接(也就是SocketChannel)的管理 客户端与服务端建立连接后,服务端来维护其连接,首先定义了一个GVConnection类,用来定义连接; 此类包括四个成员:token、userId、channel、lastAccessTime;如下: public class GVConnection { private String token; private String userId; private SocketChannel channel; private int lastAccessTime; public GVConnection(String token,String userId, SocketChannel channel, int lastAccessTime) { this.token = token; this.userId = userId; this.channel = channel; this.lastAccessTime = lastAccessTime; } public void setLastAccessTime(int lastAccessTime){ this.lastAccessTime = lastAccessTime; } public SocketChannel getChannel() { return