我正在寻找一个Java进程间通信库。我想在JVM之间发送小消息,并希望尽可能使用共享内存进行通信。
try {
int shmSize = 1024;
RandomAccessFile file = new RandomAccessFile("shm.raw","rw");
// inialize file size
if(file.length() < shmSize) {
byte[] tmp = new byte[shmSize];
file.write(tmp);
file.seek(0); // seek back to start of file.
}
// memory-map file.
FileChannel ch = file.getChannel();
MappedByteBuffer shm = ch.map(FileChannel.MapMode.READ_WRITE, 0, shmSize);
ch.close(); // channel not needed anymore.
shm.load(); // force file into physical memory.
// now use the ByteBuffer's get/put/position methods to read/write the shared memory
} catch(Exception e) { e.printStackTrace(); }