search

Home  >  Q&A  >  body text

java - spring-amqp中消费者同步回复消息有没有更简单的方法?

amqp中使用
provider使用sendAndReceive可以等待消费者取得返回值
consumer使用receiveAndReply,并添加监听器,可以取得provider的message处理并回复

当消息是异步时,直接注册监听器实现onMessage()方法即可。
但是onMessage()方法的返回值是void

那么如果要实现provider同步的功能
consumer除了

while(true){
    amqpTemplate.receiveAndReply(queueName,new Callback(Message message){
         dosomething;
         return "result";
    })
}

这种方式之外 还有别的使用方法吗?

ringa_leeringa_lee2766 days ago524

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-04-18 10:28:20

    Why do we have to wait for the return value?
    A -> B
    A <- B
    A sends a message to B, and B receives and processes the message. B sends a message (receipt) to A when A receives the message processing result. AB is Producer也是Consumer.

    If MQ still needs to do synchronization and other operations, wouldn't it be easier to use MQ directly and use the interface?

    reply
    0
  • 阿神

    阿神2017-04-18 10:28:20

    Then you want the provider to block until it receives the return value from the consumer?
    This is not recommended. If you must receive synchronously, you can make both provider and consumer single-threaded.

    reply
    0
  • Cancelreply