apache-camelspring-camelcamel-sql

How to store a global value with query results and reuse it to enrich every message


I'm trying to implement a simple streaming pipeline:

  1. Get list of users from a remote REST endpoint, spliting the list into individual messages

  2. For each user I have to enrich it with information from a SQL parametric table (departments), e.g.:

Initial message (User 1)

id:1
departmentId: 1
parentDepartmentId: 23

Departments

Department 1
id: 1
name: Dep1
    
Department 2
id: 23
name: Dep23

Enriched User 1

id:1
departmentId: 1
departmentName: Dep1
parentDepartmentId: 23
parentDepartmentName: Dep23

Route context:

    <routeContext id="route1" xmlns="http://camel.apache.org/schema/spring">
        <route id="route1">
            <from id="_from1" uri="timer:mytimer"/>
            <setHeader headerName="CamelHttpMethod">
                <constant>POST</constant>
            </setHeader>
            <setHeader headerName="Content-Type">
                <constant>application/json</constant>
            </setHeader>
            <setBody>
                <simple>{ "new": "true" }</simple>
            </setBody>
            <to id="_apiCall1" uri="https4://myapi/v1/users/search"/>
            <split streaming="true">
                <simple>${body}</simple>
                <to uri="direct:processNewUser"/>
            </split>
        </route>
        <route id="processNewUser">
            <from uri="direct:processNewUser"/>
            <enrich strategyRef="myAggregationStrategy">
                <simple>sql:select * from departments"</simple>
            </enrich>
        </route>
    </routeContext>

I need to get the whole departments table to be able to enrich the user information but I want to avoid doing so for every message.

Is there a way to store the content of the sql query and reuse it during the enrichment phase?


Solution

  • Can't you get the departments first? It seems you could do that. Then, you'd set the result to an exchange property for instance and use it to enrich each user.

    There are actually many ways to achieve that.