代码之家  ›  专栏  ›  技术社区  ›  nemo

使用Spring AMQP注释驱动的回复(@sentto)发布进程消息

  •  0
  • nemo  · 技术社区  · 6 年前

    我正在尝试使用 @SentTo 根据此处的文档,将返回值发送到rabbitmq exchange的注释: https://docs.spring.io/spring-amqp/reference/htmlsingle/#async-annotation-driven-reply

    如果我需要设置Heraders,文件上说要返回 Message<OrderStatus> 并使用 MessageBuilder .

    但是,我需要设置 correlationId 消息属性和 消息生成器 我没有办法设置属性,只有标题。

    如何使用 @ SentTo 注释?

    amqpTemplate.convertAndSend(
      amqpRoutingKey,
      orderStatus,
      message -> {
        message.getMessageProperties().setCorrelationId(orderStatus.getCorrelationId().toString());
        return message;
      }
    );
    

    谢谢!

    1 回复  |  直到 6 年前
        1
  •  1
  •   Artem Bilan    6 年前

    /**
     * Makes this builder's properties builder use a reference to properties.
     * @param properties The properties.
     * @return this.
     */
    public MessageBuilder andProperties(MessageProperties properties) {
    

    MessageBuilder message.getMessageProperties().setCorrelationId()

    AbstractRabbitListenerContainerFactory

    /**
     * Set post processors that will be applied before sending replies.
     * @param beforeSendReplyPostProcessors the post processors.
     * @since 2.0.3
     */
    public void setBeforeSendReplyPostProcessors(MessagePostProcessor... beforeSendReplyPostProcessors) {
    

    org.springframework.messaging.support.MessageBuilder AmqpHeaders.CORRELATION_ID CorrelationId

    /**
     * Post-process the given response message before it will be sent.
     * <p>
     * The default implementation sets the response's correlation id to the request message's correlation id, if any;
     * otherwise to the request message id.
     * @param request the original incoming Rabbit message
     * @param response the outgoing Rabbit message about to be sent
     */
    protected void postProcessResponse(Message request, Message response) {
        String correlation = request.getMessageProperties().getCorrelationId();
    
        if (correlation == null) {
            String messageId = request.getMessageProperties().getMessageId();
            if (messageId != null) {
                correlation = messageId;
            }
        }
        response.getMessageProperties().setCorrelationId(correlation);
    }