Asynchronous variation of the service activator EIP?

75 views Asked by At

We have the following Camel route in our application:

    from(webServiceUri).routeId("webServiceRoute")
        .unmarshal(jaxb)
        .process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                final Message in = exchange.getIn();
                final DataRequest body = in.getBody(DataRequest.class);
                final DataRequest.Items items = body.getItems();
                itemValidator.validate(items.getItem());
                getContext().createProducerTemplate().sendBody(importUri, body);
                DataResponse response = new DataResponse();
                response.setReturnCode(ReturnCode.SUCCESS);
                in.setBody(response);
            }
        })
        .marshal(jaxb);

We want the "webServiceRoute" to return the response user as soon as the processor has validated the data and forwarded the message to the "importUri". But right now it seems like the response is not returned to the caller until the "importUri" exchange is completed. So my question is what is the "correct" way to asynchronously forward the received request to another queue? There will not be any reply from the "importUri" exchange (i.e. it should be InOnly).

1

There are 1 answers

4
G Quintana On BEST ANSWER

You can replace .sendBody(importUri, body) by .asyncSendBody(importUri, body).

Nevertheless I find your route looks strange to me, why do you use a processor to forward your message. I would write something like:

DataResponse successResponse = new DataResponse();
response.setReturnCode(ReturnCode.SUCCESS);

from(webServiceUri).routeId("webServiceRoute")
    .unmarshal(jaxb)
    .bean(WebServiceRouteHelper.class,"validate")
    .to(importUri)
    .setBody(constant(sucessResponse))
    .marshal(jaxb);

class WebServiceRouteHelper {
    public DataRequest validate(DataRequest dataRequest) throws Exception {
        final DataRequest.Items items = body.getItems();
        itemValidator.validate(items.getItem());
        return dataRequest;
    }
}