Post 使用 Spring AMQP 注释驱动的回复处理消息 (@SentTo)
Post Process Message with Spring AMQP Annotation Driven Reply (@SentTo)
我正在尝试根据此处的文档使用 @SentTo
注释将我的 return 值发送到 RabbitMQ 交换:https://docs.spring.io/spring-amqp/reference/htmlsingle/#async-annotation-driven-reply
如果我需要设置 heraders,文档说 return Message<OrderStatus>
并使用 MessageBuilder
.
但是,我需要设置 correlationId
消息 属性 而 MessageBuilder
没有办法让我设置属性,只有 header。
如何使用 @SentTo
注释复制我目前在这里所做的事情?
amqpTemplate.convertAndSend(
amqpRoutingKey,
orderStatus,
message -> {
message.getMessageProperties().setCorrelationId(orderStatus.getCorrelationId().toString());
return message;
}
);
谢谢!
有这个API:
/**
* 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
中甚至还有这个 API 来自刚刚发布的 Spring AMQP:
/**
* Set post processors that will be applied before sending replies.
* @param beforeSendReplyPostProcessors the post processors.
* @since 2.0.3
*/
public void setBeforeSendReplyPostProcessors(MessagePostProcessor... beforeSendReplyPostProcessors) {
更新2
如果您坚持使用 org.springframework.messaging.support.MessageBuilder
,那么您应该在该消息中设置一个 AmqpHeaders.CORRELATION_ID
header,它将正确地重新映射到 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);
}
我正在尝试根据此处的文档使用 @SentTo
注释将我的 return 值发送到 RabbitMQ 交换:https://docs.spring.io/spring-amqp/reference/htmlsingle/#async-annotation-driven-reply
如果我需要设置 heraders,文档说 return Message<OrderStatus>
并使用 MessageBuilder
.
但是,我需要设置 correlationId
消息 属性 而 MessageBuilder
没有办法让我设置属性,只有 header。
如何使用 @SentTo
注释复制我目前在这里所做的事情?
amqpTemplate.convertAndSend(
amqpRoutingKey,
orderStatus,
message -> {
message.getMessageProperties().setCorrelationId(orderStatus.getCorrelationId().toString());
return message;
}
);
谢谢!
有这个API:
/**
* 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
中甚至还有这个 API 来自刚刚发布的 Spring AMQP:
/**
* Set post processors that will be applied before sending replies.
* @param beforeSendReplyPostProcessors the post processors.
* @since 2.0.3
*/
public void setBeforeSendReplyPostProcessors(MessagePostProcessor... beforeSendReplyPostProcessors) {
更新2
如果您坚持使用 org.springframework.messaging.support.MessageBuilder
,那么您应该在该消息中设置一个 AmqpHeaders.CORRELATION_ID
header,它将正确地重新映射到 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);
}