1
votes

Below code using Spring integration 2.2.6

I want to send the response back to inbound gateway via reply channel. I am sending reply from service activator. But Spring is not able to set the response instead returning empty message

Spring-Integration xml Config

<int-http:inbound-gateway id="entryHttpInboundGateway"
        request-channel="entryInputChannel" path="/service/getSE/{uuid}" reply-channel="entryInboundReplyChannel"
        request-payload-type="com.test.ServiceEvent" mapped-response-headers="eventUUID, HTTP_RESPONSE_HEADERS"
        supported-methods="GET, POST"  reply-timeout="1000" view-name="/process/plain2">
        <int-http:header name="eventUUID" expression="#pathVariables.uuid"/>
 </int-http:inbound-gateway>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="1" 
        p:defaultContentType="application/json">
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
            </list>
        </property>
</bean>

<int:service-activator id="sv2" input-channel="entryInputChannel" output-channel="outputChannel"
    method="sendReply" requires-reply="true" ref="checkService">
  </int:service-activator>

 <bean id="checkService" class="com.test.CheckService" />

Posting the message to inbound adapter reply channel (com.test.CheckService.java)

// Preparing the message to be sent to reply channel
     InboundReplyResponse inboundReplyResponse = new InboundReplyResponse();
        inboundReplyResponse.setMessage("Success");
        inboundReplyResponse.setEventUUID("12345");

    Map<String, Object> responseHeaderMap = new HashMap<String, Object>();
                    responseHeaderMap.put("eventUUID",eventUUID);
                    responseHeaderMap.put("Content-Type","application/json");

    GenericMessage<InboundReplyResponse> message = new GenericMessage<InboundReplyResponse>(inboundReplyResponse,responseHeaderMap);


    //Reply queue -> entryInboundReplyChannel
        MessageChannel entryInboundReplyChannel =  (MessageChannel) AppFactory.getApplicationContext().getBean("entryInboundReplyChannel");

    entryInboundReplyChannel.send(message);

web.xml

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-integration-context.xml</param-value>
    </context-param>


    <servlet>
        <servlet-name>ssTesting</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

  <servlet-mapping>    
        <servlet-name>ssTesting</servlet-name>    
        <url-pattern>/service/*</url-pattern>  
    </servlet-mapping>

Response returned to client

<Empty JSON content>
2

2 Answers

0
votes

Your code looks bad.

You can send to that entryInboundReplyChannel directly, only if you are within the gateway's invocation thread. Because gateway's sendAndReceive relies on TemporaryReplyChannel in the MessageHeaders. And that last one is involved independently of explicit reply-channel on the inbound-gateway.

Why just don't use entryInboundReplyChannel as output-channel for the <service-activator> and just return the message from sendReply method and just rely on Framewrok abilities to midiate flow correctly?

I meand don't use entryInboundReplyChannel.send(message) manually.

0
votes

Solved

// Instead of creating a new header, Request Message header should be used  

public  Message<ServiceMsg> (Message<?> inMessage){
....
....
GenericMessage<InboundReplyResponse> message = new GenericMessage<InboundReplyResponse>(inboundReplyResponse,inMessage.getHeaders());