[英]How to manually ack messages from rabbitmq using spring integration
[英]How to get header from RabbitMQ with spring integration
為了使用延遲交換,我使用以下方法通過“ int:gateway”消息將消息發送到RabbitMq:
void send(@Payload Notification request, @Header(MessageProperties.X_DELAY) Integer delay, @Header("routingKey") String routingKey);
我可以在RabbitMQ中看到標題正確顯示:x-delay:-60000
但是,當我從RabbitMQ收到此消息時如何獲得此標頭?
到目前為止,我收到的是以前作為Json發送的對象,但是如果嘗試獲取標頭,則會收到Exception。
正在發送:
integration.xml文件:
<!-- Producing service -->
<int:gateway id="gateway" default-request-channel="producingChannel" service-interface="Gateway"/>
<!-- Producing service -->
<!-- Service => RabbitMQ (Producing) -->
<int:chain input-channel="producingChannel">
<int:object-to-json-transformer/>
<int-amqp:outbound-channel-adapter exchange-name="${queuing.notifications-exchange}" routing-key-expression="headers.routingKey" mapped-request-headers="*"/>
</int:chain>
<!-- Service => RabbitMQ (Producing) -->
Java文件中的網關:
void send(@Payload Notification request, @Header(MessageProperties.X_DELAY) Integer delay, @Header("routingKey") String routingKey);
接收:
integration.xml文件:
<!-- RabbitMQ => Service (Consuming) -->
<int-amqp:inbound-channel-adapter channel="consumingChannel" queue-names="${queuing.operator.queue}" concurrent-consumers="${queuing.concurrent-consumers}" prefetch-count="${queuing.prefetch-count}" mapped-request-headers="*" error-channel="errorChannel" />
<!-- RabbitMQ => Service (Consuming) -->
<!-- Routing -->
<int:chain input-channel="consumingChannel">
<int:json-to-object-transformer type="Notification"/>
<int:service-activator ref="workingService" method="processNotificationFromQueue"/>
</int:chain>
<!-- Routing -->
Java文件中的WorkingService:
public void processNotificationFromQueue(Notification notification,
@Header(MessageProperties.X_DELAY) Integer delay) { ...
}
這里拋出異常:
Caused by: java.lang.IllegalArgumentException: required header not available: x-delay
您必須改用AmqpHeaders.RECEIVED_DELAY
。
由於您使用正確的mapping mapped-request-headers="*"
因此默認的DefaultAmqpHeaderMapper
可以正確映射:
Integer receivedDelay = amqpMessageProperties.getReceivedDelay();
if (receivedDelay != null) {
headers.put(AmqpHeaders.RECEIVED_DELAY, receivedDelay);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.