Publish-Subscribe Channel

Using a Publish-Subscribe Channel, the Publisher can deliver a message to multiple Subscribers at the same time. This channel can accomplish this feat because it has a single input channel that has the ability to split into multiple output channels with each one associated with a subscriber.

Whenever an event is published into the channel, it will deliver a copy of the message to each of the output channels. Additionally, each output channel will have only one subscriber and that subscriber is only allowed to consume the message once. In this scenario, each of the subscribers gets the message once, and the consumed copies will automatically disappear from their channels.

 

 

Components that support publish-subscribe channel

The following Apache Camel components support the publish-subscribe channel pattern:

  • JMS

  • ActiveMQ

  • SEDA

  • XMPP

 

JMS Publish-Subscribe

In JMS, a publish-subscribe channel is represented by a topic. For example, you can specify the endpoint URI for a JMS topic called StockQuotes as follows:

jms:topic:StockQuotes

ActiveMQ

In ActiveMQ, a publish-subscribe channel is represented by a topic. For example, you can specify the endpoint URI for an ActiveMQ topic called StockQuotes, as follows:

activemq:topic:StockQuotes

 

Static Subscription Lists

We can also implement publish-subscribe logic within the Apache Camel application, and a simple approach is to define a static subscription list. This is where the target endpoints are all explicitly listed at the end of the route. The following example shows how to simulate a publish-subscribe channel with a single publisher, seda:a, and three subscribers, seda:b, seda:c, and seda:d:

<camelContext id="buildStaticRecipientList" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="seda:a"/> <to uri="seda:b"/> <to uri="seda:c"/> <to uri="seda:d"/> </route> </camelContext>