简体   繁体   English

具有溢出到磁盘的Java FIFO队列

[英]Java FIFO Queue with spill-over to disk

I'm working/preparing an application that is based on a producer/consumer model. 我正在/准备一个基于生产者/消费者模型的应用程序。 In my case there will be one producer which generates several million (non-trivial) tasks, and there will be a configurable number of consumers. 在我的情况下,将有一个生产者生成数百万 (非平凡)任务,并且将有可配置数量的消费者。

Communication between producer and consumers is basically Queue-based. 生产者和消费者之间的沟通基本上是基于队列的。 I'm worried however about memory-consumption: it's quite conceivable that the number of tasks will exceed the available memory for the JVM. 然而我担心内存消耗:可以想象任务数量将超过JVM的可用内存。 So I'd like to have a Queue implementation that only keeps the "top-X" number of queue items in memory, and stores the rest on disk. 所以我想要一个Queue实现只保留内存中“top-X”数量的队列项,并将其余的存储在磁盘上。 This does not have to be resilient in that it does not need to survive a restart of the program. 这不必具有弹性,因为它不需要在重新启动程序后继续存在。

I've searched around, but can't find a Queue implementation that seems to be in widespread use (there do seem to be a few, what I call, proof-of-concept implementations, but I'm worried about future support/continued development of those implementations). 我一直在搜索,但找不到一个似乎广泛使用的队列实现(似乎有一些,我称之为,概念验证实现,但我担心未来的支持/继续开发这些实现)。 I'm also looked at external Messaging Queue applications, but (1) I don't want to run a second external process and (2) even embedding a message broker inside the same JVM process seems a bit "top heavy" for this requirement. 我也看了外部消息队列应用程序,但是(1)我不想运行第二个外部进程和(2)甚至在同一个JVM进程中嵌入消息代理似乎对这个要求有点“头重脚轻” 。

Does anybody know of any well-supported future-proof library out there that provides this functionality? 有没有人知道任何提供此功能的良好支持的面向未来的库?

Rgds RGDS

Well, JMS seems like the obvious solution. 好吧,JMS似乎是一个明显的解决方案。 I don't think you'll find something solid to solve this problem since JMS solves it and is a standard solution. 我认为你不会找到解决这个问题的可靠方法,因为JMS解决了这个问题并且是标准的解决方案。

Note however that Java also has BoundedQueues to solve this problem: dimension the queue to make sure it won't fail with an OOME when the queue is full, and producers will be blocked while trying to put a message in the full bounded queue until some task is removed from the queue by one of the consumers. 但请注意,Java也有BoundedQueues来解决这个问题:对队列进行维度以确保在队列满时它不会因OOME而失败,并且生成器在尝试将消息放入完整的有界队列时将被阻塞,直到某些其中一个消费者将任务从队列中删除。

Having the producer block when there is more than enough tasks to be consumed is usually more efficient than letting the queue grow and consume more memory. 当有足够多的任务被消耗时,生成器块通常比让队列增长和消耗更多内存更有效。 eg if your queue fits in the queue it can be several times faster than a queue which doesn't. 例如,如果您的队列适合队列,它可能比没有队列的队列快几倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM