简体   繁体   English

Java中的多线程持久队列

[英]multi threaded persistent queue in java

Advice required on architecting module 架构模块所需的建议

I have a PORT Listener that listens to a PORT. 我有一个侦听PORT的PORT Listener。 All the requests received needs to be queued. 所有收到的请求都需要排队。 I have 5 threads to process the queue. 我有5个线程来处理队列。 At the same time all the incoming requests has to be persisted and so is the responses on processing the same. 同时,所有传入的请求都必须保留,处理相同的响应也必须保留。

What is the best method to implement that? 最好的实现方法是什么?

Requests and responses can't be persisted together as there may be cases when I have to temporarily stop processing for few hours. 由于某些情况下我不得不暂时停止处理几个小时,因此请求和响应无法持久保存。 Another situation may be failure of this application. 另一种情况可能是此应用程序失败。 We should be able to resume the queue from the persistent storage. 我们应该能够从持久性存储中恢复队列。

Another issue is we can't guarantee the size of the queue. 另一个问题是我们不能保证队列的大小。 So the time taken to fetch item continuously from db should be less. 因此,从db连续获取项目所需的时间应该更少。 There may be times with no requests in DB. 有时数据库中可能没有请求。 Should I go for NOSQL? 我应该去NOSQL吗?

The basic concept is to use a persistent queue. 基本概念是使用持久队列。

Your request-handler (a Servlet, for example) should transform requests into a JMS messages and send them via a JMS Queue. 您的请求处理程序(例如Servlet)应将请求转换为JMS消息并通过JMS队列发送。 You can use ActiveMQ , HornetQ , or others. 您可以使用ActiveMQHornetQ或其他。 Next, create a Message Driven Bean that consumes messages from that queue. 接下来,创建一个使用该队列中消息的消息驱动Bean。

The JMS queues are persistent, so if your server breaks down, you will not lose your tasks. JMS队列是持久性的,因此,如果服务器发生故障,您将不会丢失任务。 The MDB can be controlled (for example using JMX, or System properties) so you can stop processing any time and tasks will be stored inside the queue until you enable the MDB again. 可以控制MDB(例如使用JMX或系统属性),因此您可以随时停止处理,并且任务将存储在队列中,直到再次启用MDB。

Here is a complete JMS walk-through from Oracle that will show you step by step how to do it. 这是Oracle的完整JMS演练 ,将逐步向您展示如何做。

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

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