在SpringAMQP中预先设置回复?

时间:2014-09-11 08:19:31

标签: java rabbitmq spring-boot spring-amqp

我正在使用SpringBoot启动连接到RabbitMQ队列的SpringAMQP应用程序。我希望能够从生产者发送消息,指定回复队列,以便消费者只需要发送而无需调查目的地(因此不必在消息本身中传递回复数据)。 / p>

这是我的配置(在生产者和消费者之间共享)

private static final String QUEUE_NAME = "testQueue";
private static final String ROUTING_KEY = QUEUE_NAME;
public static final String REPLY_QUEUE = "replyQueue";
private static final String USERNAME = "guest";
private static final String PASSWORD = "guest";
private static final String IP = "localhost";
private static final String VHOST = "/";
private static final int PORT = 5672;

@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate(connectionFactory());
    amqpAdmin().declareQueue(new Queue(QUEUE_NAME));
    amqpAdmin().declareQueue(new Queue(REPLY_QUEUE));
    return template;
}

@Bean
public AmqpAdmin amqpAdmin() {
    return new RabbitAdmin(connectionFactory());
}

@Bean
public ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(IP);
    connectionFactory.setUsername(USERNAME);
    connectionFactory.setPassword(PASSWORD);
    connectionFactory.setVirtualHost(VHOST);
    connectionFactory.setPort(PORT);
    return connectionFactory;
}

我发送的信息如下:

public Object sendAndReply(String queue, String content){
        return template.convertSendAndReceive(queue, new Data(content), new MessagePostProcessor() {

            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setReplyTo(ReplyTester.REPLY_QUEUE);
                return message;
            }
        });
    }

等待回复如下:

public void replyToQueue(String queue){
    template.receiveAndReply(queue, new ReceiveAndReplyCallback<Data, Data>() {
        @Override
        public Data handle(Data payload) {
            System.out.println("Received: "+payload.toString());
            return new Data("This is a reply for: "+payload.toString());
        }
    });
}

然而,当发送时,我得到以下异常:

Exception in thread "main" org.springframework.amqp.UncategorizedAmqpException: java.lang.IllegalArgumentException: Send-and-receive methods can only be used if the Message does not already have a replyTo property.
    at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:66)
    at org.springframework.amqp.rabbit.connection.RabbitAccessor.convertRabbitAccessException(RabbitAccessor.java:112)
    at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:841)
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:820)
    at org.springframework.amqp.rabbit.core.RabbitTemplate.doSendAndReceiveWithTemporary(RabbitTemplate.java:705)
    at org.springframework.amqp.rabbit.core.RabbitTemplate.doSendAndReceive(RabbitTemplate.java:697)
    at org.springframework.amqp.rabbit.core.RabbitTemplate.convertSendAndReceive(RabbitTemplate.java:673)
    at org.springframework.amqp.rabbit.core.RabbitTemplate.convertSendAndReceive(RabbitTemplate.java:663)
    at prodsend.Prod.sendAndReply(ReplyTester.java:137)
    at prodsend.ReplyTester.sendMessages(ReplyTester.java:49)
    at prodsend.ReplyTester.main(ReplyTester.java:102)
Caused by: java.lang.IllegalArgumentException: Send-and-receive methods can only be used if the Message does not already have a replyTo property.
    at org.springframework.util.Assert.isNull(Assert.java:89)
    at org.springframework.amqp.rabbit.core.RabbitTemplate$6.doInRabbit(RabbitTemplate.java:711)
    at org.springframework.amqp.rabbit.core.RabbitTemplate$6.doInRabbit(RabbitTemplate.java:705)
    at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:835)
    ... 8 more

行ReplyTest.137指向上述return方法中的sendAndReply行。


编辑: 这是上面提到的Data类:)

class Data{
    public String d;
    public Data(String s){ d = s; }
    public String toString() { return d; }
}

1 个答案:

答案 0 :(得分:6)

来自documentation

  

基本RPC模式。使用特定路由密钥将消息发送到默认交换,并尝试接收响应。实现通常会将reply-to标头设置为独占队列,并等待一段时间限制超时。

因此方法convertSendAndReceive处理设置replyTo标头并返回Messaage - 响应。这是同步模式 - RPC。

如果你想这样做异步 - 你似乎 - 不要使用这种方法。使用相应的convertAndSend方法,并使用相应的MessagePostProcessor添加replyTo标头。

由于这是异步的,您需要注册单独的处理程序以接收回复。这需要在发送消息到另一方之前完成。然后在发送消息后的某个时刻调用此处理程序 - 何时未知。阅读Spring AQMP Documentation

3.5.2 Asynchronous Consumer 部分

所以,异步流程:

  1. 发件人在replyTo queueue
  2. 上注册处理程序
  3. 发件人使用replyTo设置
  4. 发送邮件
  5. 客户电话receiveAndReply,处理邮件,并向replyTo
  6. 发送回复
  7. 触发发件人回调方法
  8. 同步流程是:

    1. 发件人使用sendAndReceive发送邮件并阻止
    2. 客户电话receiveAndReply,处理邮件,并向replyTo
    3. 发送回复
    4. 发件人收到回复,唤醒并处理
    5. 所以后一种情况要求发件人等待。当您使用receiveXXX而不是注册异步处理程序时,如果客户端需要一段时间来调用receiveXXX,则发件人可能会等待很长时间。

      顺便提一下,如果您想使用同步方法但使用特定replyTo,则可以随时拨打setReplyQueue。对于我提到的案例,还有一个setReplyTimeout,客户要么没有阅读邮件或忘记回复。