Spring Integration - Parallel ordered processing

2019-09-01 02:49发布

问题:

In the application i work on, there is a continuous flow of messages coming from a TCP socket. Messages have different types. Different types of messages should be processed in parallel. But each specific type of message must be processed in the order they come in. I used ExecutorChannel from spring integration and it solves parallel processing need. I created a channel for each specific type of message. But i cannot guarantee ordered processing of messages for specific types.

Is there a way to do ordered processing with publish/subscribe channels while also using parallel processing?

回答1:

Consider to use the same ExecutorChannel but as an input for each type. The trick that each of them should be configured with executors with a single thread. So, you have as much single-threaded executor channels as you have message types.

Another trick is like QueueChannel for each type and polling endpoints with the fixed-delay as subscribers to those queues.

One more option available since the current Spring Integration 5.0 is FluxMessageChannel. The ordering is guaranteed by the internal Reactor's Flux and the parallelism will be achieved by the subscribers - the processing messages in Flux is happened in the subscriber thread.