Anton Styopin
Anton Styopin

Reputation: 753

IntegrationFlow HttpRequestHandlingMessagingGateway reply directly

I am new to Spring Integration and i am trying to use the HttpRequestExecutingMessageHandler and the HttpRequestHandlingMessagingGateway. The Handler is sending a POST Request and expecting reply. The Gateway is consuming the request. It works fine, BUT the Handler is not getting a reply back. I dont know how to setup my flow, that i can direcly send a reply back and continue my flow.

@Bean
public HttpRequestExecutingMessageHandler httpOutbound() {
  HttpRequestExecutingMessageHandler handler = Http.outboundGateway(restUrl)
        .httpMethod(HttpMethod.POST)
        .messageConverters(new MappingJackson2HttpMessageConverter())
        .mappedRequestHeaders("Content-Type")
        .get();
  handler.setExpectReply(true);
  handler.setOutputChannel(httpResponseChannel());
  return handler;
}

@Bean
public HttpRequestHandlingMessagingGateway httpRequestGateway() {
  HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
  RequestMapping mapping = new RequestMapping();
  mapping.setMethods(HttpMethod.POST);
  mapping.setPathPatterns(httpRequestHandlingPathPattern);
  gateway.setRequestMapping(mapping);
  gateway.setErrorChannel(errorChannel());
  gateway.setReplyChannel(replyChannel());
  gateway.setRequestChannel(requestChannel());
  gateway.setRequestPayloadTypeClass(DocumentConverterInput.class);
  return gateway;
 }

@Bean
public IntegrationFlow documentConverterFlow() {
  return IntegrationFlows
        .from(requestChannel())
        .publishSubscribeChannel(publishSubscribeSpec ->
              publishSubscribeSpec.subscribe(flow -> flow
                          .enrichHeaders(headerEnricherSpec -> headerEnricherSpec.header("http_statusCode", HttpStatus.OK))
                          .channel(replyChannel())))
        .enrichHeaders(headerEnricherSpec ->
             headerEnricherSpec.headerExpression(Constants.DOCUMENT_CONVERTER_INPUT, "payload"))
        .handle(Jms.outboundAdapter(jmsTemplate(connectionFactory)))
        .get();
 }

My HttpRequestExecutingMessageHandler is successfully posting the request. The HttpRequestHandlingMessagingGateway is successfully consuming it. At first i had an error "No reply received within timeout", so that i added a publishSubscriberChannel. I don't know if it is the right way, but i cannot find working examples, which show how to reply correctly.

My above code is working, BUT not sending reply back to HttpRequestExecutingMessageHandler!

My goal is to receive the request message and directly send back 200 OK. After that i want to continue my integration flow, do some stuff and send the result to queue.

Any suggestions?

Upvotes: 0

Views: 1212

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121177

For just 200 OK response you need to consider to configure an HttpRequestHandlingMessagingGateway with a expectReply = false. This way it is going to work as an Inbound Channel Adapter, but having the fact that HTTP is always request-response, it will just do this setStatusCodeIfNeeded(response, httpEntity); and your HttpRequestExecutingMessageHandler on the client side will get an empty, but OK resposnse.

Not sure though why your channel(replyChannel()) doesn't work as expected. It might be the fact that you return a request palyoad into a reply message which eventually becomes as a HTTP response, but somehow it fails there, may be during conversion...

UPDATE

Here is a simple Spring Boot application demonstrating a reply-and-process scenario: https://github.com/artembilan/sandbox/tree/master/http-reply-and-process

Upvotes: 1

Related Questions