问题
RabbitMQ guarantees message order if the same Publish Channel->Exchange->Queue->Receive Channel is used. It appears Spring AMQP allows me to guarantee all those pieces except for the publish channel. This is causing messages to get out of order. Below is the problem case:
1.) I have a single thread publishing messages in order on a single exchange 2.) I have a collection of other threads publishing messages on different exchanges and sometimes creating new consumers for different exchanges
Using the cached connection factory it has a cache of channels. Each time a publish is made it goes to the cache to get a channel. Because other threads are publishing and consuming there are no guarantees that the same channel will be used for successive publishes by my main thread. This is true even if I have a cache of one. Has anybody gotten around this issue?
Ideally I would like a dedicated channel for a publisher-exchange combination or at the very least a dedicated channel for publishing. I don't see a way of providing either. It seems a significant flaw in the Spring AMQP if there is no way to guarantee publish order when RabbitMQ has a mechanism for guaranteeing order.
回答1:
You would have to use a dedicated connection (hence: connection factory) for each publisher.
Or, you could use RabbitTemplate.execute()
and use the channel provided in the callback to publish al messages.
来源:https://stackoverflow.com/questions/26242254/spring-amqp-ensuring-message-order-in-multi-threaded-environment