简体   繁体   中英

Apache Camel : Send a response to the caller after consuming a rest endpoint

I'm new to Apache Camel, I have a camel rest endpoint which receives some JSON data and operates some changes to a database table. The data is sent by some application via a POST request, and I want to notify this application if the whole processing is OK/KO by sending back a response code with some information after the camel Route is done. Is there a way to do that?

EDIT: To give some more information, the whole schema will look like this:

// PART 1
from(rest:restEndpoint)
    .process(someProcessing) // process number 1
    .to(activemq:queue:somequeue)

// PART 2
from(activemq:queue:somequeue)
    .process(someOtherProcessing) // process number 2 

The question here is to know if there is a way to reply back the caller application after finishing each one of the two processes.

When you got a Camel route with a REST endpoint (synchronous) that is called, you already send back a response.

In your routes, when a request comes in, PART 1 is processed. As soon as the message has been sent to ActiveMQ (asynchronous), Camel sends back a response to the caller because the synchronous part of your route is done.

The response body is by default simply the message body at the end of the synchronous processing. The response code is set based on processing result (ie 200 when no error occurs).

So when you want to modify your response body, you can simply add a transformation to the end of PART 1

...
.to(activemq:queue:somequeue)
.transform().constant("Response body")

Now to the harder parts

Send a response after the asynchronous second part

You can mimic synchronous processing with JMS. See this part of Camel JMS documentation for that. If you do this, Camel waits for a reply after sending a message to ActiveMQ.

So with this mechanism, Camel does NOT send a response after the first part, but only after the second part is done.

Send a response after both processing steps

I have to ask back if your caller supports this. A traditional HTTP request has one response. If the caller receives it, she stops listening. For a second response, she has to do a second request.

However, there are multiple options to send a second, asynchronous response over an alternative communication channel (callback URL, JMS message etc). But all of these solutions require that your caller provides such an alternative communication channel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM