Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用内置Spring RestController生成和使用RabbitMQ消息并将其发送回用户_Spring_Kotlin_Rabbitmq_Spring Cloud Stream - Fatal编程技术网

如何使用内置Spring RestController生成和使用RabbitMQ消息并将其发送回用户

如何使用内置Spring RestController生成和使用RabbitMQ消息并将其发送回用户,spring,kotlin,rabbitmq,spring-cloud-stream,Spring,Kotlin,Rabbitmq,Spring Cloud Stream,大家好。我正在开发一个Spring启动应用程序。这是我的问题。我有一个SpringRestController,它带有一个post映射,可以接收一些数据。然后,我需要通过RabbitMQ将该数据发送到另一个应用程序,作为回报,该应用程序将对该数据执行一些计算,然后将其发送回我,然后我希望将其返回给用户 我知道RabbitMQ用于异步通信。但我需要我的控制器一次性返回从RabbitMQ返回的结果。现在我正在使用 @EnableBinding(Sink::class) class Optimized

大家好。我正在开发一个Spring启动应用程序。这是我的问题。我有一个SpringRestController,它带有一个post映射,可以接收一些数据。然后,我需要通过RabbitMQ将该数据发送到另一个应用程序,作为回报,该应用程序将对该数据执行一些计算,然后将其发送回我,然后我希望将其返回给用户

我知道RabbitMQ用于异步通信。但我需要我的控制器一次性返回从RabbitMQ返回的结果。现在我正在使用

@EnableBinding(Sink::class)
class OptimizedScheduleMessageListener {
    @StreamListener(Sink.INPUT)
    fun handler(incomingMessage: MyDTO) {
        println(incomingMessage)
    }
}
从RabbitMQ检索结果。现在我只需要我的控制器返回它

@PostMapping( produces = ["application/json"])
   fun retrieveOptimizedSchedule: Result<MyDTO> {

       myUncalculatedDTO: MyDTO()
       source.output().send(MessageBuilder.withPayload(myUncalculadeDTO).build())

       return ???

   }
@PostMapping(生成=[“应用程序/json”])
fun retrieveOptimizedSchedule:结果{
MyUncalculatedTo:MyDTO()
source.output().send(MessageBuilder.withPayload(myuncalculatedto.build())
返回???
}
非常感谢您在这方面给予的任何帮助


提前谢谢

出于两个原因,不建议您尝试做什么

   1. The failure of the 'Another application' which consumes the Rabbit
      MQ messages will result in Requests being blocked on the controller end.
   2. There is a limit on how many requests you can have simultaneously from the server to clients.

对于这个特定部分,您可以使用REST以外的任何其他通信协议。可能是Websocket将是一个理想的解决方案。如果没有,则需要有两个REST端点。一个用于提交并获取请求id,另一个用于使用请求id定期轮询并获得已完成的响应。

Spring Cloud Stream不是为请求/回复处理而设计的

rabbitmplate
具有
sendAndReceive
convertSendAndReceive
方法来实现RPC模型

在服务器端,可以使用
@RabbitListener
方法进行请求/回复