Search code examples
javajmsmessage-queue

The difference between a Message Queue, EventBus and a Pub/Sub?


I am confused between the concept of Message Queue (e.g., ActiveMQ, RabbitMQ, ZeroMQ) and EventBus (e.g., Guava Event Bus, Akka EventBus)

I think MQ and eventBus both use the pub/sub pattern. MQ seems more powerful and heavy, compared to Guava.

But what's the real difference? Is EventBus the same as MQ?


Solution

  • A message is usually used for inter-process communication and for sending messages between machines. You can encapsulate an event in a message (for example as XML or JSON) and transport this event using a message. TIBCO RV, JMS, IBM or Hornet MQ, ...

    An event is usually used for intra-application communication. For example to communicate between threads or to react on user input in a GUI application (think Swing events, Guava, etc).

    A queue is a 1-to-1 destination of messages. The message is received by only one of the consuming receivers (please note: consistently using subscribers for 'topic client's and receivers for queue client's avoids confusion). Messages sent to a queue are stored on disk or memory until someone picks it up or it expires.

    A bus is a 1-to-many model of distribution. The destination in this model is usually called topic or subject. The same published message is received by all consuming subscribers. You can also call this the 'broadcast' model. You can think of a topic as the equivalent of a Subject in an Observer design pattern for distributed computing. Some message bus providers efficiently choose to implement this as UDP instead of TCP. For topic's the message delivery is 'fire-and-forget' - if no one listens, the message just disappears. If that's not what you want, you can use 'durable subscriptions'.

    If you take this all together you have these:

    1. Message Queue: queue-based messaging middlewares are IBM MQ, JMS/ActiveMQ Queues, Hornet MQ

    2. Event queue: queue-based programming framework. You could implement this with any class that implements the Java Queue interface. e.g. BlockingQueue

    3. Message Bus: a publish/subscribe messaging middleware, e.g. JMS/ActiveMQ Topics, TIBCO RV. Messages are sent to another process over TCP or UDP. For further details see JMS Topic vs Queues

    4. Event Bus: a publish/subscribe based programming framework. Guava EventBus, Observer design pattern