Skip to content

Selective Consumer

Sometimes a consumer wants only some of the messages on a channel. It needs a way to choose which ones it receives. (A channel is the path that carries messages between systems. See Message Channel.)

The Selective Consumer pattern solves this. The consumer states its criteria, and the channel filters what it delivers. Only matching messages arrive. The channel does the filtering, not the consumer.

Concrete forms

  • JMS message selectors. JMS is the Java messaging API. A selector lets a consumer say which messages it is interested in. Its syntax is based on a subset of SQL92 conditional expressions. The JMS provider, not the application, does the filtering.
  • RabbitMQ and AMQP bindings. Bindings are rules that tell an exchange how to route messages to queues. The broker does the matching. A headers exchange can route on several header attributes. The x-match setting controls whether any or all of them must match.

Limits of matching

JMS selectors look only at message headers and properties. They cannot look at the message body. Consumers are therefore tied to how those headers and properties are structured.

Several consumers on one channel

On a Point-to-Point Channel, several Selective Consumers behave as selective Competing Consumers. They compete for messages, but each one only competes for messages that match.

Cover every value

If no consumer matches a message’s selection value, nobody consumes that message. It clutters the channel until it expires. The criteria of all consumers together must therefore cover every valid value.

An order channel carries messages with a header called region. The EU billing service asks for region = 'EU'. The US billing service asks for region = 'US'. Each service receives only its own orders. If a message arrives with region = 'APAC', no service selects it. It stays on the channel until it expires. The team must add a consumer for that value or change the criteria.

Benefits

  • The consumer does not filter messages itself. The channel or broker does that work.
  • One channel can serve consumers with different interests.

Costs and risks

  • Unmatched messages clutter the channel until they expire. Criteria must cover all valid values.
  • Selectors depend on header and property structure. This couples consumers to that structure.
  • Selectors cannot see the message body, so body content cannot drive the choice.

A channel delivers each message only to the consumer whose criteria match.

Diagram source
flowchart LR
P[Producer] --> C[(Channel)]
C -- region = EU --> A[EU billing consumer]
C -- region = US --> B[US billing consumer]
C -. no match .-> X[Message waits until it expires]
  • The selection criteria of all consumers on a channel together cover every valid selection value.
  • The document states which message headers or properties consumers select on, because selectors cannot use the message body.
  • The document states that the broker or messaging provider, not the consumer, performs the filtering.