我需要在同一台机器(两个不同的 JVM)上运行的两个 Java 应用程序之间共享数据。我准确地说,要共享的数据很大(大约7GB)。应用程序必须非常快速地访问数据,因为它们必须以非常高的速率回答传入的查询。我不希望应用程序为每个应用程序保存一份数据副本。
我发现一种选择是使用内存映射文件。应用程序 A 从某个地方(假设是数据库)获取数据并将其存储在文件中。然后应用程序 B 可以使用 java.nio 访问这些文件。我不知道内存映射文件到底是如何工作的,我只知道数据存储在一个文件中,并且该文件(或其一部分)映射到内存的某个区域(虚拟内存?)。因此,这两个应用程序可以读写内存中的数据,并且更改会自动(我猜?)提交到文件。我也不知道文件完全映射到内存中是否有最大大小。
我的第一个问题是,在这种情况下两个应用程序共享数据的不同可能性是什么(我的意思是考虑到数据量非常大并且访问这些数据必须非常快)?我明确指出,这个问题与内存映射 I/O 无关,它只是想知道解决同一问题的其他方法是什么。
我的第二个问题是使用内存映射文件的优点和缺点是什么?
谢谢
I need to share data between two Java applications running on the same machine (two different JVMs). I precise that the data to be shared is large (about 7 GB). The applications must access the data very fast because they have to answer incoming queries at a very high rate. I don't want the applications to hold each one a copy of the data.
I've seen that one option is to use memory-mapped files. Application A gets the data from somewhere (let's say a database) and stores it in files. Then application B may access these files using java.nio
. I don't know exactly how memory-mapped files work, I only know that the data is stored in a file and that this file (or a part of it) is mapped to a region of the memory (virtual memory?). So, the two applications can read-write the data in memory and the changes are automatically (I guess?) committed to the file. I also don't know if there is a maximum size for a file to be entirely mapped in memory.
My first question is what are the different possibilities for two applications to share data in this scenario (I mean taking into account that the amount of data is very large and that access to this data must be very fast)? I precise that this question is not related to memory-mapped I/O, it just to know what are the other ways to solve the same problem.
My second question is what are the pros and cons of using memory-mapped files?
Thanks
发布评论
评论(2)
正如 S.Lott 指出的,有很多机制:
AF_UNIX
或AF_INET
或AF_INET6
优点:
缺点:
As S.Lott points out, there's a lot of mechanisms:
AF_UNIX
orAF_INET
orAF_INET6
Pros:
Cons:
内存映射文件听起来很让人头疼。一个简单且不易出错的选项是使用具有集群感知缓存的共享数据库。这样,只有写入会写入数据库,而读取则可以从缓存中进行。
作为如何在 hibernate 中执行此操作的示例,请参阅 http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-cache
Memory mapped files sounds like a headache. A simple option and less error prone would be to use a shared database with a cluster aware cache. That way only writes go down to the database and reads can be served from the cache.
As an example of how to do this in hibernate see http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-cache