# Content-based Router

**In short:** A Content-Based Router reads each message and sends it to a channel chosen from data inside the message. It lets one logical function span several systems. Its fixed rules need careful upkeep.

## Explanation

A Content-Based Router looks at each message that arrives. It reads data in the message and picks the channel that fits. A channel is a path that carries messages to a receiver (see the article Message Channel). The router then puts the message on that channel.

The pattern solves a common problem. Sometimes one logical function is spread over several systems. The router sends each message to the right system, based on what the message contains.

**How it looks in tools**

- Apache Camel implements the pattern with its Choice feature. Choice routes on the contents of the message exchange.
- In Camel, the first `when` clause that matches decides the branch.
- An optional `otherwise` clause handles messages that match no `when`. Without it, no branch is selected.
- Spring Integration describes routers as components that read from one channel. They forward each message to other channels based on conditions.
- Spring Integration offers payload-type, header-value, recipient-list, XPath and generic routers. By default it throws an exception if no channel is found.

**Related pattern**

The rules in a Content-Based Router are fixed. The router depends on all possible destinations. The Dynamic Router removes this dependency. It adds a control channel. Recipients announce themselves on that channel, so the router configures itself. See the article Dynamic Router.

## Example

An online shop receives orders on one channel. Each order carries a product category. The router reads the category. Book orders go to the channel of the book supplier. Electronics orders go to the channel of the electronics supplier. A fallback branch, or an error, must handle a category that no rule covers.

## Trade-offs

- The routing logic is a maintenance concern. The pattern itself warns that it must be kept easy to maintain.
- The router depends on all possible destinations. Each new destination means a change to the router.
- Order matters in Camel: the first matching `when` wins, so rule sequence affects results.
- Messages that match no rule need a deliberate answer. Camel's `otherwise` clause is one option. Spring Integration throws an exception by default.
- The Dynamic Router trades fixed rules for self-configuration through a control channel.

A router reads the category in each order and picks the matching channel.

```mermaid
flowchart LR
  In[Incoming orders] --> R{Content-Based Router}
  R -->|category = books| A[Book supplier channel]
  R -->|category = electronics| B[Electronics supplier channel]
  R -->|no rule matches| C[Fallback or error handling]
```

## Checks

- The routing rules are documented and kept easy to maintain.
- The document states what happens to a message that matches no rule.
- Where rules are checked in order, the order is documented, because the first match decides the branch.
- The document lists the destinations the router depends on, or explains how they are discovered, for example with a Dynamic Router.

## Related

- [Dynamic Router](https://arc.fuet.ch/concepts/integration/dynamic-router.md)
- [Message Routing](https://arc.fuet.ch/concepts/integration/message-routing.md)
- [Recipient List](https://arc.fuet.ch/concepts/integration/recipient-list.md)
- [Message Filter](https://arc.fuet.ch/concepts/integration/message-filter.md)
- [Message Channel](https://arc.fuet.ch/concepts/integration/message-channel.md)
- [Enterprise Integration Patterns](https://arc.fuet.ch/concepts/integration/messaging-pattern-language.md)
- [Publish/Subscribe Channel](https://arc.fuet.ch/concepts/integration/publish-subscribe-channel.md)
- [Loose Coupling](https://arc.fuet.ch/concepts/principles/loose-coupling.md)

## Sources

- [Content-Based Router - Enterprise Integration Patterns](https://www.enterpriseintegrationpatterns.com/patterns/messaging/ContentBasedRouter.html) — enterpriseintegrationpatterns.com, accessed 2026-11-09
- [Choice :: Apache Camel](https://camel.apache.org/components/4.22.x/eips/choice-eip.html) — camel.apache.org, accessed 2026-11-09
- [Overview :: Spring Integration](https://docs.spring.io/spring-integration/reference/router/overview.html) — docs.spring.io, accessed 2026-11-09
- [Dynamic Router - Enterprise Integration Patterns](https://www.enterpriseintegrationpatterns.com/patterns/messaging/DynamicRouter.html) — enterpriseintegrationpatterns.com, accessed 2026-11-09
