TuGordoBello
TuGordoBello

Reputation: 4509

Spring Integration - Messaging Gateway with a returning value

I'm new with Spring Integration. I need to implement a Messaging Gateway with a returning value. In order to continue some processing asynchronously after executing some synchronous steps. So I made 2 activators

@Slf4j
@MessageEndpoint
public class Activator1 {

@ServiceActivator(inputChannel = "asyncChannel")
public void async(){
    log.info("Just async message");
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        log.error("I don't want to sleep now");
    }
}
}

and

@Slf4j
@MessageEndpoint
public class Activator2 {

@ServiceActivator(inputChannel = "syncChannel")
public ResponseEntity sync(){
    try {
        Thread.sleep(500);
        return ResponseEntity.ok("Return Http Message");
    } catch (InterruptedException e) {
        log.error("I don't want to sleep");
    }
    return ResponseEntity.badRequest().build();
}
}

The pipeline

@Configuration
public class Pipeline {

@Bean
MessageChannel asyncChannel() {
    return new DirectChannel();
}

@Bean
public MessageChannel syncChannel() {
    return MessageChannels.direct().get();
}

}

the gateway

@MessagingGateway
public interface ReturningGateway {

@Gateway(requestChannel = "asyncChannel", replyChannel = "syncChannel")
public ResponseEntity getSyncHttpResponse();

}

And Controller

@Slf4j
@RestController
@RequestMapping("/sync")
public class ResponseController {

@Autowired
ReturningGateway returningGateway;

@PostMapping("/http-response")
public ResponseEntity post() {
    
    return returningGateway.getSyncHttpResponse();
    
}
}

So I'm not sure if thats the correct way to do what I want to do

Can you give me hand?

Upvotes: 1

Views: 1732

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121272

Let me try to explain some things first of all!

@Gateway(requestChannel = "asyncChannel", replyChannel = "syncChannel")

The requestChannel is where a gateway sends a message. But since you don't have any arguments in the gateway method and there is no a payloadExpression, the behavior is to "receive" from that channel. See docs for more info: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#gateway-calling-no-argument-methods.

The replyChannel is where to wait for a reply, not send. In most cases a gateway relies on the replyChannel header for correlation. The request-reply pattern in messaging. We need an explicit replyChannel if it is a PublishSubscribeChannel to track a reply somehow or when we deal with the flow which we can't modify to rely on the replyChannel header. See the same gateway chapter in the docs.

Your use-case is not clear for me: you say async continuation, but at the same time the return from your gateway contract looks like a result of that sync() method. From here, please make yourself familiar with the gateway contract and then come back to us with refreshed vision for your solution.

Upvotes: 2

Related Questions