This is quite a general question.

I want to design a set of classes and interfaces so I can swap between messaging systems without changing code, probably between RabbitMq and Kafka.

My main concern is how to abstract the events they handle, as both systems require event handlers but, of course, event handlers on both systems are different.


I would suggest separating business logic module from the communication module.

The business logic module would cover the data processing and would have some interface to the communication logic module.

The communication module would be in charge solely on receiving and sending the messages, without any data processing, apart from basic data validation. The communication module would implement Strategy Pattern, where the business logic module would invoke an exposed interface of the communication module, which, in turn would have two implementations: one using RabbitMq and the other using Kafka.

Assuming your application has the same business logic, regardless of whether you use RabbitMq or Kafka (otherwise you would not be asking this question), you would abstract that business logic, not the event handlers per se.

Let's say that Rabbit Mq has a ShutDown event handler, and Kafka has something called Stop event handler (probably doesn't, but for the sake of the argument). This functionality would be abstracted through the ICommunication interface (for example), using method StopWorking. Then, RabbitMq would implement it using ShutDown, and Kafka would implement it using Stop. Business logic module would see ICommunication.StopWorking. Whatever happens beyond that is of no concern to it and is separated from that part.

  • How would I abstract the event handlers? For instance, RabbitMq has a ShutDown event handler that is specific to RabbitMq. I am not sure about Kafka, but I imagine it will have its own. Should I have a dictionary of delegates that each one has as a field in the communication module? – user197619 May 10 '17 at 7:51
  • @user197619 I expanded my answer with an example based on the comment. – Vladimir Stokic May 10 '17 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.