Skip to content

Content-based routing with JQ

Talisman Karavan

Use case

This use-case outlines the process of routing messages based on the totalAmount field within a JSON data structure. The objective is to automate the decision-making process for message routing, ensuring that messages are directed to the appropriate processing based on the total amount of an order. This mechanism is vital for streamlining operations, such as order processing, customer service, and financial auditing.

Below is an example of the data format:

example.json
{
  "orderId": "12345",
  "customer": {
    "customerId": "67890",
    "name": "Jane Doe",
    "email": "jane.doe@example.com",
    "address": "123 Main St, Anytown, Anystate, 12345"
  },
  "totalAmount": 40.00,
  "orderDate": "2024-03-25",
  "status": "Pending"
}

The core of this use-case is the routing logic based on the totalAmount field of the order data. The routing logic is defined as follows:

If the total amount is less than $100, the message will be routed to a Small amount processing If the total amount is $100 or more, the message is directed to a Large amount processing'

Design

Image title

  1. Messages are received from a direct component
  2. The core of the routing logic uses the Choice element to route the message path based on a condition.
  3. when Clause: If the totalAmount is less than 100, identified by the jq expression .totalAmount < 100, the message is considered to have a small amount. A log step is executed, logging the message "SMALL," indicating the message pertains to a standard processing queue.
  4. otherwise Clause: For amounts of $100 or more, where the totalAmount does not meet the when clause's condition, the otherwise block is executed. It includes a log step that logs "LARGE," directing the message towards the premium processing queue.

YAML

routing.camel.yaml
- route:
    id: simple-routing
    description: Simple Routing
    nodePrefixId: route-217
    from:
      id: from-574e
      description: Received JSON
      uri: direct
      parameters:
        name: simple-routing
      steps:
        - choice:
            id: choice-e68e
            when:
              - id: when-42ff
                expression:
                  jq:
                    id: jq-3418
                    expression: .totalAmount < 100
                steps:
                  - log:
                      id: log-7e51
                      description: Small amount
                      message: SMALL
            otherwise:
              id: otherwise-a3b0
              steps:
                - log:
                    id: log-7528
                    description: Large amount
                    message: LARGE

This additional YAML configuration provides a demonstration route that generates a JSON message and sends it to the simple-routing route you previously defined. Let's break down the components of this configuration:

demo.camel.yaml
- route:
    id: route-a1c1
    nodePrefixId: route-13f
    from:
      id: from-fe49
      uri: timer
      parameters:
        timerName: test
      steps:
        - setBody:
            id: setBody-a132
            expression:
              constant:
                id: constant-de93
                expression: |
                  {
                    "orderId": "12345",
                    "customer": {
                      "customerId": "67890",
                      "name": "Jane Doe",
                      "email": "jane.doe@example.com",
                      "address": "123 Main St, Anytown, Anystate, 12345"
                    },
                    "totalAmount": 400.00,
                    "orderDate": "2024-03-25",
                    "status": "Pending"
                  }
        - to:
            id: to-1203
            uri: direct
            parameters:
              name: simple-routing