本文介绍了Spring Integration在没有poller的情况下侦听队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Spring Integration实现HTTP端点,Spring Integration监听http请求,将请求数据作为消息发送到通道,另一个端点应该监听此通道上的消息并处理它们。

I want to implement the HTTP endpoint using Spring Integration, which listen to the http requests, sends the request data as messages to the channel and another endpoint should listen messages on this channel and process them.

听起来很简单。但我想要实现的目标是:

Sounds simple. But what I want to achieve is:


  1. 消息应该按顺序处理。

  2. 消息应该asap处理(如果队列已经为空,则在http请求之后没有延迟)。

  3. 一旦收到消息,就应该响应http请求,而不是在处理之后,http请求应该响应,所以发件人只会知道收到的邮件需要处理。

  4. 我不想使用像RabbitMQ这样的外部队列。

  1. Messages should be processed in order.
  2. Messages should be processed asap (without delay after http request, if the queue is already empty).
  3. The http request should be responded as soon as message is received, not after it is processed, so the sender will know only that message is received for processing.
  4. I don't want to use external queues, like RabbitMQ.

所以我需要一个 QueueChannel 。但如果我理解正确,从队列接收消息的唯一方法是 poller 。所以第2点不会令人满意。在收到消息之后和轮询器看到之前会有很小的延迟。

So I need a QueueChannel for this. But if I understand correctly, the only way to receive messages from the queue is the poller. So the point 2 will not be satisfied. There will be small delay after message received and before poller sees it.

所以问题是:有没有简单的方法在Spring Integration中实现这一点,我不这样做看?

So the question is: is there any simple way to achieve this in Spring Integration which I don't see?

我当然可以自己实施。例如,创建 SmartLifeCycle 组件,该组件侦听 DirectChannel ,并将消息放入 java。 util.concurrent.BlockingQueue ,并且还启动一个专用线程,它将在此队列上等待并将消息发送到另一个 DirectChannel 进行处理。所以没有延迟,因为一旦 BlockingQueue 不为空,线程就会被解锁。

Of course I can implement it myself. For example creating SmartLifeCycle component, which listen on DirectChannel and just put the messages into java.util.concurrent.BlockingQueue, and also starts a dedicated thread which will wait on this queue and send the message into another DirectChannel for processing. So there will be no delay because thread will be unblocked as soon as BlockingQueue is not empty.

这听起来都是就像一个模式 - 基于专用线程的两个直接通道之间的一些队列。

This all sounds like a "pattern" - some queue between two direct channels based on dedicated thread.

也许有一种更简单的方法,已经在Spring Integration中实现,我只是不喜欢因为在这方面缺乏经验,所以看到了吗?

Maybe there is a simplier way, already implemented in Spring Integration, which I just don't see because of absense of expirience in this area?

推荐答案

即使使用轮询器,也可以满足第2点 - 只需设置 fixed-delay 为0和/或增加接收超时(默认为1秒);轮询线程将在队列中阻塞,直到消息到达为止;然后立即再次等待。

Point 2 can be satisfied, even with a poller - just set the fixed-delay to 0 and/or increase the receive timeout (default 1 second); the poller thread will block in the queue until a message arrives; then immediately wait again.

您还可以使用执行程序通道(http线程移交执行程序线程)。

You can also use an executor channel (the http thread hands off to the executor thread).

这篇关于Spring Integration在没有poller的情况下侦听队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 17:53