Milla
Milla

Reputation: 503

Apache camel blueprint web service response in another route

I built a web service in apache camel running as a bundle on karaf which takes the requests and saves the information in a database.

After this another bundle takes this inserted record, modifies the data and saves it back to the db.

Now I need the response of the original request to contain the modified data so besides the route for the web service that looks like this

<route id="cxf">
    <from uri="cxf:bean:getHopEndpoint" />
    <recipientList>
        <simple>direct:${header.operationName}</simple>
    </recipientList>
</route>
<route id="getHop">
    <from uri="direct:getHop" />
    <process ref="getHopToDbProcessor" />
    <to
        uri="sql:INSERT INTO myTable (field1, field2) VALUES (:#field1, :#field2)"/>
</route>

I would need another one like this

<route id="cxfResponse">
    <from uri="sql:SELECT * FROM myTable"/>
    <!-- to web service response in any way -->
</route>

Is there any way to do this?

Upvotes: 1

Views: 530

Answers (2)

Milla
Milla

Reputation: 503

I found another solution: I'm using the Direct VM Component. My Routes look like this now:

Bundle 1 recieve web service request and save the data in the database:

<route id="cxf">
    <from uri="cxf:bean:getHopEndpoint" />
    <recipientList>
            <simple>direct:${header.operationName}</simple>
    </recipientList>
</route>
<route id="getHop">
    <from uri="direct:getHop" />
    <process ref="getHopToDbProcessor" />
    <to uri="sql:INSERT INTO myTable (field1, field2) VALUES (:#field1, :#field2)" />
    <to uri="direct-vm:processHop" />
</route>

In bundle 2 which processes the data I just inserted:

<route>
    <from uri="direct-vm:processHop"/>
    <to uri="sql:SELECT * FROM myTable WHERE processed = false" />
    <process ref="getHopComputopUrlProcessor" />
    <to uri="sql:UPDATE webshop_gethop_requests SET new_data = :#newData, processed = true WHERE some_id = :#someId" />
    <to uri="direct-vm:response"/>
</route>

And then back to bundle 1 again to send the response with the processed data:

<route>
    <from uri="direct-vm:response" />
    <to uri="sql:SELECT some_id,new_data FROM myTable WHERE some_id = :#someId AND processed = true" />
    <process ref="getHopResponseProcessor" />
</route>

This works just fine for me :)

Upvotes: 1

Christian Schneider
Christian Schneider

Reputation: 19626

You would have to set the body to the desired response as the last step in the first route. There is no way to set the response outside this route.

So I think there are two way to solve your problem.

  1. Do all you need to do inside the first route in a synchronous way
  2. Change your service to be completely asynchronous

For Variant 2 you can either use messaging like jms or you give the first call a webservice uri to call back.

If you have high load on the service a completely async approach might make your system work better.

Upvotes: 1

Related Questions