简体   繁体   English

Java中的持久阻塞队列?

[英]Persistent Blocking Queue in Java?

TL;DR; TL; DR; I need to know if there's a lib with persistent blocking queue that performatic. 我需要知道是否存在具有持久阻塞队列的lib,这是一种性能。

I hava a classic producer/consumers program. 我有一个经典的生产者/消费者计划。 They share a LinkedBlockingQueue to share the data, and I use BlockingQueue#take method in the Consumers, as I need them to live forever waiting for new elements. 他们共享一个LinkedBlockingQueue来共享数据,我在消费者中使用BlockingQueue#take方法,因为我需要他们永远等待新元素。

The problem is I have LOTS of data and I can't lose them. 问题是我有很多数据,我不能丢失它们。 Even after the consumers stops, the producer can persist to generate some data. 即使在消费者停止之后,生产者也可以坚持生成一些数据。 I am thinking about implementing my BlockingQueue ta uses H2 behind to store/get the data after some threshold is reached. 我正在考虑实现我的BlockingQueue ta后面使用H2来存储/获取一些阈值后的数据。 My main problem is that I need performance and I need to consume the elements in the order they are created. 我的主要问题是我需要性能,我需要按照创建的顺序使用元素。

Is there an implementation of persistent blocking queue that I can use for something like this? 是否有持久阻塞队列的实现,我可以用于这样的事情? If it doesn't, any sugestions for me to achieve something like this? 如果没有,那么我可以通过什么方式来实现这样的目标呢?

You can try ActiveMQ . 你可以试试ActiveMQ The ActiveMQ can write to your file system so if the producer is generating many more elements than the consumer can take you either have a lot of blocking (on whatever the upper bound of the queue is) or excessive data (if there is no upper bound to the queue). ActiveMQ可以写入您的文件系统,因此如果生产者生成的元素多于消费者可以带来的元素,则要么有很多阻塞(无论队列的上限是什么)还是过多的数据(如果没有上限)到队列)。

I would use ActiveMQ lib and Spring JMS, here is a usage example 我会使用ActiveMQ lib和Spring JMS,这是一个用法示例

start broker 启动经纪人

BrokerService broker = new BrokerService();
broker.addConnector("tcp://localhost:61616");
broker.start();

read msg 阅读消息

ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
JmsTemplate t = new JmsTemplate(cf);
Message msg = t.receive();

send message 发信息

ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
JmsTemplate t = new JmsTemplate(cf);
t.send("test", new MessageCreator() {
  public Message createMessage(Session session) throws JMSException {
    return session.createTextMessage("test");
  }
});

您是否遇到过Amazon SQS无限制的队列且速度非常快,它保证了订单。您想要持久保存数据吗?

You can use any JMS implementation for supporting excess incoming data. 您可以使用任何JMS实现来支持过多的传入数据。 This is a producer consume problem and jms is designed for this. 这是一个生产者消费问题,jms就是为此而设计的。

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

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