Publish/Subscribe Pattern with Apache Camel
Publish/Subscribe is a simple messaging pattern where a publisher sends messages to a channel without the knowledge of who is going to receive them. Then it is the responsibility of the channel to deliver a copy of the messages to each subscriber. This messaging model enables creation of loosely coupled and scalable systems.
It is a very common messaging pattern and there are so many ways to create a kind of pub-sub in Apache Camel. But bear in mind that they are all different and have different characteristics. From the simplest to more complex, here is a list:
- Multicast - works only with a static list of subscribers, can deliver the message to subscriber in parallel, stops or continues on exception if one of the subscribers fails.
- Recipient List - it is similar to multicast, but allows the subscribers to be defined at run time, for example in the message header.
- SEDA - this component provides asynchronous SEDA behaviour using BlockingQueue. When multipleConsumers option is set, it can be used for asynchronous pub-sub messaging. It also has possibilities to block when full, set queue size or time out publishing if the message is not consumed on time.
- VM - same as SEDA, but works cross multiple CamelContexts, as long as they are in the same JMV. It is a nice mechanism for sending messages between webapps in a web-container or bundles in OSGI container.
- Spring-redis - Redis has pubsub feature which allows publishing messages to multiple receivers. It is possible to subscribe to a channel by name or using pattern-matching. When pattern-matching is used, the subscriber will receive messages from all the channels matching the pattern. Keep in mind that in this case it is possible to receive a message more than once, if the multiple patterns matches the same channel where the message was sent.
- JMS (ActiveMQ) - that's probably the best know way for doing pub-sub including durable subscriptions. For a complete list of features check ActiveMQ website.
- Amazon SNS/SQS - if you need a really scalable and reliable solution, SNS is the way to go. Subscribing a SQS queue to the topic, turns it into a durable subscriber and allows polling the messages later. The important point to remember in this case is that it is not very fast and most importantly, Amazon doesn't guarantee FIFO order for your messages.
There are also less popular Camel components which offer publish-subscribe messaging model:
- websocket - it uses Eclipse Jetty Server and can sends message to all clients which are currently connected.
- hazelcast - SEDA implements a work-queue in order to support asynchronous SEDA architectures.
- guava-eventbus - integration bridge between Camel and Google Guava EventBus infrastructure.
- spring-event - provides access to the Spring ApplicationEvent objects.
- eventadmin - on OSGi environment to receive OSGI events.
- xmpp - implements XMPP (Jabber) transport. Posting a message in chat room is also pub-sub;)
- mqtt - for communicating with MQTT compliant message brokers.
- amqp - supports the AMQP protocol using the Client API of the Qpid project.
- javaspace - a transport for working with any JavaSpace compliant implementation.
Can you name any other way for doing publish-subscribe?
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)