|
如需使用最新稳定版本,请使用 Spring Integration 7.0.4! |
异步出站网关
上一节讨论的网关是同步的,即发送线程会挂起,直到收到回复(或发生超时)。
Spring Integration 4.3 版本添加了一个异步网关,它使用了来自 Spring AMQP 的 AsyncRabbitTemplate。
当消息发送时,发送操作完成后线程会立即返回;当消息被接收时,回复将在模板的监听器容器线程上发送。
这在网关由轮询线程调用时非常有用。
线程将被释放,可用于框架中的其他任务。
以下列表展示了 AMQP 异步出站网关的可能配置选项:
-
Java DSL
-
Java
-
XML
@Configuration
public class AmqpAsyncApplication {
@Bean
public IntegrationFlow asyncAmqpOutbound(AsyncRabbitTemplate asyncRabbitTemplate) {
return f -> f
.handle(Amqp.asyncOutboundGateway(asyncRabbitTemplate)
.routingKey("queue1")); // default exchange - route to queue 'queue1'
}
@MessagingGateway(defaultRequestChannel = "asyncAmqpOutbound.input")
public interface MyGateway {
String sendToRabbit(String data);
}
}
@Configuration
public class AmqpAsyncConfig {
@Bean
@ServiceActivator(inputChannel = "amqpOutboundChannel")
public AsyncAmqpOutboundGateway amqpOutbound(AsyncRabbitTemplate asyncTemplate) {
AsyncAmqpOutboundGateway outbound = new AsyncAmqpOutboundGateway(asyncTemplate);
outbound.setRoutingKey("foo"); // default exchange - route to queue 'foo'
return outbound;
}
@Bean
public AsyncRabbitTemplate asyncTemplate(RabbitTemplate rabbitTemplate,
SimpleMessageListenerContainer replyContainer) {
return new AsyncRabbitTemplate(rabbitTemplate, replyContainer);
}
@Bean
public SimpleMessageListenerContainer replyContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(ccf);
container.setQueueNames("asyncRQ1");
return container;
}
@Bean
public MessageChannel amqpOutboundChannel() {
return new DirectChannel();
}
}
<int-amqp:outbound-async-gateway id="asyncOutboundGateway" (1)
request-channel="myRequestChannel" (2)
async-template="" (3)
exchange-name="" (4)
exchange-name-expression="" (5)
order="1" (6)
reply-channel="" (7)
reply-timeout="" (8)
requires-reply="" (9)
routing-key="" (10)
routing-key-expression="" (11)
default-delivery-mode"" (12)
confirm-correlation-expression="" (13)
confirm-ack-channel="" (14)
confirm-nack-channel="" (15)
confirm-timeout="" (16)
return-channel="" (17)
lazy-connect="true" /> (18)
| 1 | 此适配器的唯一 ID。 可选。 |
| 2 | 消息通道,消息应发送至此通道以进行转换并发布到 AMQP 交换。 必需。 |
| 3 | 对已配置的 AsyncRabbitTemplate 的 Bean 引用。
可选(默认为 asyncRabbitTemplate)。 |
| 4 | 要发送消息的 AMQP 交换器的名称。 如果未提供,消息将发送到默认的无名称交换器。 与 'exchange-name-expression' 互斥。 可选。 |
| 5 | 一个 SpEL 表达式,用于求值以确定消息发送到的 AMQP 交换机的名称,其中消息作为根对象。 如果未提供,消息将发送到默认的无名称交换机。 与 'exchange-name' 互斥。 可选。 |
| 6 | 当注册多个消费者时,为此消费者指定的顺序,从而实现负载均衡和故障转移。
可选(默认为 Ordered.LOWEST_PRECEDENCE [=Integer.MAX_VALUE])。 |
| 7 | 消息通道,用于在从 AMQP 队列接收并转换回复后发送这些回复。 可选。 |
| 8 | 网关在向 reply-channel 发送回复消息时等待的时间。
仅当 reply-channel 可能阻塞时才适用此设置——例如容量已满且当前处于满状态的 QueueChannel。
默认值为无限。 |
| 9 | 当在 AsyncRabbitTemplate’s `receiveTimeout 属性规定的时间内未收到回复消息,且此设置值为 true 时,网关会将错误消息发送至入站消息的 errorChannel 头部。
当在 AsyncRabbitTemplate’s `receiveTimeout 属性规定的时间内未收到回复消息,且此设置值为 false 时,网关会将错误消息发送至默认的 errorChannel(如果可用)。
其默认值为 true。 |
| 10 | 发送消息时要使用的路由键。
默认情况下,这是一个空的 String。
与 'routing-key-expression' 互斥。
可选。 |
| 11 | 一个 SpEL 表达式,用于在发送消息时计算所使用的路由键,
其中消息作为根对象(例如,'payload.key')。
默认情况下,这是一个空的 String。
与 'routing-key' 互斥。
可选。 |
| 12 | 消息的默认传递模式:PERSISTENT 或 NON_PERSISTENT。
如果设置了 header-mapper,则会被覆盖。
如果存在 Spring Integration 消息头(amqp_deliveryMode),则 DefaultHeaderMapper 将设置该值。
如果未提供此属性且头部映射器也未设置它,则默认值取决于底层 Spring AMQP 所使用的 MessagePropertiesConverter,该组件由 RabbitTemplate 使用。
如果未进行自定义,则默认值为 PERSISTENT。
可选。 |
| 13 | 定义关联数据的表达式。当提供此配置时,将配置底层的 AMQP 模板以接收发布者确认。需要专用的 RabbitTemplate 和一个 CachingConnectionFactory,且其 publisherConfirms 属性设置为 true。当收到发布者确认且提供了关联数据时,确认信息将根据确认类型写入 confirm-ack-channel 或 confirm-nack-channel。确认的负载是由此表达式定义的相关数据,且消息的 'amqp_publishConfirm' 头被设置为 true (ack) 或 false (nack)。对于 nack 个实例,将提供额外的标头(amqp_publishConfirmNackCause)。示例:headers['myCorrelationData']、payload。如果表达式解析为 Message<?> 实例(例如“#this”),则在 ack/nack 通道上发出的消息将基于该消息,并添加额外的标头。另请参阅 发布确认和返回的替代机制。
Optional. |
| 14 | 用于发送正向(ack)发布者确认的通道。
有效负载是由 confirm-correlation-expression 定义的相关数据。
要求底层的 AsyncRabbitTemplate 将其 enableConfirms 属性设置为 true。
另请参阅 发布者确认和返回的替代机制。
可选(默认为 nullChannel)。 |
| 15 | 自 4.2 版本起。
用于发送负面(nack)发布者确认的通道。
有效负载是由 confirm-correlation-expression 定义的相关数据。
要求底层的 AsyncRabbitTemplate 将其 enableConfirms 属性设置为 true。
另请参阅 发布者确认和返回的替代机制。
可选(默认为 nullChannel)。 |
| 16 | 设置后,如果在指定的毫秒时间内未收到发布确认,网关将合成一个负确认(nack)。 待处理的确认每 50% 的该值时间检查一次,因此实际发送 nack 的时间将在该值的 1 倍到 1.5 倍之间。 另请参阅 发布确认和返回的替代机制。 默认值为无(不会生成 nack)。 |
| 17 | 返回消息发送的目标通道。
如果提供此配置,底层的 AMQP 模板将被配置为将不可投递的消息返回给网关。
该消息基于从 AMQP 接收的数据构建,并包含以下附加头信息:amqp_returnReplyCode、amqp_returnReplyText、amqp_returnExchange和amqp_returnRoutingKey。
要求底层 AsyncRabbitTemplate 的 mandatory 属性设置为 true。
另请参阅 发布者确认与返回的替代机制。
可选。 |
| 18 | 当设置为 false 时,端点会在应用上下文初始化期间尝试连接到代理。
这样做允许通过记录错误消息(如果代理宕机)来快速失败地检测配置错误。
当设置为 true(默认值)时,连接会在发送第一条消息时建立(如果尚未由其他组件建立)。 |
另请参阅 异步服务激活器 以获取更多信息。
|
RabbitTemplate
当您使用确认和返回时,我们建议将 |