Spring Integration listen on queue without poller

橙三吉。 提交于 2019-12-22 00:24:10

问题


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. 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.

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.

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

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.

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?


回答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.

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



来源:https://stackoverflow.com/questions/41410979/spring-integration-listen-on-queue-without-poller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!