java JMS 超时或生存时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7526367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JMS Timeout or TimeToLive
提问by Paul
I am fairly new to Java EE and JMS and am looking at doing an implementation using JMS.
我是 Java EE 和 JMS 的新手,正在考虑使用 JMS 进行实现。
Think of the following scenario:
考虑以下场景:
Scenario
设想
A user hits a servlet. A message is then put into a JMS server/Queue from this servlet. A response is then sent back to the user saying "Message Queued".
用户点击了一个 servlet。然后将一条消息从该 servlet 放入 JMS 服务器/队列。然后将响应发送回用户,说“消息已排队”。
Option 1
选项1
The consumer/MDB receives the message from the JMS queue and processes it. This is normal operation and pretty standard.
使用者/MDB 从 JMS 队列接收消息并对其进行处理。这是正常操作,非常标准。
Option 2
选项 2
There is no consumer(for what ever reason) or the receiver is processing messages too slow. So what I would like is for the message in the queue to timeout. Once timed out, and email should be sent etc (email is just as an example).
没有消费者(无论出于何种原因)或接收者处理消息的速度太慢。所以我想要的是队列中的消息超时。一旦超时,应发送电子邮件等(电子邮件仅作为示例)。
Reading the API spec/Java EE 6 tutorial I have found in the QueuSender class
阅读我在 QueuSender 类中找到的 API 规范/Java EE 6 教程
void send(Message message, int deliveryMode, int priority, long timeToLive)
So by settings the timeToLive the message will be evicted from the queue. The problem is that the is no "interface/call back" to know that the message was evicted. It just disappears. Or am I mistaken?
因此,通过设置 timeToLive 消息将从队列中逐出。问题是没有“接口/回调”来知道消息已被驱逐。它只是消失了。还是我错了?
Another approach I thought of was for a thread to monitor the queue and evict messages that are "expired" and pull them from the queue. But I don't think that is possible, is it?
我想到的另一种方法是让线程监视队列并驱逐“过期”的消息并将它们从队列中拉出。但我不认为这是可能的,是吗?
Any light shed on this matter would greatly be appreciated.
对此问题的任何了解将不胜感激。
采纳答案by jarnbjo
You have to make use of some implementation specific functionality to fulfill your requirements. The JMS specification does neither define which action is taken with a timed out message, nor does it offer you any reasonable criteria selection when polling messages from a queue.
您必须利用某些特定于实现的功能来满足您的要求。JMS 规范既没有定义对超时消息采取的操作,也没有为您从队列轮询消息时提供任何合理的标准选择。
Most (if not all) JMS implementations do however offer the concept of DLQs (dead letter queues). If a message cannot be delivered to a regular consumer or times out, the JMS implementation will most likely be able to move the message to a DLQ, which is basically also a regular queue with its own listener.
然而,大多数(如果不是全部)JMS 实现确实提供了 DLQ(死信队列)的概念。如果消息无法传递给常规使用者或超时,JMS 实现很可能能够将消息移动到 DLQ,它基本上也是一个具有自己的侦听器的常规队列。
So, if you set up two queues, Q1 and Q2 and configure Q2 as a DLQ for Q1, you would do your normal request processing in a listener on Q1 and implement an additional listener for Q2 to do the error/timeout handling.
因此,如果您设置两个队列 Q1 和 Q2,并将 Q2 配置为 Q1 的 DLQ,您将在 Q1 上的侦听器中执行正常请求处理,并为 Q2 实现一个额外的侦听器来执行错误/超时处理。
回答by Ivan Velykorodnyy
Synchronous interaction over JMS might be of help to you either. Basicly on the client side you:
JMS 上的同步交互也可能对您有所帮助。基本上在客户端,您:
- send a message with a correlation id and time-to-live
- receive a message (usually in the same thread) using the same correlation id and specifying timeout (time-to-live == timeout so if you treat it dead, it's really dead)
- 发送带有关联 ID 和生存时间的消息
- 接收一条消息(通常在同一个线程中)使用相同的相关 id 并指定超时(生存时间 == 超时所以如果你把它当作死的,它真的死了)
On the other side, server:
另一方面,服务器:
- on an incoming message must fetch the correlation id
- specify that correlation id for a response while sending it back to the client. Of course server must be quick enough to fit the timeout/time-to-live threshold.
- 在传入消息上必须获取相关 ID
- 在将响应发送回客户端时为响应指定相关 ID。当然,服务器必须足够快以适应超时/生存时间阈值。
So on the client side you are always sure what's happend to the message that was sent.
因此,在客户端,您始终可以确定发送的消息发生了什么。