简体   繁体   English

JMS超时或TimeToLive

[英]JMS Timeout or TimeToLive

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. 用户点击一个servlet。 A message is then put into a JMS server/Queue from this servlet. 然后将一条消息从此servlet放入JMS服务器/队列中。 A response is then sent back to the user saying "Message Queued". 然后将响应发送回给用户,说“消息已排队”。

Option 1 选项1

The consumer/MDB receives the message from the JMS queue and processes it. 使用者/ MDB从JMS队列接收消息并进行处理。 This is normal operation and pretty standard. 这是正常操作,很标准。

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. 因此,通过设置timeToLive消息将从队列中逐出。 The problem is that the is no "interface/call back" to know that the message was evicted. 问题在于,没有“接口/回叫”来知道消息已被逐出。 It just disappears. 它只是消失了。 Or am I mistaken? 还是我弄错了?

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. 对此问题的任何见解将不胜感激。

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). 但是,大多数(如果不是全部)JMS实现确实提供了DLQ(死信队列)的概念。 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,而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实现一个附加的侦听器以执行错误/超时处理。

Synchronous interaction over JMS might be of help to you either. 通过JMS进行同步交互也可能对您有所帮助。 Basicly on the client side you: 基本上在客户端上,您:

  1. send a message with a correlation id and time-to-live 发送具有相关性ID和生存时间的消息
  2. 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并指定超时(生存时间==超时,因此如果您将其视为无效,则它实际上是无效的)会收到一条消息(通常在同一线程中)

On the other side, server: 另一方面,服务器:

  1. on an incoming message must fetch the correlation id 在传入消息上必须获取相关ID
  2. specify that correlation id for a response while sending it back to the client. 为响应指定相关ID,同时将其发送回客户端。 Of course server must be quick enough to fit the timeout/time-to-live threshold. 当然,服务器必须足够快以适合超时/生存时间阈值。

So on the client side you are always sure what's happend to the message that was sent. 因此,在客户端,您始终可以确定所发送的消息发生了什么。

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

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