Selective Consumer
Filtering on the queue side rather than in the consumer code — to avoid pulling 1000 messages just to drop 999.
Problem
A shared queue carries messages destined to several consumers per a criterion (partner, type, environment). A naive consumer pulls everything and filters in code — wasted bandwidth and CPU.
Forces
- A single queue is often imposed by a shared EDI broker; per-consumer queues are not always possible.
- Pulling then dropping consumes the message lock, blocks other consumers, and inflates the commit log.
- Queue-side filtering applies only to headers / properties, not to encrypted payload.
- An ill-fitted selector can leave a message stuck if no consumer matches.
Solution
Configure the consumer with a broker-side selector. Under JMS, the selector is a SQL-like expression on headers (`AS2_PARTNER = 'WALMART' AND DOC_TYPE = 'ORDERS'`). Under Kafka, rely on key-based partitioning plus a Streams filter. Under RabbitMQ, use a binding key on a topic exchange. The result: each consumer receives only its messages, no pull-and-drop.
EDI implementation
In EDI, Selective Consumer is used to split processing per partner (one consumer per major retailer Walmart / Target / Amazon) without duplicating the queue. Headers are populated by the AS2 Gateway at ingestion: `AS2_FROM`, `AS2_TO`, `DOC_TYPE`. Camel consumers subscribe with `selector='DOC_TYPE = ORDERS'`. Beware of relying on headers not guaranteed (the partner may omit `AS2_FROM` in some signed-only flows).
Anti-patterns
- Too-broad selector that matches everything — the pattern degenerates into disguised pull-and-drop.
- Selector that depends on payload (impossible broker-side) — confused with an application Message Filter.
- Overlapping selectors across consumers — same message pulled twice.
- Non-exhaustive selectors (no consumer matches) — message stuck until TTL or DLQ.
Related patterns
- Message Filter — application-side filter (post-pull).
- Competing Consumers — scaling out a single queue.
- Datatype Channel — alternative: one queue per type.
- Event-Driven Consumer — complementary push consumer.
Sources
- Hohpe G., Woolf B. — EIP, Selective Consumer (p. 515). www.enterpriseintegrationpatterns.com/patterns/messaging/MessageSelector.html
- Apache ActiveMQ — Message Selectors. activemq.apache.org/components/classic/documentation/selectors