This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

AnnouncementAnnouncement Module

Collapse

No announcement yet.

Spring Integration: publish-subscribe-channel and order of message deliveryPage Title Module

Spring Integration: publish-subscribe-channel and order of message delivery

Jan 28th, 2009, 08:57 PM

Hi,

I am using a publish-subscribe-channel to send notifications to multiple subscribers. In one scenario, two messages were sent consecutively.
However, it appeared that the second message was delivered first and then the first message was delivered.

Is this the expected behavior? If so, is it possible to configure the channel to deliver messages in a FIFO order?

I just created a simple demo based on your code, and I am seeing the messages in the right order every time. Do you think something that you have left out could affect order?... or are you seeing Messages from multiple invocations?... or perhaps it's simply reordered in the console output but not the actual execution?

If you could provide a very simple full code example that reproduces this, it would be extremely helpful in determining if there is in fact a bug.

Thanks,
Mark

Comment

Yes, it is a simplified version of the real code, so the 2 actual calls do not happen that close together. There are calls to other beans and back in between the "send" calls, including steps that involve Hibernate data persistence. However, the two "send" calls do take place inside the same bean (same thread) and they do not go through other message endpoints.

I am fairly certain the output is correct. Actually, I've also stepped through the code via the eclipse debugger and that seems to be the order.

Unfortunately, the code base is quite large and has "privacy" restriction, so I am not sure I am allowed to send it out. I'll find out.

Thank you again for your reply. Now that I know that this is not the expected behavior, I'll concentrate on the code logic to see maybe it's something else that is causing this problem.

Comment

Comment

Just want to let you know that we've found the problem and it's in the way our code behaves. The Message Channel delivers the messages in the correct order.

Using the above example,
- the "CREATE" message was send
- SubscriberA's service-activator method was the first to get invoked.
(SubscriberB's and SubscriberC's service-activator methods are still waiting to be invoked)
- SubscriberA's service-activator method performs some task that results in the publisher sending the "UPDATE".
- The send() call with "UPDATE" results in the service-activator method for all 3 subscribers to get invoked with the UPDATE message.
- All 3 subscribers finished processing the UPDATE message.
- SubscriberB's service-activtor method is invoked with the CREATE message
- SubscriberC's service-activtor method is invoked with the CREATE message

So the result is that while SubscriberA received the messages in the right order, SubscriberB and SubscriberC received the messages in the reversed order.

Note that I did not use thread-pool for the service activators since our beans are currently not thread-safe.

To resolve this for now, I just reversed the order of my service-activators in the applicationContext, i.e. putting subscriberA's service-activator last so that it is the last one to receive the message.

This seems to get me past the problem; however, I am not sure this is the ultimate solution or if I can always depend on this order being the same all the time (e.g. future implementation of Spring Integration may use a different ordering scheme)

This may sound a little strange, but is there any way I can guarantee the order in which the service-activator's get invoked?